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, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Class Method Details

.mock_backendObject



84
85
86
# File 'lib/deimos/schema_backends/proto_base.rb', line 84

def self.mock_backend
  :mock
end

Instance Method Details

#coerce(payload) ⇒ Object



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

def coerce(payload)
  payload
end

#coerce_field(field, value) ⇒ Object



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

def coerce_field(field, value)
end

#decode_key(payload, key_id) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/deimos/schema_backends/proto_base.rb', line 41

def decode_key(payload, key_id)
  val = begin
          JSON.parse(payload)
  rescue StandardError
          payload
  end
  key_id ? val[key_id.to_s] : val
end

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



32
33
34
35
36
37
38
# File 'lib/deimos/schema_backends/proto_base.rb', line 32

def encode_key(key_id, key, topic: nil)
  if key.is_a?(Hash)
    key_id ? key.with_indifferent_access[key_id].to_s : key.sort.to_h.to_json
  else
    key.to_s
  end
end

#generate_key_schema(_field_name) ⇒ Object



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

def generate_key_schema(_field_name)
  raise 'Protobuf cannot generate key schemas! Please use field_config :plain'
end

#proto_schema(schema = @schema) ⇒ Object



27
28
29
# File 'lib/deimos/schema_backends/proto_base.rb', line 27

def proto_schema(schema=@schema)
  Google::Protobuf::DescriptorPool.generated_pool.lookup(schema)
end

#schema_fieldsObject



73
74
75
76
77
# File 'lib/deimos/schema_backends/proto_base.rb', line 73

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:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/deimos/schema_backends/proto_base.rb', line 51

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

#validate(payload, schema:) ⇒ Object



80
81
# File 'lib/deimos/schema_backends/proto_base.rb', line 80

def validate(payload, schema:)
end