Class: Deimos::SchemaBackends::ProtoBase

Inherits:
Base
  • Object
show all
Defined in:
lib/deimos/schema_backends/proto_base.rb

Overview

Encode / decode using Avro, either locally or via schema registry.

Direct Known Subclasses

ProtoLocal, ProtoSchemaRegistry

Constant Summary collapse

SQL_MAP =
{
  string: :string,
  int32: :integer,
  uint32: :integer,
  sint32: :integer,
  fixed32: :integer,
  sfixed32: :integer,
  int64: :bigint,
  uint64: :bigint,
  sint64: :bigint,
  fixed64: :bigint,
  sfixed64: :bigint,
  bool: :boolean,
  bytes: :string,
  float: :float,
  message: :record
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#key_schema, #namespace, #registry_info, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

content_type, #decode, #decode_payload, #encode, #encode_payload, field_type, #generate_key_schema, #initialize, #inspect, #load_schema, #supports_class_generation?

Constructor Details

This class inherits a constructor from Deimos::SchemaBackends::Base

Class Method Details

.mock_backendObject



108
109
110
# File 'lib/deimos/schema_backends/proto_base.rb', line 108

def self.mock_backend
  :mock
end

Instance Method Details

#coerce(payload) ⇒ Object



88
89
90
# File 'lib/deimos/schema_backends/proto_base.rb', line 88

def coerce(payload)
  payload
end

#coerce_field(field, value) ⇒ Object



93
94
# File 'lib/deimos/schema_backends/proto_base.rb', line 93

def coerce_field(field, value)
end

#decode_key(payload, key_id) ⇒ Object



69
70
71
72
# File 'lib/deimos/schema_backends/proto_base.rb', line 69

def decode_key(payload, key_id)
  val = decode_proto_key(payload)
  key_id ? val[key_id.to_s] : val
end

#decode_proto_key(payload) ⇒ Object



62
63
64
65
66
# File 'lib/deimos/schema_backends/proto_base.rb', line 62

def decode_proto_key(payload)
  JSON.parse(payload)
rescue StandardError
  payload
end

#encode_key(key_id, key, topic: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/deimos/schema_backends/proto_base.rb', line 37

def encode_key(key_id, key, topic: nil)
  if key.respond_to?(:to_h)
    hash = if key_id
             key_id.to_s.split('.')[...-1].each do |k|
               key = key.with_indifferent_access[k]
             end
             key.to_h.with_indifferent_access.slice(key_id.split('.').last)
           else
             key.to_h.sort.to_h
           end
    self.encode_proto_key(hash, topic: topic, field: key_id)
  elsif key_id
    hash = { key_id.to_s.split('.').last => key }
    self.encode_proto_key(hash, topic: topic, field: key_id)
  else
    key.to_s
  end
end

#encode_proto_key(hash, topic: nil, field: nil) ⇒ String

Parameters:

  • hash (Hash)

Returns:

  • (String)


58
59
60
# File 'lib/deimos/schema_backends/proto_base.rb', line 58

def encode_proto_key(hash, topic: nil, field: nil)
  hash.sort.to_h.to_json
end

#proto_schema(schema = @schema) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/deimos/schema_backends/proto_base.rb', line 27

def proto_schema(schema=@schema)
  proto = Google::Protobuf::DescriptorPool.generated_pool.lookup(schema)
  if proto.nil?
    raise "Could not find Protobuf schema '#{schema}'."
  end

  proto
end

#schema_fieldsObject



97
98
99
100
101
# File 'lib/deimos/schema_backends/proto_base.rb', line 97

def schema_fields
  proto_schema.to_a.map do |f|
    SchemaField.new(f.name, f.subtype&.name || 'record', [], nil)
  end
end

#sql_type(field) ⇒ Object

:nodoc:



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/deimos/schema_backends/proto_base.rb', line 75

def sql_type(field)
  type = field.type
  return SQL_MAP[type] if SQL_MAP[type]
  return :array if type.repeated?

  if type == :double
    warn('Protobuf `double` type turns into SQL `float` type. Please ensure you have the correct `limit` set.')
    return :float
  end

  :string
end

#supports_key_schemas?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/deimos/schema_backends/proto_base.rb', line 112

def supports_key_schemas?
  false
end

#validate(payload, schema:) ⇒ Object



104
105
# File 'lib/deimos/schema_backends/proto_base.rb', line 104

def validate(payload, schema:)
end