Class: Deimos::SchemaBackends::Base

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

Overview

Base class for encoding / decoding.

Direct Known Subclasses

AvroBase, Mock, Plain, ProtoBase

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, namespace: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • schema (String, Symbol)
  • namespace (String) (defaults to: nil)


39
40
41
42
# File 'lib/deimos/schema_backends/base.rb', line 39

def initialize(schema:, namespace: nil)
  @schema = schema
  @namespace = namespace
end

Instance Attribute Details

#key_schemaString

Returns:

  • (String)


35
36
37
# File 'lib/deimos/schema_backends/base.rb', line 35

def key_schema
  @key_schema
end

#namespaceString

Returns:

  • (String)


33
34
35
# File 'lib/deimos/schema_backends/base.rb', line 33

def namespace
  @namespace
end

#schemaString

Returns:

  • (String)


31
32
33
# File 'lib/deimos/schema_backends/base.rb', line 31

def schema
  @schema
end

Class Method Details

.content_typeString

The content type to use when encoding / decoding requests over HTTP via ActionController.

Returns:

  • (String)

Raises:



102
103
104
# File 'lib/deimos/schema_backends/base.rb', line 102

def self.content_type
  raise MissingImplementationError
end

.field_type(_schema) ⇒ String

Converts your schema to String form for generated YARD docs. To be defined by subclass.

Parameters:

  • schema (Object)

Returns:

  • (String)

    A string representation of the Type

Raises:



110
111
112
# File 'lib/deimos/schema_backends/base.rb', line 110

def self.field_type(_schema)
  raise MissingImplementationError
end

.mock_backendSymbol

Indicate a class which should act as a mocked version of this backend. This class should perform all validations but not actually do any encoding. Note that the “mock” version should return its own symbol when this is called, since it may be called multiple times depending on the order of RSpec helpers.

Returns:

  • (Symbol)


96
97
98
# File 'lib/deimos/schema_backends/base.rb', line 96

def self.mock_backend
  :mock
end

Instance Method Details

#coerce(payload) ⇒ Hash

Given a hash, coerce its types to our schema. To be defined by subclass.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)


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

def coerce(payload)
  result = {}
  self.schema_fields.each do |field|
    name = field.name
    next unless payload.key?(name)

    val = payload[name]
    result[name] = coerce_field(field, val)
  end
  result.with_indifferent_access
end

#coerce_field(_field, _value) ⇒ Object

Given a value and a field definition (as defined by whatever the underlying schema library is), coerce the given value to the given field type.

Parameters:

Returns:

  • (Object)

Raises:



151
152
153
# File 'lib/deimos/schema_backends/base.rb', line 151

def coerce_field(_field, _value)
  raise MissingImplementationError
end

#decode(payload, schema: nil) ⇒ Hash?

Decode a payload with a schema. Public method.

Parameters:

  • payload (String)
  • schema (String, Symbol) (defaults to: nil)

Returns:

  • (Hash, nil)


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

def decode(payload, schema: nil)
  return nil if payload.nil?

  decode_payload(payload, schema: schema || @schema)
end

#decode_key(_payload, _key_id) ⇒ String

Decode a message key. To be defined by subclass.

Parameters:

  • payload (Hash)

    the message itself.

  • key_id (String, Symbol)

    the field in the message to decode.

Returns:

  • (String)

Raises:



186
187
188
# File 'lib/deimos/schema_backends/base.rb', line 186

def decode_key(_payload, _key_id)
  raise MissingImplementationError
end

#decode_payload(_payload, schema:) ⇒ Hash

Decode a payload. To be defined by subclass.

Parameters:

  • payload (String)
  • schema (String, Symbol)

Returns:

  • (Hash)

Raises:



127
128
129
# File 'lib/deimos/schema_backends/base.rb', line 127

def decode_payload(_payload, schema:)
  raise MissingImplementationError
end

#encode(payload, schema: nil, topic: nil) ⇒ String

Encode a payload with a schema. Public method.

Parameters:

  • payload (Hash)
  • schema (String, Symbol) (defaults to: nil)
  • topic (String) (defaults to: nil)

Returns:

  • (String)


59
60
61
62
# File 'lib/deimos/schema_backends/base.rb', line 59

def encode(payload, schema: nil, topic: nil)
  validate(payload, schema: schema || @schema)
  encode_payload(payload, schema: schema || @schema, topic: topic)
end

#encode_key(_key, _key_id, topic: nil) ⇒ String

Encode a message key. To be defined by subclass.

Parameters:

  • key (String, Hash)

    the value to use as the key.

  • key_id (String, Symbol)

    the field name of the key.

  • topic (String) (defaults to: nil)

Returns:

  • (String)

Raises:



178
179
180
# File 'lib/deimos/schema_backends/base.rb', line 178

def encode_key(_key, _key_id, topic: nil)
  raise MissingImplementationError
end

#encode_payload(_payload, schema:, topic: nil) ⇒ String

Encode a payload. To be defined by subclass.

Parameters:

  • payload (Hash)
  • schema (String, Symbol)
  • topic (String) (defaults to: nil)

Returns:

  • (String)

Raises:



119
120
121
# File 'lib/deimos/schema_backends/base.rb', line 119

def encode_payload(_payload, schema:, topic: nil)
  raise MissingImplementationError
end

#generate_key_schema(_field_name) ⇒ Object

Generate a key schema from the given value schema and key ID. This is used when encoding or decoding keys from an existing value schema.

Parameters:

  • field_name (Symbol)

Raises:



169
170
171
# File 'lib/deimos/schema_backends/base.rb', line 169

def generate_key_schema(_field_name)
  raise MissingImplementationError
end

#load_schemaObject

Forcefully loads the schema into memory.

Returns:

  • (Object)

    The schema that is of use.

Raises:



192
193
194
# File 'lib/deimos/schema_backends/base.rb', line 192

def load_schema
  raise MissingImplementationError
end

#schema_fieldsArray<SchemaField>

List of field names belonging to the schema. To be defined by subclass.

Returns:

Raises:



141
142
143
# File 'lib/deimos/schema_backends/base.rb', line 141

def schema_fields
  raise MissingImplementationError
end

#sql_type(_field) ⇒ Symbol

Given a field definition, return the SQL type that might be used in ActiveRecord table creation - e.g. for Avro, a ‘long` type would return `:bigint`. There are also special values that need to be returned: `:array`, `:map` and `:record`, for types representing those structures. `:enum` is also recognized.

Parameters:

Returns:

  • (Symbol)

Raises:



162
163
164
# File 'lib/deimos/schema_backends/base.rb', line 162

def sql_type(_field)
  raise MissingImplementationError
end

#supports_class_generation?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/deimos/schema_backends/base.rb', line 50

def supports_class_generation?
  false
end

#supports_key_schemas?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/deimos/schema_backends/base.rb', line 45

def supports_key_schemas?
  false
end

#validate(_payload, schema:) ⇒ void

This method returns an undefined value.

Validate that a payload matches the schema. To be defined by subclass.

Parameters:

  • payload (Hash)
  • schema (String, Symbol)

Raises:



135
136
137
# File 'lib/deimos/schema_backends/base.rb', line 135

def validate(_payload, schema:)
  raise MissingImplementationError
end