aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby
diff options
context:
space:
mode:
authorGravatar igorpeshansky <igorpeshansky@users.noreply.github.com>2018-06-19 16:26:41 -0400
committerGravatar Paul Yang <TeBoring@users.noreply.github.com>2018-06-19 13:26:41 -0700
commit944693c44c4cd2de8ed3516c2406c2c1fcec5cbb (patch)
treeb59599dd594f18a838c26d4e461c14b7bb7046e6 /ruby
parent0c3db0269ba1157461fede1d1c52a620660fdf12 (diff)
Add Google::Protobuf::Any.pack convenience class method. (#4719)
Diffstat (limited to 'ruby')
-rw-r--r--ruby/lib/google/protobuf/well_known_types.rb6
-rw-r--r--ruby/tests/well_known_types_test.rb8
2 files changed, 13 insertions, 1 deletions
diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb
index 921ddbc0..3e759591 100644
--- a/ruby/lib/google/protobuf/well_known_types.rb
+++ b/ruby/lib/google/protobuf/well_known_types.rb
@@ -39,6 +39,12 @@ module Google
module Protobuf
Any.class_eval do
+ def self.pack(msg, type_url_prefix='type.googleapis.com/')
+ any = self.new
+ any.pack(msg, type_url_prefix)
+ any
+ end
+
def pack(msg, type_url_prefix='type.googleapis.com/')
if type_url_prefix.empty? or type_url_prefix[-1] != '/' then
self.type_url = "#{type_url_prefix}/#{msg.class.descriptor.name}"
diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb
index bd24c328..240281e7 100644
--- a/ruby/tests/well_known_types_test.rb
+++ b/ruby/tests/well_known_types_test.rb
@@ -120,11 +120,17 @@ class TestWellKnownTypes < Test::Unit::TestCase
end
def test_any
- any = Google::Protobuf::Any.new
ts = Google::Protobuf::Timestamp.new(seconds: 12345, nanos: 6789)
+
+ any = Google::Protobuf::Any.new
any.pack(ts)
assert any.is(Google::Protobuf::Timestamp)
assert_equal ts, any.unpack(Google::Protobuf::Timestamp)
+
+ any = Google::Protobuf::Any.pack(ts)
+
+ assert any.is(Google::Protobuf::Timestamp)
+ assert_equal ts, any.unpack(Google::Protobuf::Timestamp)
end
end