Class: Deimos::SchemaBackends::AvroLocal

Inherits:
AvroBase show all
Defined in:
lib/deimos/schema_backends/avro_local.rb,
sig/defs.rbs

Overview

Encode / decode using local Avro encoding.

Instance Attribute Summary

Attributes inherited from AvroBase

#schema_store

Attributes inherited from Base

#key_schema, #namespace, #registry_info, #schema

Instance Method Summary collapse

Methods inherited from AvroBase

#coerce_field, content_type, #decode_key, #encode_key, field_type, #generate_key_schema, #initialize, #load_schema, mock_backend, schema_base_class, schema_classname, #schema_fields, #sql_type, #supports_class_generation?, #supports_key_schemas?, #validate

Methods inherited from Base

#coerce, #coerce_field, content_type, #decode, #decode_key, #encode, #encode_key, field_type, #generate_key_schema, #initialize, #inspect, #load_schema, mock_backend, #schema_fields, #sql_type, #supports_class_generation?, #supports_key_schemas?, #validate

Constructor Details

This class inherits a constructor from Deimos::SchemaBackends::AvroBase

Instance Method Details

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

Parameters:

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

Returns:

  • (::Hash[untyped, untyped])


10
11
12
13
14
15
# File 'lib/deimos/schema_backends/avro_local.rb', line 10

def decode_payload(payload, schema:)
  stream = StringIO.new(payload)
  schema = @schema_store.find("#{@namespace}.#{schema}")
  reader = Avro::IO::DatumReader.new(nil, schema)
  Avro::DataFile::Reader.new(stream, reader).first
end

#encode_payload(payload, schema: nil, subject: nil) ⇒ String

Parameters:

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

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
# File 'lib/deimos/schema_backends/avro_local.rb', line 18

def encode_payload(payload, schema: nil, subject: nil)
  stream = StringIO.new
  schema = schema_store.find("#{@namespace}.#{schema}")
  writer = Avro::IO::DatumWriter.new(schema)

  dw = Avro::DataFile::Writer.new(stream, writer, schema)
  dw << payload.to_h
  dw.close
  stream.string
end