Skip to content

Commit

Permalink
Merge pull request #8254 from jufemaiz/feature/ruby-better-from_-supp…
Browse files Browse the repository at this point in the history
…ort-well_known_types

Improvement: better ruby from_* support in well_known_types.rb
  • Loading branch information
deannagarcia committed Oct 27, 2021
2 parents 65852d6 + bbdcea8 commit 85c4bc4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
17 changes: 11 additions & 6 deletions ruby/lib/google/protobuf/well_known_types.rb
Expand Up @@ -82,16 +82,16 @@ def to_time
end
end

def self.from_time(time)
new.from_time(time)
end

def from_time(time)
self.seconds = time.to_i
self.nanos = time.nsec
self
end

def self.from_time(time)
new.from_time(time)
end

def to_i
self.seconds
end
Expand Down Expand Up @@ -137,10 +137,14 @@ def to_ruby(recursive = false)
end
end

def self.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
Expand All @@ -160,6 +164,8 @@ def from_ruby(value)
else
raise UnexpectedStructType
end

self
end
end

Expand Down Expand Up @@ -230,6 +236,5 @@ def self.from_a(arr)
ret
end
end

end
end
43 changes: 41 additions & 2 deletions ruby/tests/well_known_types_test.rb
Expand Up @@ -15,18 +15,28 @@ def test_timestamp

# millisecond accuracy
time = Time.at(123456, 654321)
ts = Google::Protobuf::Timestamp.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))
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 654321321, ts.nanos
assert_equal time, ts.to_time

# Instance method returns the same value as class method
# 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
Expand Down Expand Up @@ -205,4 +215,33 @@ def test_b8325
)
assert_equal '[<Google::Protobuf::Value: string_value: "Hello">]', value_field.get(proto).inspect
end

def test_from_ruby
pb = Google::Protobuf::Value.from_ruby(nil)
assert_equal pb.null_value, :NULL_VALUE

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], '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], '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.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3])

pb = Google::Protobuf::Value.from_ruby([1, 2, 3])
assert_equal pb.list_value, Google::Protobuf::ListValue.from_a([1, 2, 3])
end
end

0 comments on commit 85c4bc4

Please sign in to comment.