Class: Avromatic::Model::MessageDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/avromatic/model/message_decoder.rb

Overview

This class is used to decode messages encoded using Avro to their corresponding models.

Defined Under Namespace

Classes: DuplicateKeyError, MagicByteError, UnexpectedKeyError

Constant Summary collapse

MAGIC_BYTE =
[0].pack('C').freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*models, schema_registry: nil, registry_url: nil) ⇒ MessageDecoder

Returns a new instance of MessageDecoder.

Parameters:

  • *models (generated models)

    Models to register for decoding.

  • schema_registry (Avromatic::SchemaRegistryClient) (defaults to: nil)

    Optional schema registry client.

  • registry_url (String) (defaults to: nil)

    Optional URL for schema registry server.



41
42
43
44
45
46
47
48
# File 'lib/avromatic/model/message_decoder.rb', line 41

def initialize(*models, schema_registry: nil, registry_url: nil)
  @model_map = build_model_map(models)
  @schema_names_by_id = {}
  @schema_registry = schema_registry ||
    Avromatic.schema_registry ||
    (registry_url && AvroTurf::SchemaRegistry.new(registry_url, logger: Avromatic.logger)) ||
    Avromatic.build_schema_registry
end

Class Method Details

.model_key(model) ⇒ Object



30
31
32
33
# File 'lib/avromatic/model/message_decoder.rb', line 30

def self.model_key(model)
  [model.key_avro_schema && model.key_avro_schema.fullname,
   model.value_avro_schema.fullname]
end

Instance Method Details

#decode(*args) ⇒ Avromatic model

If two arguments are specified then the first is interpreted as the message key and the second is the message value. If there is only one arg then it is used as the message value.

Returns:



54
55
56
57
58
59
# File 'lib/avromatic/model/message_decoder.rb', line 54

def decode(*args)
  message_key, message_value = args.size > 1 ? args : [nil, args.first]
  value_schema_name = schema_name_for_data(message_value)
  key_schema_name = schema_name_for_data(message_key) if message_key
  deserialize([key_schema_name, value_schema_name], message_key, message_value)
end