Sha256: bb21b044a0a1011e31545812c58fecc9e01feed113b3b258975ad1adeb322e06

Contents?: true

Size: 1.13 KB

Versions: 13

Compression:

Stored size: 1.13 KB

Contents

require 'avro_turf/messaging'
require 'avromatic/io/datum_reader'

module Avromatic
  # Subclass AvroTurf::Messaging to use a custom DatumReader for decode.
  class Messaging < AvroTurf::Messaging
    attr_reader :registry

    def decode(data, schema_name: nil, namespace: @namespace)
      readers_schema = schema_name && @schema_store.find(schema_name, namespace)
      stream = StringIO.new(data)
      decoder = Avro::IO::BinaryDecoder.new(stream)

      # The first byte is MAGIC!!!
      magic_byte = decoder.read(1)

      if magic_byte != MAGIC_BYTE
        raise "Expected data to begin with a magic byte, got `#{magic_byte.inspect}`"
      end

      # The schema id is a 4-byte big-endian integer.
      schema_id = decoder.read(4).unpack('N').first

      writers_schema = @schemas_by_id.fetch(schema_id) do
        schema_json = @registry.fetch(schema_id)
        @schemas_by_id[schema_id] = Avro::Schema.parse(schema_json)
      end

      # The following line differs from the parent class to use a custom DatumReader
      reader = Avromatic::IO::DatumReader.new(writers_schema, readers_schema)
      reader.read(decoder)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
avromatic-0.23.0 lib/avromatic/messaging.rb
avromatic-0.22.0 lib/avromatic/messaging.rb
avromatic-0.21.1 lib/avromatic/messaging.rb
avromatic-0.21.0 lib/avromatic/messaging.rb
avromatic-0.21.0.rc1 lib/avromatic/messaging.rb
avromatic-0.21.0.rc0 lib/avromatic/messaging.rb
avromatic-0.20.0 lib/avromatic/messaging.rb
avromatic-0.19.0 lib/avromatic/messaging.rb
avromatic-0.18.1 lib/avromatic/messaging.rb
avromatic-0.18.0 lib/avromatic/messaging.rb
avromatic-0.18.0.rc0 lib/avromatic/messaging.rb
avromatic-0.17.1 lib/avromatic/messaging.rb
avromatic-0.17.0 lib/avromatic/messaging.rb