Class: Deimos::SchemaBackends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/deimos/schema_backends/base.rb,
sig/defs.rbs

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

@param schema

@param namespace

Parameters:

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


39
40
41
42
# File 'lib/deimos/schema_backends/base.rb', line 39

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

Instance Attribute Details

#key_schemaString

Returns:

  • (String)


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

def key_schema
  @key_schema
end

#namespaceString

Returns:

  • (String)


33
34
35
# File 'lib/deimos/schema_backends/base.rb', line 33

def namespace
  @namespace
end

#schemaString

Returns:

  • (String)


31
32
33
# File 'lib/deimos/schema_backends/base.rb', line 31

def schema
  @schema
end

Class Method Details

.content_typeString

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

Returns:

  • (String)


92
93
94
# File 'lib/deimos/schema_backends/base.rb', line 92

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.

@param schema

@return — A string representation of the Type

Parameters:

  • schema (Object)

Returns:

  • (String)


100
101
102
# File 'lib/deimos/schema_backends/base.rb', line 100

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)


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

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

Parameters:

  • payload (::Hash[untyped, untyped])

Returns:

  • (::Hash[untyped, untyped])


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/deimos/schema_backends/base.rb', line 67

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

Parameters:

Returns:

  • (Object)


141
142
143
# File 'lib/deimos/schema_backends/base.rb', line 141

def coerce_field(field, value)
  raise NotImplementedError
end

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

Decode a payload with a schema. Public method.

@param payload

@param schema

Parameters:

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

Returns:

  • (::Hash[untyped, untyped], nil)


58
59
60
61
62
# File 'lib/deimos/schema_backends/base.rb', line 58

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.

Parameters:

  • payload (::Hash[untyped, untyped])
  • key_id (String, Symbol)

Returns:

  • (String)


169
170
171
# File 'lib/deimos/schema_backends/base.rb', line 169

def decode_key(payload, key_id)
  raise NotImplementedError
end

#decode_payload(payload, schema:) ⇒ ::Hash[untyped, untyped]

Decode a payload. To be defined by subclass.

@param payload

@param schema

Parameters:

  • payload (String)
  • schema: (String, Symbol)

Returns:

  • (::Hash[untyped, untyped])


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

def decode_payload(payload, schema:)
  raise NotImplementedError
end

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

Encode a payload with a schema. Public method.

@param payload

@param schema

@param topic

Parameters:

  • payload (::Hash[untyped, untyped])
  • schema: (String, Symbol, nil) (defaults to: nil)
  • topic: (String, nil) (defaults to: nil)

Returns:

  • (String)


49
50
51
52
# File 'lib/deimos/schema_backends/base.rb', line 49

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.

@param key — the value to use as the key.

@param key_id — the field name of the key.

@param topic

Parameters:

  • key (String, ::Hash[untyped, untyped])
  • key_id (String, Symbol)
  • topic: (String, nil) (defaults to: nil)

Returns:

  • (String)


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

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.

@param payload

@param schema

@param topic

Parameters:

  • payload (::Hash[untyped, untyped])
  • schema: (String, Symbol)
  • topic: (String, nil) (defaults to: nil)

Returns:

  • (String)


109
110
111
# File 'lib/deimos/schema_backends/base.rb', line 109

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

#load_schemaObject

Forcefully loads the schema into memory.

@return — The schema that is of use.

Returns:

  • (Object)


175
176
177
# File 'lib/deimos/schema_backends/base.rb', line 175

def load_schema
  raise NotImplementedError
end

#schema_fields::Array[SchemaField]

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

Returns:



131
132
133
# File 'lib/deimos/schema_backends/base.rb', line 131

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.

@param field

Parameters:

Returns:

  • (Symbol)


152
153
154
# File 'lib/deimos/schema_backends/base.rb', line 152

def sql_type(field)
  raise NotImplementedError
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

Parameters:

  • payload (::Hash[untyped, untyped])
  • schema: (String, Symbol)


125
126
127
# File 'lib/deimos/schema_backends/base.rb', line 125

def validate(payload, schema:)
  raise NotImplementedError
end