class Deimos::SchemaBackends::Base
Base
class for encoding / decoding.
Attributes
Public Class Methods
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
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
@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
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
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 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 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 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 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 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 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
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
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 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