Class: Rimless::Karafka::AvroDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rimless/karafka/avro_deserializer.rb

Overview

A custom Apache Avro compatible message deserializer.

Instance Method Summary collapse

Instance Method Details

#call(params) ⇒ Hash{Symbol => Mixed}

Deserialize an Apache Avro encoded Apache Kafka message.

Parameters:

  • params (Karafka::Params::Params)

    the Karafka message parameters

Returns:

  • (Hash{Symbol => Mixed})

    the deserialized Apache Avro message



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rimless/karafka/avro_deserializer.rb', line 11

def call(params)
  # When the Kafka message does not have a payload, we won't fail.
  # This is for Kafka users which use log compaction with a nil payload.
  return if params.raw_payload.nil?

  # We use sparsed hashes inside of Apache Avro messages for schema-less
  # blobs of data, such as loosely structured metadata blobs.  Thats a
  # somewhat bad idea on strictly typed and defined messages, but their
  # occurence should be rare.
  Rimless
    .decode(params.raw_payload)
    .then { |data| Sparsify(data, sparse_array: true) }
    .then { |data| data.transform_keys { |key| key.delete('\\') } }
    .then { |data| Unsparsify(data, sparse_array: true) }
    .deep_symbolize_keys
end