Class: Deimos::SchemaBackends::Base
- Inherits:
-
Object
- Object
- Deimos::SchemaBackends::Base
- Defined in:
- lib/deimos/schema_backends/base.rb
Overview
Base class for encoding / decoding.
Instance Attribute Summary collapse
-
#key_schema ⇒ Object
Returns the value of attribute key_schema.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#schema ⇒ Object
Returns the value of attribute schema.
Class Method Summary collapse
-
.mock_backend ⇒ Symbol
Indicate a class which should act as a mocked version of this backend.
Instance Method Summary collapse
-
#coerce(payload) ⇒ Hash
Given a hash, coerce its types to our schema.
-
#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.
-
#decode(payload, schema: nil) ⇒ Hash
Decode a payload with a schema.
-
#decode_key(_payload, _key_id) ⇒ String
Decode a message key.
-
#decode_payload(_payload, schema:) ⇒ Hash
Decode a payload.
-
#encode(payload, schema: nil, topic: nil) ⇒ String
Encode a payload with a schema.
-
#encode_key(_key, _key_id, topic: nil) ⇒ String
Encode a message key.
-
#encode_payload(_payload, schema:, topic: nil) ⇒ String
Encode a payload.
-
#initialize(schema:, namespace: nil) ⇒ Base
constructor
A new instance of Base.
-
#schema_fields ⇒ Array<SchemaField>
List of field names belonging to the schema.
-
#validate(_payload, schema:) ⇒ Object
Validate that a payload matches the schema.
Constructor Details
#initialize(schema:, namespace: nil) ⇒ Base
Returns a new instance of Base.
23 24 25 26 |
# File 'lib/deimos/schema_backends/base.rb', line 23 def initialize(schema:, namespace: nil) @schema = schema @namespace = namespace end |
Instance Attribute Details
#key_schema ⇒ Object
Returns the value of attribute key_schema.
19 20 21 |
# File 'lib/deimos/schema_backends/base.rb', line 19 def key_schema @key_schema end |
#namespace ⇒ Object
Returns the value of attribute namespace.
19 20 21 |
# File 'lib/deimos/schema_backends/base.rb', line 19 def namespace @namespace end |
#schema ⇒ Object
Returns the value of attribute schema.
19 20 21 |
# File 'lib/deimos/schema_backends/base.rb', line 19 def schema @schema end |
Class Method Details
.mock_backend ⇒ Symbol
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.
68 69 70 |
# File 'lib/deimos/schema_backends/base.rb', line 68 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.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/deimos/schema_backends/base.rb', line 49 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.
108 109 110 |
# File 'lib/deimos/schema_backends/base.rb', line 108 def coerce_field(_field, _value) raise NotImplementedError end |
#decode(payload, schema: nil) ⇒ Hash
Decode a payload with a schema. Public method.
42 43 44 |
# File 'lib/deimos/schema_backends/base.rb', line 42 def decode(payload, schema: nil) decode_payload(payload, schema: schema || @schema) end |
#decode_key(_payload, _key_id) ⇒ String
Decode a message key. To be defined by subclass.
125 126 127 |
# File 'lib/deimos/schema_backends/base.rb', line 125 def decode_key(_payload, _key_id) raise NotImplementedError end |
#decode_payload(_payload, schema:) ⇒ Hash
Decode a payload. To be defined by subclass.
85 86 87 |
# File 'lib/deimos/schema_backends/base.rb', line 85 def decode_payload(_payload, schema:) raise NotImplementedError end |
#encode(payload, schema: nil, topic: nil) ⇒ String
Encode a payload with a schema. Public method.
33 34 35 36 |
# File 'lib/deimos/schema_backends/base.rb', line 33 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.
117 118 119 |
# File 'lib/deimos/schema_backends/base.rb', line 117 def encode_key(_key, _key_id, topic: nil) raise NotImplementedError end |
#encode_payload(_payload, schema:, topic: nil) ⇒ String
Encode a payload. To be defined by subclass.
77 78 79 |
# File 'lib/deimos/schema_backends/base.rb', line 77 def encode_payload(_payload, schema:, topic: nil) raise NotImplementedError end |
#schema_fields ⇒ Array<SchemaField>
List of field names belonging to the schema. To be defined by subclass.
98 99 100 |
# File 'lib/deimos/schema_backends/base.rb', line 98 def schema_fields raise NotImplementedError end |
#validate(_payload, schema:) ⇒ Object
Validate that a payload matches the schema. To be defined by subclass.
92 93 94 |
# File 'lib/deimos/schema_backends/base.rb', line 92 def validate(_payload, schema:) raise NotImplementedError end |