Sha256: ae7eea0390a85f11d707e83b39fffdfe3b2eed7a5466306f8e0e217213e8c092

Contents?: true

Size: 1.49 KB

Versions: 13

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Avromatic
  module IO
    # Subclass DatumReader to include additional information about the union
    # member index used. The code modified below is based on salsify/avro,
    # branch 'salsify-master' with the tag 'v1.9.0.3'
    class DatumReader < Avro::IO::DatumReader

      def read_data(writers_schema, readers_schema, decoder)
        # schema resolution: reader's schema is a union, writer's schema is not
        return super unless writers_schema.type_sym != :union && readers_schema.type_sym == :union

        rs_index = readers_schema.schemas.find_index do |s|
          self.class.match_schemas(writers_schema, s)
        end

        raise Avro::IO::SchemaMatchException.new(writers_schema, readers_schema) unless rs_index

        datum = read_data(writers_schema, readers_schema.schemas[rs_index], decoder)
        optional = readers_schema.schemas.first.type_sym == :null

        if readers_schema.schemas.size == 2 && optional
          # Avromatic does not treat the union of null and 1 other type as a union
          datum
        elsif datum.nil?
          # Avromatic does not treat the null of an optional field as part of the union
          nil
        else
          # Avromatic does not treat the null of an optional field as part of the union so
          # adjust the member index accordingly
          member_index = optional ? rs_index - 1 : rs_index
          Avromatic::IO::UnionDatum.new(member_index, datum)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
avromatic-5.1.1 lib/avromatic/io/datum_reader.rb
avromatic-5.1.0 lib/avromatic/io/datum_reader.rb
avromatic-5.0.0 lib/avromatic/io/datum_reader.rb
avromatic-4.3.0 lib/avromatic/io/datum_reader.rb
avromatic-4.2.0 lib/avromatic/io/datum_reader.rb
avromatic-4.1.1 lib/avromatic/io/datum_reader.rb
avromatic-4.1.0 lib/avromatic/io/datum_reader.rb
avromatic-4.0.0 lib/avromatic/io/datum_reader.rb
avromatic-3.0.2 lib/avromatic/io/datum_reader.rb
avromatic-3.0.1 lib/avromatic/io/datum_reader.rb
avromatic-3.0.0 lib/avromatic/io/datum_reader.rb
avromatic-2.4.0 lib/avromatic/io/datum_reader.rb
avromatic-2.3.0 lib/avromatic/io/datum_reader.rb