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 912c15b AVRO-2677: Ruby parse/serialize bytes decimal logical type
(#761)
912c15b is described below
commit 912c15b36caacdea5cf3a6753316022156681470
Author: Sergey Toy <[email protected]>
AuthorDate: Sat Jan 11 20:26:42 2020 +0800
AVRO-2677: Ruby parse/serialize bytes decimal logical type (#761)
* add BytesDecimalSchema class
* remove unnecessary comment
* add tests for decimal schema
* refactor bytes decimal `to_avro`
* refactor BytesDecimalSchema to BytesSchema to works with all bytes schema
and not only for decimal
---
lang/ruby/lib/avro/schema.rb | 28 +++++++++++++++++++++++--
lang/ruby/test/test_schema.rb | 49 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 2 deletions(-)
diff --git a/lang/ruby/lib/avro/schema.rb b/lang/ruby/lib/avro/schema.rb
index f95fd61..24b6329 100644
--- a/lang/ruby/lib/avro/schema.rb
+++ b/lang/ruby/lib/avro/schema.rb
@@ -55,8 +55,14 @@ module Avro
type_sym = type.to_sym
if PRIMITIVE_TYPES_SYM.include?(type_sym)
- return PrimitiveSchema.new(type_sym, logical_type)
-
+ case type_sym
+ when :bytes
+ precision = json_obj['precision']
+ scale = json_obj['scale']
+ return BytesSchema.new(type_sym, logical_type, precision, scale)
+ else
+ return PrimitiveSchema.new(type_sym, logical_type)
+ end
elsif NAMED_TYPES_SYM.include? type_sym
name = json_obj['name']
if !Avro.disable_schema_name_validation && name !~ NAME_REGEX
@@ -396,6 +402,24 @@ module Avro
end
end
+ class BytesSchema < PrimitiveSchema
+ attr_reader :precision, :scale
+ def initialize(type, logical_type=nil, precision=nil, scale=nil)
+ super(type.to_sym, logical_type)
+ @precision = precision
+ @scale = scale
+ end
+
+ def to_avro(names=nil)
+ avro = super
+ return avro if avro.is_a?(String)
+
+ avro['precision'] = precision if precision
+ avro['scale'] = scale if scale
+ avro
+ end
+ end
+
class FixedSchema < NamedSchema
attr_reader :size
def initialize(name, space, size, names=nil, logical_type=nil)
diff --git a/lang/ruby/test/test_schema.rb b/lang/ruby/test/test_schema.rb
index 32d042f..e16765e 100644
--- a/lang/ruby/test/test_schema.rb
+++ b/lang/ruby/test/test_schema.rb
@@ -492,4 +492,53 @@ class TestSchema < Test::Unit::TestCase
assert_equal('Error validating default for veggies: at . expected type
null, got string with value "apple"',
exception.to_s)
end
+
+ def test_bytes_decimal_to_include_precision_scale
+ schema = Avro::Schema.parse <<-SCHEMA
+ {
+ "type": "bytes",
+ "logicalType": "decimal",
+ "precision": 9,
+ "scale": 2
+ }
+ SCHEMA
+
+ schema_hash =
+ {
+ 'type' => 'bytes',
+ 'logicalType' => 'decimal',
+ 'precision' => 9,
+ 'scale' => 2
+ }
+
+ assert_equal schema_hash, schema.to_avro
+ end
+
+ def test_bytes_decimal_to_without_precision_scale
+ schema = Avro::Schema.parse <<-SCHEMA
+ {
+ "type": "bytes",
+ "logicalType": "decimal"
+ }
+ SCHEMA
+
+ schema_hash =
+ {
+ 'type' => 'bytes',
+ 'logicalType' => 'decimal'
+ }
+
+ assert_equal schema_hash, schema.to_avro
+ end
+
+ def test_bytes_schema
+ schema = Avro::Schema.parse <<-SCHEMA
+ {
+ "type": "bytes"
+ }
+ SCHEMA
+
+ schema_str = 'bytes'
+ assert_equal schema_str, schema.to_avro
+ end
end