lib/avro/io.rb in avro-1.9.2 vs lib/avro/io.rb in avro-1.10.0

- old
+ new

@@ -170,11 +170,11 @@ def initialize(writer) @writer = writer end # null is written as zero bytes - def write_null(datum) + def write_null(_datum) nil end # a boolean is written as a single byte # whose value is either 0 (false) or 1 (true). @@ -290,24 +290,24 @@ end readers_schema.type_adapter.decode(datum) end - def read_fixed(writers_schema, readers_schema, decoder) + def read_fixed(writers_schema, _readers_schema, decoder) decoder.read(writers_schema.size) end def read_enum(writers_schema, readers_schema, decoder) index_of_symbol = decoder.read_int read_symbol = writers_schema.symbols[index_of_symbol] - # TODO(jmhodges): figure out what unset means for resolution - # schema resolution - unless readers_schema.symbols.include?(read_symbol) - # 'unset' here + if !readers_schema.symbols.include?(read_symbol) && readers_schema.default + read_symbol = readers_schema.default end + # This implementation deviates from the spec by always returning + # a symbol. read_symbol end def read_array(writers_schema, readers_schema, decoder) read_items = [] @@ -357,30 +357,32 @@ def read_record(writers_schema, readers_schema, decoder) readers_fields_hash = readers_schema.fields_hash read_record = {} writers_schema.fields.each do |field| - if readers_field = readers_fields_hash[field.name] + readers_field = readers_fields_hash[field.name] + if readers_field field_val = read_data(field.type, readers_field.type, decoder) read_record[field.name] = field_val + elsif readers_schema.fields_by_alias.key?(field.name) + readers_field = readers_schema.fields_by_alias[field.name] + field_val = read_data(field.type, readers_field.type, decoder) + read_record[readers_field.name] = field_val else skip_data(field.type, decoder) end end # fill in the default values - if readers_fields_hash.size > read_record.size - writers_fields_hash = writers_schema.fields_hash - readers_fields_hash.each do |field_name, field| - unless writers_fields_hash.has_key? field_name - if field.default? - field_val = read_default_value(field.type, field.default) - read_record[field.name] = field_val - else - raise AvroError, "Missing data for #{field.type} with no default" - end - end + readers_fields_hash.each do |field_name, field| + next if read_record.key?(field_name) + + if field.default? + field_val = read_default_value(field.type, field.default) + read_record[field.name] = field_val + else + raise AvroError, "Missing data for #{field.type} with no default" end end read_record end @@ -466,11 +468,11 @@ def skip_fixed(writers_schema, decoder) decoder.skip(writers_schema.size) end - def skip_enum(writers_schema, decoder) + def skip_enum(_writers_schema, decoder) decoder.skip_int end def skip_union(writers_schema, decoder) index = decoder.read_long @@ -543,11 +545,11 @@ else raise AvroError.new("Unknown type: #{writers_schema.type}") end end - def write_fixed(writers_schema, datum, encoder) + def write_fixed(_writers_schema, datum, encoder) encoder.write(datum) end def write_enum(writers_schema, datum, encoder) index_of_datum = writers_schema.symbols.index(datum) @@ -589,10 +591,10 @@ end def write_record(writers_schema, datum, encoder) raise AvroTypeError.new(writers_schema, datum) unless datum.is_a?(Hash) writers_schema.fields.each do |field| - write_data(field.type, datum[field.name], encoder) + write_data(field.type, datum.key?(field.name) ? datum[field.name] : datum[field.name.to_sym], encoder) end end end # DatumWriter end end