Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(531)

Unified Diff: third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java

Issue 2599263002: third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Address comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
diff --git a/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
index 39213c4d1670adb97676bfa4155a6298ac8c81fb..733ccfbc7dbc21123c350b75eca79d763fd9d86c 100644
--- a/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
+++ b/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
@@ -41,6 +41,8 @@ import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
@@ -164,8 +166,21 @@ public class RubyMessage extends RubyObject {
*/
@JRubyMethod
public IRubyObject hash(ThreadContext context) {
- int hashCode = System.identityHashCode(this);
- return context.runtime.newFixnum(hashCode);
+ try {
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
+ for (RubyMap map : maps.values()) {
+ digest.update((byte) map.hashCode());
+ }
+ for (RubyRepeatedField repeatedField : repeatedFields.values()) {
+ digest.update((byte) repeatedFields.hashCode());
+ }
+ for (IRubyObject field : fields.values()) {
+ digest.update((byte) field.hashCode());
+ }
+ return context.runtime.newString(new ByteList(digest.digest()));
+ } catch (NoSuchAlgorithmException ignore) {
+ return context.runtime.newFixnum(System.identityHashCode(this));
+ }
}
/*
@@ -504,7 +519,7 @@ public class RubyMessage extends RubyObject {
break;
case BYTES:
case STRING:
- Utils.validateStringEncoding(context.runtime, fieldDescriptor.getType(), value);
+ Utils.validateStringEncoding(context, fieldDescriptor.getType(), value);
RubyString str = (RubyString) value;
switch (fieldDescriptor.getType()) {
case BYTES:
@@ -592,13 +607,17 @@ public class RubyMessage extends RubyObject {
protected IRubyObject getField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) {
Descriptors.OneofDescriptor oneofDescriptor = fieldDescriptor.getContainingOneof();
if (oneofDescriptor != null) {
- if (oneofCases.containsKey(oneofDescriptor)) {
- if (oneofCases.get(oneofDescriptor) != fieldDescriptor)
- return context.runtime.getNil();
+ if (oneofCases.get(oneofDescriptor) == fieldDescriptor) {
return fields.get(fieldDescriptor);
} else {
Descriptors.FieldDescriptor oneofCase = builder.getOneofFieldDescriptor(oneofDescriptor);
- if (oneofCase != fieldDescriptor) return context.runtime.getNil();
+ if (oneofCase != fieldDescriptor) {
+ if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) {
+ return context.runtime.getNil();
+ } else {
+ return wrapField(context, fieldDescriptor, fieldDescriptor.getDefaultValue());
+ }
+ }
IRubyObject value = wrapField(context, oneofCase, builder.getField(oneofCase));
fields.put(fieldDescriptor, value);
return value;
@@ -691,7 +710,7 @@ public class RubyMessage extends RubyObject {
}
}
if (addValue) {
- Utils.checkType(context, fieldType, value, (RubyModule) typeClass);
+ value = Utils.checkType(context, fieldType, value, (RubyModule) typeClass);
this.fields.put(fieldDescriptor, value);
} else {
this.fields.remove(fieldDescriptor);

Powered by Google App Engine
This is Rietveld 408576698