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
-
.content_type ⇒ String
The content type to use when encoding / decoding requests over HTTP via ActionController.
-
.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.
-
#sql_type(field) ⇒ Symbol
Given a field definition, return the SQL type that might be used in ActiveRecord table creation - e.g.
-
#validate(_payload, schema:) ⇒ Object
Validate that a payload matches the schema.
Constructor Details
#initialize(schema:, namespace: nil) ⇒ Base
Returns a new instance of Base.
25 26 27 28 |
# File 'lib/deimos/schema_backends/base.rb', line 25 def initialize(schema:, namespace: nil) @schema = schema @namespace = namespace end |
Instance Attribute Details
#key_schema ⇒ Object
Returns the value of attribute key_schema.
21 22 23 |
# File 'lib/deimos/schema_backends/base.rb', line 21 def key_schema @key_schema end |
#namespace ⇒ Object
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/deimos/schema_backends/base.rb', line 21 def namespace @namespace end |
#schema ⇒ Object
Returns the value of attribute schema.
21 22 23 |
# File 'lib/deimos/schema_backends/base.rb', line 21 def schema @schema end |
Class Method Details
.content_type ⇒ String
The content type to use when encoding / decoding requests over HTTP via ActionController.
77 78 79 |
# File 'lib/deimos/schema_backends/base.rb', line 77 def self.content_type raise NotImplementedError end |
.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.
71 72 73 |
# File 'lib/deimos/schema_backends/base.rb', line 71 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.
52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ 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.
117 118 119 |
# File 'lib/deimos/schema_backends/base.rb', line 117 def coerce_field(_field, _value) raise NotImplementedError end |
#decode(payload, schema: nil) ⇒ Hash?
Decode a payload with a schema. Public method.
44 45 46 47 |
# 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) ⇒ String
Decode a message key. To be defined by subclass.
145 146 147 |
# File 'lib/deimos/schema_backends/base.rb', line 145 def decode_key(_payload, _key_id) raise NotImplementedError end |
#decode_payload(_payload, schema:) ⇒ Hash
Decode a payload. To be defined by subclass.
94 95 96 |
# File 'lib/deimos/schema_backends/base.rb', line 94 def decode_payload(_payload, schema:) raise NotImplementedError end |
#encode(payload, schema: nil, topic: nil) ⇒ String
Encode a payload with a schema. Public method.
35 36 37 38 |
# 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) ⇒ String
Encode a message key. To be defined by subclass.
137 138 139 |
# 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) ⇒ String
Encode a payload. To be defined by subclass.
86 87 88 |
# File 'lib/deimos/schema_backends/base.rb', line 86 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.
107 108 109 |
# File 'lib/deimos/schema_backends/base.rb', line 107 def schema_fields raise NotImplementedError 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.
128 129 130 |
# File 'lib/deimos/schema_backends/base.rb', line 128 def sql_type(field) raise NotImplementedError end |
#validate(_payload, schema:) ⇒ Object
Validate that a payload matches the schema. To be defined by subclass.
101 102 103 |
# File 'lib/deimos/schema_backends/base.rb', line 101 def validate(_payload, schema:) raise NotImplementedError end |