From ef70acbc03e8dbcf4370ee82b5371f01a4952e39 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 3 Feb 2021 12:34:52 +1100 Subject: [PATCH 1/5] Improvement: better ruby from_* support in well_known_types.rb * Added capability to support from_* requests properly by adding class methods and returning self for instance methods * `Timestamp.from_time` * `Value.from_ruby` --- ruby/lib/google/protobuf/well_known_types.rb | 36 +++++++++++++++++++- ruby/tests/well_known_types_test.rb | 14 ++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb index 37f8d5b675cd..942782026c09 100755 --- a/ruby/lib/google/protobuf/well_known_types.rb +++ b/ruby/lib/google/protobuf/well_known_types.rb @@ -82,9 +82,14 @@ def to_time end end + def self.from_time(time) + Timestamp.new(seconds: time.to_i, nanos: time.nsec) + end + def from_time(time) self.seconds = time.to_i self.nanos = time.nsec + self end def to_i @@ -131,6 +136,34 @@ def to_ruby(recursive = false) raise UnexpectedStructType end end + + def self.from_ruby(value) + ret = Value.new + case value + when NilClass + ret.null_value = 0 + when Numeric + ret.number_value = value + when String + ret.string_value = value + when TrueClass + ret.bool_value = true + when FalseClass + ret.bool_value = false + when Struct + ret.struct_value = value + when Hash + ret.struct_value = Struct.from_hash(value) + when ListValue + ret.list_value = value + when Array + ret.list_value = ListValue.from_a(value) + else + raise UnexpectedStructType + end + + ret + end def from_ruby(value) case value @@ -155,6 +188,8 @@ def from_ruby(value) else raise UnexpectedStructType end + + self end end @@ -225,6 +260,5 @@ def self.from_a(arr) ret end end - end end diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 3eafe095ad0f..9949795250cd 100755 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -15,16 +15,26 @@ def test_timestamp # millisecond accuracy time = Time.at(123456, 654321) - ts.from_time(time) + resp = ts.from_time(time) assert_equal 123456, ts.seconds assert_equal 654321000, ts.nanos assert_equal time, ts.to_time + assert_equal resp, ts # nanosecond accuracy time = Time.at(123456, Rational(654321321, 1000)) - ts.from_time(time) + resp = ts.from_time(time) + assert_equal 123456, ts.seconds assert_equal 654321321, ts.nanos assert_equal time, ts.to_time + assert_equal resp, ts + + # Class based initialisation using from_time + time = Time.at(123456, Rational(654321321, 1000)) + ts = Google::Protobuf::Timestamp.from_time(time) + assert_equal 123456, ts.seconds + assert_equal 654321000, ts.nanos + assert_equal time, ts.to_time end def test_duration From 21478b371e0e71ead9fc1c357c261be7a168b1be Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Tue, 19 Oct 2021 12:25:21 +1100 Subject: [PATCH 2/5] Update well_known_types_test.rb --- ruby/tests/well_known_types_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 5fcc3dbabe62..996dbc30a5a6 100755 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -33,13 +33,13 @@ def test_timestamp time = Time.at(123456, Rational(654321321, 1000)) ts = Google::Protobuf::Timestamp.from_time(time) assert_equal 123456, ts.seconds - assert_equal 654321000, ts.nanos + assert_equal 654321321, ts.nanos assert_equal time, ts.to_time # Instance method returns the same value as class method assert_equal Google::Protobuf::Timestamp.new.from_time(time), Google::Protobuf::Timestamp.from_time(time) -end + end def test_duration duration = Google::Protobuf::Duration.new(seconds: 123, nanos: 456) From 947a51a464ec9ecd3aa7098fe3abfcc4822c1fe5 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Tue, 26 Oct 2021 12:43:57 +1100 Subject: [PATCH 3/5] Test coverage: .from_ruby / #from_ruby --- ruby/lib/google/protobuf/well_known_types.rb | 4 +-- ruby/tests/well_known_types_test.rb | 29 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb index b58e815670e8..f86fe28b77cd 100755 --- a/ruby/lib/google/protobuf/well_known_types.rb +++ b/ruby/lib/google/protobuf/well_known_types.rb @@ -136,7 +136,7 @@ def to_ruby(recursive = false) raise UnexpectedStructType end end - + def self.from_ruby(value) Value.new.from_ruby(value) end @@ -164,7 +164,7 @@ def from_ruby(value) else raise UnexpectedStructType end - + self end end diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 996dbc30a5a6..3e94ca01f3e5 100755 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -215,4 +215,33 @@ def test_b8325 ) assert_equal '[]', value_field.get(proto).inspect end + + def test_from_ruby + pb = Google::Protobuf::Value.from_ruby(nil) + assert_equal pb.null_value, 0 + + pb = Google::Protobuf::Value.from_ruby(1.23) + assert_equal pb.number_value, 1.23 + + pb = Google::Protobuf::Value.from_ruby('1.23') + assert_equal pb.string_value, '1.23' + + pb = Google::Protobuf::Value.from_ruby(true) + assert_equal pb.bool_value, true + + pb = Google::Protobuf::Value.from_ruby(false) + assert_equal pb.bool_value, false + + pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] })) + assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] }) + + pb = Google::Protobuf::Value.from_ruby({ a: 1, b: '2', c: [1, 2, 3] }) + assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] }) + + pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::ListValue.from_a([1, 2, 3])) + assert_equal pb.struct_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) + + pb = Google::Protobuf::Value.from_ruby([1, 2, 3]) + assert_equal pb.struct_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) + end end From 4d94a7cd935a28f2161fcc43b00545bb16021d42 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 27 Oct 2021 22:20:45 +1100 Subject: [PATCH 4/5] Fixed: incorrect tests --- ruby/lib/google/protobuf/well_known_types.rb | 4 ++-- ruby/tests/well_known_types_test.rb | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb index f86fe28b77cd..2d06ca27422a 100755 --- a/ruby/lib/google/protobuf/well_known_types.rb +++ b/ruby/lib/google/protobuf/well_known_types.rb @@ -138,13 +138,13 @@ def to_ruby(recursive = false) end def self.from_ruby(value) - Value.new.from_ruby(value) + self.new.from_ruby(value) end def from_ruby(value) case value when NilClass - self.null_value = 0 + self.null_value = :NULL_VALUE when Numeric self.number_value = value when String diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 3e94ca01f3e5..2f345cd01739 100755 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -1,7 +1,7 @@ #!/usr/bin/ruby require 'test/unit' -require 'google/protobuf/well_known_types' +require 'lib/google/protobuf/well_known_types' class TestWellKnownTypes < Test::Unit::TestCase def test_timestamp @@ -218,7 +218,7 @@ def test_b8325 def test_from_ruby pb = Google::Protobuf::Value.from_ruby(nil) - assert_equal pb.null_value, 0 + assert_equal pb.null_value, :NULL_VALUE pb = Google::Protobuf::Value.from_ruby(1.23) assert_equal pb.number_value, 1.23 @@ -232,16 +232,16 @@ def test_from_ruby pb = Google::Protobuf::Value.from_ruby(false) assert_equal pb.bool_value, false - pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] })) - assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] }) + pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true })) + assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }) - pb = Google::Protobuf::Value.from_ruby({ a: 1, b: '2', c: [1, 2, 3] }) - assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ a: 1, b: '2', c: [1, 2, 3] }) + pb = Google::Protobuf::Value.from_ruby({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }) + assert_equal pb.struct_value, Google::Protobuf::Struct.from_hash({ 'a' => 1, 'b' => '2', 'c' => [1, 2, 3], 'd' => nil, 'e' => true }) pb = Google::Protobuf::Value.from_ruby(Google::Protobuf::ListValue.from_a([1, 2, 3])) - assert_equal pb.struct_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) + assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) pb = Google::Protobuf::Value.from_ruby([1, 2, 3]) - assert_equal pb.struct_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) + assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3]) end end From bbdcea8547e43b2cf2a1851fec6c50c272dcc212 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 28 Oct 2021 07:09:11 +1100 Subject: [PATCH 5/5] Remove lib from require --- ruby/tests/well_known_types_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 2f345cd01739..fbdee38a2969 100755 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -1,7 +1,7 @@ #!/usr/bin/ruby require 'test/unit' -require 'lib/google/protobuf/well_known_types' +require 'google/protobuf/well_known_types' class TestWellKnownTypes < Test::Unit::TestCase def test_timestamp