aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby/src
diff options
context:
space:
mode:
authorGravatar Anders Carling <lowe@lowe.nu>2015-11-20 21:55:13 +0100
committerGravatar Anders Carling <lowe@lowe.nu>2015-11-20 21:55:18 +0100
commit0df1e398eb6022602b2909acdfe06c668ae6a8a2 (patch)
treecff1498bc83bb9222cd3dbe33d1ffa8e6afd5851 /ruby/src
parent956a770adc772184e5911f964b866028465d1727 (diff)
Raise NoMethodError for unknown fields
More informative and more ruby-like
Diffstat (limited to 'ruby/src')
-rw-r--r--ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java12
1 files changed, 12 insertions, 0 deletions
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..e313518d 100644
--- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
+++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
@@ -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])) {
+ throw context.runtime.newNoMethodError("undefined method `" + args[0].toString() + "' for " + metaClass.toString(), args[0].asJavaString(), metaClass);
+ }
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)) {
+ throw context.runtime.newNoMethodError("undefined method `" + args[0].asJavaString() + "' for " + metaClass.toString(), args[0].asJavaString(), metaClass);
+ }
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;