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 (AvroTurf::ConfluentSchemaRegistry) (defaults to: nil)

    Optional schema registry client.

  • registry_url (String) (defaults to: nil)

    Optional URL for schema registry server.



45
46
47
48
49
50
51
52
# File 'lib/avromatic/model/message_decoder.rb', line 45

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::ConfluentSchemaRegistry.new(registry_url, logger: Avromatic.logger)) ||
    Avromatic.build_schema_registry
end

Class Method Details

.model_key(model) ⇒ Object



34
35
36
37
# File 'lib/avromatic/model/message_decoder.rb', line 34

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

Returns:



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

def decode(*args)
  model, message_key, message_value = extract_decode_args(*args)
  model.avro_message_decode(message_key, message_value)
end

#decode_hash(*args) ⇒ Hash

Returns:

  • (Hash)


61
62
63
64
# File 'lib/avromatic/model/message_decoder.rb', line 61

def decode_hash(*args)
  model, message_key, message_value = extract_decode_args(*args)
  model.avro_message_attributes(message_key, message_value)
end

#model(*args) ⇒ Avromatic model class

Returns:



67
68
69
# File 'lib/avromatic/model/message_decoder.rb', line 67

def model(*args)
  extract_decode_args(*args).first
end