Class: Avromatic::IO::DatumReader

Inherits:
Avro::IO::DatumReader
  • Object
show all
Defined in:
lib/avromatic/io/datum_reader.rb

Overview

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’

Instance Method Summary collapse

Instance Method Details

#read_data(writers_schema, readers_schema, decoder) ⇒ Object

Raises:

  • (Avro::IO::SchemaMatchException)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/avromatic/io/datum_reader.rb', line 10

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