Class: Deimos::SchemaBackends::Base
- Inherits:
-
Object
- Object
- Deimos::SchemaBackends::Base
- Defined in:
- lib/deimos/schema_backends/base.rb,
sig/defs.rbs
Overview
Base class for encoding / decoding.
Instance Attribute Summary collapse
Class Method Summary collapse
-
.content_type ⇒ String
The content type to use when encoding / decoding requests over HTTP via ActionController.
-
.field_type(_schema) ⇒ String
Converts your schema to String form for generated YARD docs.
-
.mock_backend ⇒ Symbol
Indicate a class which should act as a mocked version of this backend.
Instance Method Summary collapse
-
#coerce(payload) ⇒ ::Hash[untyped, untyped]
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[untyped, untyped]?
Decode a payload with a schema.
-
#decode_key(_payload, _key_id) ⇒ String
Decode a message key.
-
#decode_payload(_payload, schema:) ⇒ ::Hash[untyped, untyped]
Decode a payload.
-
#encode(payload, schema: nil, topic: nil, is_key: false) ⇒ String
Encode a payload with a schema.
-
#encode_key(_key, _key_id, topic: nil) ⇒ String
Encode a message key.
-
#encode_payload(_payload, schema:, subject: nil) ⇒ String
Encode a payload.
-
#generate_key_schema(_field_name) ⇒ Object
Generate a key schema from the given value schema and key ID.
-
#initialize(schema:, namespace: nil, registry_info: nil) ⇒ Base
constructor
@param
schema. - #inspect ⇒ String
-
#load_schema ⇒ Object
Forcefully loads the schema into memory.
-
#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.
- #supports_class_generation? ⇒ Boolean
- #supports_key_schemas? ⇒ Boolean
-
#validate(_payload, schema:) ⇒ void
Validate that a payload matches the schema.
Constructor Details
#initialize(schema:, namespace: nil, registry_info: nil) ⇒ Base
@param schema
@param namespace
59 60 61 62 63 |
# File 'lib/deimos/schema_backends/base.rb', line 59 def initialize(schema:, namespace: nil, registry_info: nil) @schema = schema @namespace = namespace @registry_info = registry_info end |
Instance Attribute Details
#key_schema ⇒ String
53 54 55 |
# File 'lib/deimos/schema_backends/base.rb', line 53 def key_schema @key_schema end |
#namespace ⇒ String
51 52 53 |
# File 'lib/deimos/schema_backends/base.rb', line 51 def namespace @namespace end |
#registry_info ⇒ RegistryInfo
55 56 57 |
# File 'lib/deimos/schema_backends/base.rb', line 55 def registry_info @registry_info end |
#schema ⇒ String
49 50 51 |
# File 'lib/deimos/schema_backends/base.rb', line 49 def schema @schema end |
Class Method Details
.content_type ⇒ String
The content type to use when encoding / decoding requests over HTTP via ActionController.
130 131 132 |
# File 'lib/deimos/schema_backends/base.rb', line 130 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.
@param schema
@return — A string representation of the Type
138 139 140 |
# File 'lib/deimos/schema_backends/base.rb', line 138 def self.field_type(_schema) raise MissingImplementationError 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.
124 125 126 |
# File 'lib/deimos/schema_backends/base.rb', line 124 def self.mock_backend :mock end |
Instance Method Details
#coerce(payload) ⇒ ::Hash[untyped, untyped]
Given a hash, coerce its types to our schema. To be defined by subclass.
@param payload
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/deimos/schema_backends/base.rb', line 105 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.
@param field
@param value
179 180 181 |
# File 'lib/deimos/schema_backends/base.rb', line 179 def coerce_field(_field, _value) raise MissingImplementationError end |
#decode(payload, schema: nil) ⇒ ::Hash[untyped, untyped]?
Decode a payload with a schema. Public method.
@param payload
@param schema
96 97 98 99 100 |
# File 'lib/deimos/schema_backends/base.rb', line 96 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.
@param payload — the message itself.
@param key_id — the field in the message to decode.
214 215 216 |
# File 'lib/deimos/schema_backends/base.rb', line 214 def decode_key(_payload, _key_id) raise MissingImplementationError end |
#decode_payload(_payload, schema:) ⇒ ::Hash[untyped, untyped]
Decode a payload. To be defined by subclass.
@param payload
@param schema
155 156 157 |
# File 'lib/deimos/schema_backends/base.rb', line 155 def decode_payload(_payload, schema:) raise MissingImplementationError end |
#encode(payload, schema: nil, topic: nil, is_key: false) ⇒ String
Encode a payload with a schema. Public method.
@param payload
@param schema
@param topic
85 86 87 88 89 90 |
# File 'lib/deimos/schema_backends/base.rb', line 85 def encode(payload, schema: nil, topic: nil, is_key: false) validate(payload, schema: schema || @schema) subject = topic.presence || schema subject = is_key ? "#{subject}-key" : "#{subject}-value" encode_payload(payload, schema: schema || @schema, subject: subject) end |
#encode_key(_key, _key_id, topic: nil) ⇒ String
Encode a message key. To be defined by subclass.
@param key — the value to use as the key.
@param key_id — the field name of the key.
@param topic
206 207 208 |
# File 'lib/deimos/schema_backends/base.rb', line 206 def encode_key(_key, _key_id, topic: nil) raise MissingImplementationError end |
#encode_payload(_payload, schema:, subject: nil) ⇒ String
Encode a payload. To be defined by subclass.
@param payload
@param schema
@param topic
147 148 149 |
# File 'lib/deimos/schema_backends/base.rb', line 147 def encode_payload(_payload, schema:, subject: 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.
197 198 199 |
# File 'lib/deimos/schema_backends/base.rb', line 197 def generate_key_schema(_field_name) raise MissingImplementationError end |
#inspect ⇒ String
66 67 68 |
# File 'lib/deimos/schema_backends/base.rb', line 66 def inspect "Type #{self.class.name.demodulize} Schema: #{@namespace}.#{@schema} Key schema: #{@key_schema}" end |
#load_schema ⇒ Object
Forcefully loads the schema into memory.
@return — The schema that is of use.
220 221 222 |
# File 'lib/deimos/schema_backends/base.rb', line 220 def load_schema raise MissingImplementationError end |
#schema_fields ⇒ ::Array[SchemaField]
List of field names belonging to the schema. To be defined by subclass.
169 170 171 |
# File 'lib/deimos/schema_backends/base.rb', line 169 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.
@param field
190 191 192 |
# File 'lib/deimos/schema_backends/base.rb', line 190 def sql_type(_field) raise MissingImplementationError end |
#supports_class_generation? ⇒ Boolean
76 77 78 |
# File 'lib/deimos/schema_backends/base.rb', line 76 def supports_class_generation? false end |
#supports_key_schemas? ⇒ Boolean
71 72 73 |
# File 'lib/deimos/schema_backends/base.rb', line 71 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.
@param payload
@param schema
163 164 165 |
# File 'lib/deimos/schema_backends/base.rb', line 163 def validate(_payload, schema:) raise MissingImplementationError end |