Class: Deimos::SchemaBackends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/deimos/schema_backends/base.rb

Overview

Base class for encoding / decoding.

Direct Known Subclasses

AvroBase, Mock

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, namespace: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • schema (String|Symbol)
  • namespace (String) (defaults to: nil)


26
27
28
29
# File 'lib/deimos/schema_backends/base.rb', line 26

def initialize(schema:, namespace: nil)
  @schema = schema
  @namespace = namespace
end

Instance Attribute Details

#key_schemaObject

Returns the value of attribute key_schema.



22
23
24
# File 'lib/deimos/schema_backends/base.rb', line 22

def key_schema
  @key_schema
end

#namespaceObject

Returns the value of attribute namespace.



22
23
24
# File 'lib/deimos/schema_backends/base.rb', line 22

def namespace
  @namespace
end

#schemaObject

Returns the value of attribute schema.



22
23
24
# File 'lib/deimos/schema_backends/base.rb', line 22

def schema
  @schema
end

Class Method Details

.content_typeString

The content type to use when encoding / decoding requests over HTTP via ActionController.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/deimos/schema_backends/base.rb', line 79

def self.content_type
  raise NotImplementedError
end

.field_type(schema) ⇒ String

Converts your schema to String form for generated YARD docs. To be defined by subclass.

Parameters:

  • schema (Object)

Returns:

  • (String)

    A string representation of the Type

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/deimos/schema_backends/base.rb', line 87

def self.field_type(schema)
  raise NotImplementedError
end

.mock_backendSymbol

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.

Returns:

  • (Symbol)


73
74
75
# File 'lib/deimos/schema_backends/base.rb', line 73

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.

Parameters:

  • payload (Hash)

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/deimos/schema_backends/base.rb', line 54

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.

Parameters:

Returns:

  • (Object)

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/deimos/schema_backends/base.rb', line 127

def coerce_field(_field, _value)
  raise NotImplementedError
end

#decode(payload, schema: nil) ⇒ Hash?

Decode a payload with a schema. Public method.

Parameters:

  • payload (String)
  • schema (Symbol|String) (defaults to: nil)

Returns:

  • (Hash, nil)


45
46
47
48
49
# File 'lib/deimos/schema_backends/base.rb', line 45

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.

Parameters:

  • payload (Hash)

    the message itself.

  • key_id (Symbol|String)

    the field in the message to decode.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


155
156
157
# File 'lib/deimos/schema_backends/base.rb', line 155

def decode_key(_payload, _key_id)
  raise NotImplementedError
end

#decode_payload(_payload, schema:) ⇒ Hash

Decode a payload. To be defined by subclass.

Parameters:

  • payload (String)
  • schema (String|Symbol)

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/deimos/schema_backends/base.rb', line 104

def decode_payload(_payload, schema:)
  raise NotImplementedError
end

#encode(payload, schema: nil, topic: nil) ⇒ String

Encode a payload with a schema. Public method.

Parameters:

  • payload (Hash)
  • schema (Symbol|String) (defaults to: nil)
  • topic (String) (defaults to: nil)

Returns:

  • (String)


36
37
38
39
# File 'lib/deimos/schema_backends/base.rb', line 36

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.

Parameters:

  • key (String|Hash)

    the value to use as the key.

  • key_id (Symbol|String)

    the field name of the key.

  • topic (String) (defaults to: nil)

Returns:

  • (String)

Raises:

  • (NotImplementedError)


147
148
149
# File 'lib/deimos/schema_backends/base.rb', line 147

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.

Parameters:

  • payload (Hash)
  • schema (Symbol|String)
  • topic (String) (defaults to: nil)

Returns:

  • (String)

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/deimos/schema_backends/base.rb', line 96

def encode_payload(_payload, schema:, topic: nil)
  raise NotImplementedError
end

#load_schemaObject

Forcefully loads the schema into memory.

Returns:

  • (Object)

    The schema that is of use.

Raises:

  • (NotImplementedError)


161
162
163
# File 'lib/deimos/schema_backends/base.rb', line 161

def load_schema
  raise NotImplementedError
end

#schema_fieldsArray<SchemaField>

List of field names belonging to the schema. To be defined by subclass.

Returns:

Raises:

  • (NotImplementedError)


117
118
119
# File 'lib/deimos/schema_backends/base.rb', line 117

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.

Parameters:

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)


138
139
140
# File 'lib/deimos/schema_backends/base.rb', line 138

def sql_type(field)
  raise NotImplementedError
end

#validate(_payload, schema:) ⇒ Object

Validate that a payload matches the schema. To be defined by subclass.

Parameters:

  • payload (Hash)
  • schema (String|Symbol)

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/deimos/schema_backends/base.rb', line 111

def validate(_payload, schema:)
  raise NotImplementedError
end