diff options
Diffstat (limited to 'ruby')
-rw-r--r-- | ruby/Gemfile.lock | 6 | ||||
-rw-r--r-- | ruby/Rakefile | 11 | ||||
-rw-r--r-- | ruby/ext/google/protobuf_c/message.c | 8 | ||||
-rw-r--r-- | ruby/google-protobuf.gemspec | 11 | ||||
-rw-r--r-- | ruby/lib/google/protobuf.rb | 6 | ||||
-rw-r--r-- | ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java | 16 | ||||
-rw-r--r-- | ruby/tests/basic.rb | 29 |
7 files changed, 73 insertions, 14 deletions
diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock index 8599da75..27e57506 100644 --- a/ruby/Gemfile.lock +++ b/ruby/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - google-protobuf (3.0.0.alpha.4.0) + google-protobuf (3.0.0.alpha.5.0) GEM remote: https://rubygems.org/ @@ -10,6 +10,7 @@ GEM rake (10.4.2) rake-compiler (0.9.5) rake + rake-compiler-dock (0.5.1) rubygems-tasks (0.2.4) test-unit (3.0.9) power_assert @@ -21,8 +22,9 @@ PLATFORMS DEPENDENCIES google-protobuf! rake-compiler + rake-compiler-dock rubygems-tasks test-unit BUNDLED WITH - 1.10.6 + 1.11.2 diff --git a/ruby/Rakefile b/ruby/Rakefile index c25103d8..81c3119e 100644 --- a/ruby/Rakefile +++ b/ruby/Rakefile @@ -20,6 +20,17 @@ else Rake::ExtensionTask.new("protobuf_c", spec) do |ext| ext.ext_dir = "ext/google/protobuf_c" ext.lib_dir = "lib/google" + ext.cross_compile = true + ext.cross_platform = [ + 'x86-mingw32', 'x64-mingw32', + 'x86_64-linux', 'x86-linux', + 'universal-darwin' + ] + end + + task 'gem:windows' do + require 'rake_compiler_dock' + RakeCompilerDock.sh "bundle && rake cross native gem RUBY_CC_VERSION=2.3.0:2.2.2:2.1.6" end end diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c index ebe2f1ab..283939c9 100644 --- a/ruby/ext/google/protobuf_c/message.c +++ b/ruby/ext/google/protobuf_c/message.c @@ -166,7 +166,7 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) { name, name_len); if (f == NULL) { - rb_raise(rb_eArgError, "Unknown field"); + return rb_call_super(argc, argv); } if (setter) { @@ -197,7 +197,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { f = upb_msgdef_ntofz(self->descriptor->msgdef, name); if (f == NULL) { rb_raise(rb_eArgError, - "Unknown field name in initialization map entry."); + "Unknown field name '%s' in initialization map entry.", name); } if (is_map_field(f)) { @@ -205,7 +205,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { if (TYPE(val) != T_HASH) { rb_raise(rb_eArgError, - "Expected Hash object as initializer value for map field."); + "Expected Hash object as initializer value for map field '%s'.", name); } map = layout_get(self->descriptor->layout, Message_data(self), f); Map_merge_into_self(map, val); @@ -214,7 +214,7 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { if (TYPE(val) != T_ARRAY) { rb_raise(rb_eArgError, - "Expected array as initializer value for repeated field."); + "Expected array as initializer value for repeated field '%s'.", name); } ary = layout_get(self->descriptor->layout, Message_data(self), f); for (int i = 0; i < RARRAY_LEN(val); i++) { diff --git a/ruby/google-protobuf.gemspec b/ruby/google-protobuf.gemspec index 312a0c8c..7b64ee77 100644 --- a/ruby/google-protobuf.gemspec +++ b/ruby/google-protobuf.gemspec @@ -8,12 +8,13 @@ Gem::Specification.new do |s| s.authors = ["Protobuf Authors"] s.email = "protobuf@googlegroups.com" s.require_paths = ["lib"] - s.files = `git ls-files -z`.split("\x0").find_all{|f| f =~ /lib\/.+\.rb/} - unless RUBY_PLATFORM == "java" - s.files += `git ls-files "*.c" "*.h" extconf.rb Makefile`.split - s.extensions= ["ext/google/protobuf_c/extconf.rb"] - else + s.files = Dir.glob('lib/**/*.rb') + if RUBY_PLATFORM == "java" s.files += ["lib/google/protobuf_java.jar"] + else + s.files += Dir.glob('ext/**/*') + s.extensions= ["ext/google/protobuf_c/extconf.rb"] + s.add_development_dependency "rake-compiler-dock" end s.test_files = ["tests/basic.rb", "tests/stress.rb", diff --git a/ruby/lib/google/protobuf.rb b/ruby/lib/google/protobuf.rb index f0eb6268..62bdd1bf 100644 --- a/ruby/lib/google/protobuf.rb +++ b/ruby/lib/google/protobuf.rb @@ -44,7 +44,11 @@ if RUBY_PLATFORM == "java" require 'json' require 'google/protobuf_java' else - require 'google/protobuf_c' + begin + require "google/#{RUBY_VERSION.sub(/\.\d$/, '')}/protobuf_c" + rescue LoadError + require 'google/protobuf_c' + end end require 'google/protobuf/repeated_field' diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java index 547ab22c..39213c4d 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java @@ -86,14 +86,14 @@ public class RubyMessage extends RubyObject { if (Utils.isMapEntry(fieldDescriptor)) { if (!(value instanceof RubyHash)) - throw runtime.newArgumentError("Expected Hash object as initializer value for map field."); + throw runtime.newArgumentError("Expected Hash object as initializer value for map field '" + key.asJavaString() + "'."); final RubyMap map = newMapForField(context, fieldDescriptor); map.mergeIntoSelf(context, value); maps.put(fieldDescriptor, map); } else if (fieldDescriptor.isRepeated()) { if (!(value instanceof RubyArray)) - throw runtime.newTypeError("Expected array as initializer var for repeated field."); + throw runtime.newArgumentError("Expected array as initializer value for repeated field '" + key.asJavaString() + "'."); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, value); addRepeatedField(fieldDescriptor, repeatedField); } else { @@ -217,6 +217,9 @@ public class RubyMessage extends RubyObject { RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]); if (oneofDescriptor.isNil()) { + if (!hasField(args[0])) { + return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); + } return index(context, args[0]); } RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor; @@ -233,6 +236,10 @@ public class RubyMessage extends RubyObject { if (field.end_with_p(context, equalSign).isTrue()) { field.chomp_bang(context, equalSign); } + + if (!hasField(field)) { + return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); + } return indexSet(context, field, args[1]); } } @@ -435,6 +442,11 @@ public class RubyMessage extends RubyObject { return ret; } + private boolean hasField(IRubyObject fieldName) { + String nameStr = fieldName.asJavaString(); + return this.descriptor.findFieldByName(Utils.escapeIdentifier(nameStr)) != null; + } + private void checkRepeatedFieldType(ThreadContext context, IRubyObject value, Descriptors.FieldDescriptor fieldDescriptor) { Ruby runtime = context.runtime; diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index 40c20078..da85520f 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -191,6 +191,35 @@ module BasicTest assert m1.hash != m2.hash end + def test_unknown_field_errors + e = assert_raise NoMethodError do + TestMessage.new.hello + end + assert_match(/hello/, e.message) + + e = assert_raise NoMethodError do + TestMessage.new.hello = "world" + end + assert_match(/hello/, e.message) + end + + def test_initialization_map_errors + e = assert_raise ArgumentError do + TestMessage.new(:hello => "world") + end + assert_match(/hello/, e.message) + + e = assert_raise ArgumentError do + MapMessage.new(:map_string_int32 => "hello") + end + assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32'." + + e = assert_raise ArgumentError do + TestMessage.new(:repeated_uint32 => "hello") + end + assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32'." + end + def test_type_errors m = TestMessage.new assert_raise TypeError do |