class Deimos::SchemaBackends::Base

Base class for encoding / decoding.

Attributes

key_schema[RW]
namespace[RW]
schema[RW]

Public Class Methods

content_type() click to toggle source

The content type to use when encoding / decoding requests over HTTP via ActionController. @return [String]

# File lib/deimos/schema_backends/base.rb, line 77
def self.content_type
  raise NotImplementedError
end
mock_backend() click to toggle source

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 (e.g. avro_validation) should return its own symbol when this is called, since it may be called multiple times depending on the order of RSpec helpers. @return [Symbol]

# File lib/deimos/schema_backends/base.rb, line 71
def self.mock_backend
  :mock
end
new(schema:, namespace: nil) click to toggle source

@param schema [String|Symbol] @param namespace [String]

# File lib/deimos/schema_backends/base.rb, line 25
def initialize(schema:, namespace: nil)
  @schema = schema
  @namespace = namespace
end

Public Instance Methods

coerce(payload) click to toggle source

Given a hash, coerce its types to our schema. To be defined by subclass. @param payload [Hash] @return [Hash]

# File lib/deimos/schema_backends/base.rb, line 52
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) click to toggle source

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. @param field [SchemaField] @param value [Object] @return [Object]

# File lib/deimos/schema_backends/base.rb, line 117
def coerce_field(_field, _value)
  raise NotImplementedError
end
decode(payload, schema: nil) click to toggle source

Decode a payload with a schema. Public method. @param payload [String] @param schema [Symbol|String] @return [Hash,nil]

# File lib/deimos/schema_backends/base.rb, line 44
def decode(payload, schema: nil)
  return nil if payload.nil?
  decode_payload(payload, schema: schema || @schema)
end
decode_key(_payload, _key_id) click to toggle source

Decode a message key. To be defined by subclass. @param payload [Hash] the message itself. @param key_id [Symbol|String] the field in the message to decode. @return [String]

# File lib/deimos/schema_backends/base.rb, line 145
def decode_key(_payload, _key_id)
  raise NotImplementedError
end
decode_payload(_payload, schema:) click to toggle source

Decode a payload. To be defined by subclass. @param payload [String] @param schema [String|Symbol] @return [Hash]

# File lib/deimos/schema_backends/base.rb, line 94
def decode_payload(_payload, schema:)
  raise NotImplementedError
end
encode(payload, schema: nil, topic: nil) click to toggle source

Encode a payload with a schema. Public method. @param payload [Hash] @param schema [Symbol|String] @param topic [String] @return [String]

# File lib/deimos/schema_backends/base.rb, line 35
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) click to toggle source

Encode a message key. To be defined by subclass. @param key [String|Hash] the value to use as the key. @param key_id [Symbol|String] the field name of the key. @param topic [String] @return [String]

# File lib/deimos/schema_backends/base.rb, line 137
def encode_key(_key, _key_id, topic: nil)
  raise NotImplementedError
end
encode_payload(_payload, schema:, topic: nil) click to toggle source

Encode a payload. To be defined by subclass. @param payload [Hash] @param schema [Symbol|String] @param topic [String] @return [String]

# File lib/deimos/schema_backends/base.rb, line 86
def encode_payload(_payload, schema:, topic: nil)
  raise NotImplementedError
end
schema_fields() click to toggle source

List of field names belonging to the schema. To be defined by subclass. @return [Array<SchemaField>]

# File lib/deimos/schema_backends/base.rb, line 107
def schema_fields
  raise NotImplementedError
end
sql_type(field) click to toggle source

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. @param field [SchemaField] @return [Symbol]

# File lib/deimos/schema_backends/base.rb, line 128
def sql_type(field)
  raise NotImplementedError
end
validate(_payload, schema:) click to toggle source

Validate that a payload matches the schema. To be defined by subclass. @param payload [Hash] @param schema [String|Symbol]

# File lib/deimos/schema_backends/base.rb, line 101
def validate(_payload, schema:)
  raise NotImplementedError
end