You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by fo...@apache.org on 2020/04/11 06:50:09 UTC

[avro] branch master updated: AVRO-2749: Add ruby support for datum hashes with non-string keys (#819)

This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 01e039c  AVRO-2749: Add ruby support for datum hashes with non-string keys (#819)
01e039c is described below

commit 01e039ca12ade8bc08b6b4b33ef87a32f3f06b88
Author: Justin Lambert <jl...@eml.cc>
AuthorDate: Sat Apr 11 00:50:01 2020 -0600

    AVRO-2749: Add ruby support for datum hashes with non-string keys (#819)
    
    * support datum hashes with non-string keys in ruby
    
    * lint fixes
    
    * datum keys as string or symbol only
    
    * empty commit
    
    * empty commit, tests
    
    * empty commit, tests
---
 lang/ruby/lib/avro/io.rb  |  2 +-
 lang/ruby/test/test_io.rb | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/lang/ruby/lib/avro/io.rb b/lang/ruby/lib/avro/io.rb
index 9a1863e..406fac4 100644
--- a/lang/ruby/lib/avro/io.rb
+++ b/lang/ruby/lib/avro/io.rb
@@ -591,7 +591,7 @@ module Avro
       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
diff --git a/lang/ruby/test/test_io.rb b/lang/ruby/test/test_io.rb
index 7158055..ca724b7 100644
--- a/lang/ruby/test/test_io.rb
+++ b/lang/ruby/test/test_io.rb
@@ -161,6 +161,17 @@ EOS
     check_default(fixed_schema, '"a"', "a")
   end
 
+  def test_record_variable_key_types
+    datum = { sym: "foo", "str"=>"bar"}
+    ret_val = { "sym"=> "foo", "str"=>"bar"}
+    schema = Schema.parse('{"type":"record", "name":"rec", "fields":[{"name":"sym", "type":"string"}, {"name":"str", "type":"string"}]}')
+
+    writer, _encoder, _datum_writer = write_datum(datum, schema)
+
+    ret_datum = read_datum(writer, schema)
+    assert_equal ret_datum, ret_val
+  end
+
   def test_record_with_nil
     schema = Avro::Schema.parse('{"type":"record", "name":"rec", "fields":[{"type":"int", "name":"i"}]}')
     assert_raise(Avro::IO::AvroTypeError) do