diff options
author | Josh Haberman <jhaberman@gmail.com> | 2016-08-01 14:31:31 -0700 |
---|---|---|
committer | Josh Haberman <jhaberman@gmail.com> | 2016-08-01 14:34:54 -0700 |
commit | 3cec2ea8d61f5ebebb64af967db782e867da849f (patch) | |
tree | 5a3530bc9d584463560e50c7efba4e24440a4211 /ruby | |
parent | a207a2bd004f7b89699d3e52064dbed34603ae2d (diff) | |
download | protobuf-3cec2ea8d61f5ebebb64af967db782e867da849f.tar.gz protobuf-3cec2ea8d61f5ebebb64af967db782e867da849f.tar.bz2 protobuf-3cec2ea8d61f5ebebb64af967db782e867da849f.zip |
Ruby: added custom Struct exception type and fixed Makefile.am.
Diffstat (limited to 'ruby')
-rw-r--r-- | ruby/lib/google/protobuf/well_known_types.rb | 28 | ||||
-rw-r--r-- | ruby/tests/well_known_types_test.rb | 24 |
2 files changed, 41 insertions, 11 deletions
diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb index d0d15045..547de874 100644 --- a/ruby/lib/google/protobuf/well_known_types.rb +++ b/ruby/lib/google/protobuf/well_known_types.rb @@ -90,6 +90,8 @@ module Google end end + class UnexpectedStructType < Google::Protobuf::Error; end + Value.class_eval do def to_ruby(recursive = false) case self.kind @@ -114,31 +116,32 @@ module Google when :bool_value self.bool_value else - raise "Value not set" + raise UnexpectedStructType end end def from_ruby(value) - if value.nil? + case value + when NilClass self.null_value = 0 - elsif value.is_a?(Numeric) + when Numeric self.number_value = value - elsif value.is_a?(String) + when String self.string_value = value - elsif value.is_a?(TrueClass) + when TrueClass self.bool_value = true - elsif value.is_a?(FalseClass) + when FalseClass self.bool_value = false - elsif value.is_a?(Struct) + when Struct self.struct_value = value - elsif value.is_a?(Hash) + when Hash self.struct_value = Struct.from_hash(value) - elsif value.is_a?(ListValue) + when ListValue self.list_value = value - elsif value.is_a?(Array) + when Array self.list_value = ListValue.from_a(value) else - raise "Unexpected type" + raise UnexpectedStructType end end end @@ -149,6 +152,9 @@ module Google end def []=(key, value) + unless key.is_a?(String) + raise UnexpectedStructType, "Struct keys must be strings." + end self.fields[key] ||= Google::Protobuf::Value.new self.fields[key].from_ruby(value) end diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb index 32e73f55..9b46632b 100644 --- a/ruby/tests/well_known_types_test.rb +++ b/ruby/tests/well_known_types_test.rb @@ -85,6 +85,30 @@ class TestWellKnownTypes < Test::Unit::TestCase assert_equal(should_equal, struct.to_h) assert_equal(should_equal["sublist"].length, struct["sublist"].length) + + assert_raise Google::Protobuf::UnexpectedStructType do + struct[123] = 5 + end + + assert_raise Google::Protobuf::UnexpectedStructType do + struct[5] = Time.new + end + + assert_raise Google::Protobuf::UnexpectedStructType do + struct[5] = [Time.new] + end + + assert_raise Google::Protobuf::UnexpectedStructType do + struct[5] = {123 => 456} + end + + assert_raise Google::Protobuf::UnexpectedStructType do + struct = Google::Protobuf::Struct.new + struct.fields["foo"] = Google::Protobuf::Value.new + # Tries to return a Ruby value for a Value class whose type + # hasn't been filled in. + struct["foo"] + end end def test_any |