private static void writeMessage(Message message, JsonWriter writer) { MessageType type = MessageType.of(message); for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) { if (fieldDescriptor.isRepeated()) { writer.name(fieldDescriptor.getName()); if (fieldDescriptor.isMapField()) { writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer); } else { writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor)); } } else if (message.hasField(fieldDescriptor)) { writer.name(fieldDescriptor.getName()); Object fieldValue = message.getField(fieldDescriptor); writeFieldValue(fieldDescriptor, fieldValue, writer); } } }
public Builder newBuilderForField(FieldDescriptor field) { verifyContainingType(field); if (field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( "newBuilderForField is only valid for fields with message type."); } return new Builder(field.getMessageType()); }
} else if (fieldDescriptor.isRepeated()) { checkRepeatedFieldType(context, value, fieldDescriptor); if (value instanceof RubyRepeatedField) { Descriptors.OneofDescriptor oneofDescriptor = fieldDescriptor.getContainingOneof(); if (oneofDescriptor != null) { Descriptors.FieldDescriptor oneofCase = oneofCases.get(oneofDescriptor); Descriptors.FieldDescriptor.Type fieldType = fieldDescriptor.getType(); IRubyObject typeClass = context.runtime.getObject(); boolean addValue = true; Descriptors.EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType(); if (Utils.isRubyNum(value)) { Descriptors.EnumValueDescriptor val = enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value)); if (val.getIndex() != -1) value = context.runtime.newSymbol(val.getName());
public BuilderType mergeFrom(final Message other) { if (other.getDescriptorForType() != getDescriptorForType()) { throw new IllegalArgumentException( "mergeFrom(Message) can only merge messages of the same type."); other.getAllFields().entrySet()) { final FieldDescriptor field = entry.getKey(); if (field.isRepeated()) { for (final Object element : (List)entry.getValue()) { addRepeatedField(field, element); } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { final Message existingValue = (Message)getField(field); if (existingValue == existingValue.getDefaultInstanceForType()) { setField(field, entry.getValue()); } else { setField(field, existingValue.newBuilderForType() .mergeFrom(existingValue) .mergeFrom((Message)entry.getValue()) .build());
@Override public Message.Builder newBuilderForField(FieldDescriptor field) { checkFieldDescriptor(field);; // This method should be called for message fields and in a MapEntry // message only the value field can possibly be a message field. if (field.getNumber() != 2 || field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new RuntimeException( "\"" + field.getFullName() + "\" is not a message value field."); } return ((Message) value).newBuilderForType(); }
if (shouldCompare == FieldScopeResult.EXCLUDED_RECURSIVELY) { builder.addSingularField( fieldDescriptor.getNumber(), SingularField.ignored(name(fieldDescriptor))); continue; if (fieldDescriptor.isRepeated()) { if (fieldDescriptor.isMapField()) { Map<Object, Object> actualMap = toProtoMap(actualFields.get(fieldDescriptor)); Map<Object, Object> expectedMap = toProtoMap(expectedFields.get(fieldDescriptor)); Sets.union(actualMap.keySet(), expectedMap.keySet()).immutableCopy(); builder.addAllSingularFields( fieldDescriptor.getNumber(), compareMapFieldsByKey( actualMap, if (ignoreRepeatedFieldOrder) { builder.addRepeatedField( fieldDescriptor.getNumber(), compareRepeatedFieldIgnoringOrder( actualList, } else if (ignoreExtraRepeatedFieldElements && !expectedList.isEmpty()) { builder.addRepeatedField( fieldDescriptor.getNumber(), compareRepeatedFieldExpectingSubsequence( actualList, } else {
private static void writeMessageValue(Message message, JsonWriter writer) { MessageType messageType = MessageType.of(message); if (messageType.doesWrapRepeated) { Descriptors.FieldDescriptor repeatedDescriptor = messageType.fieldDescriptors[0]; if (repeatedDescriptor.isMapField()) { writeMap((Collection<MapEntry>) message.getField(repeatedDescriptor), writer); } else { writeArray(writer, repeatedDescriptor, (Collection) message.getField(repeatedDescriptor)); } } else { writer.beginObject(); writeMessage(message, writer); writer.endObject(); } } }
static FieldNumberTree fromMessage(Message message) { FieldNumberTree tree = new FieldNumberTree(); // Known fields. Map<FieldDescriptor, Object> knownFieldValues = message.getAllFields(); for (FieldDescriptor field : knownFieldValues.keySet()) { Key key = Key.known(field.getNumber()); FieldNumberTree childTree = new FieldNumberTree(); tree.children.put(key, childTree); Object fieldValue = knownFieldValues.get(field); if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { List<?> valueList = (List<?>) fieldValue; for (Object value : valueList) { childTree.merge(fromMessage((Message) value)); } } else { childTree.merge(fromMessage((Message) fieldValue)); } } } // Unknown fields. tree.merge(fromUnknownFieldSet(message.getUnknownFields())); return tree; }
Object answer = null; Map<Descriptors.FieldDescriptor, Object> fieldsMap = ((Message)message).getAllFields(); for (Map.Entry<Descriptors.FieldDescriptor, Object> field : fieldsMap.entrySet()) { if (sameField(fieldName, field.getKey().getName())) { if (field.getKey().isMapField()) {
@SuppressWarnings("unchecked") @Override public Builder<K, V> setField(FieldDescriptor field, Object value) { checkFieldDescriptor(field); if (field.getNumber() == 1) { setKey((K) value); } else { if (field.getType() == FieldDescriptor.Type.ENUM) { value = ((EnumValueDescriptor) value).getNumber(); } else if (field.getType() == FieldDescriptor.Type.MESSAGE) { if (value != null && !metadata.defaultValue.getClass().isInstance(value)) { // The value is not the exact right message type. However, if it // is an alternative implementation of the same type -- e.g. a // DynamicMessage -- we should accept it. In this case we can make // a copy of the message. value = ((Message) metadata.defaultValue).toBuilder().mergeFrom((Message) value).build(); } } setValue((V) value); } return this; }
public static boolean isMapEntry(Descriptors.FieldDescriptor fieldDescriptor) { return fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && fieldDescriptor.isRepeated() && fieldDescriptor.getMessageType().getOptions().getMapEntry(); }
private RubyRepeatedField repeatedFieldForFieldDescriptor(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) { IRubyObject typeClass = context.runtime.getNilClass(); IRubyObject descriptor = getDescriptorForField(context, fieldDescriptor); Descriptors.FieldDescriptor.Type type = fieldDescriptor.getType(); if (type == Descriptors.FieldDescriptor.Type.MESSAGE) { typeClass = ((RubyDescriptor) descriptor).msgclass(context); } else if (type == Descriptors.FieldDescriptor.Type.ENUM) { typeClass = ((RubyEnumDescriptor) descriptor).enummodule(context); } return new RubyRepeatedField(context.runtime, cRepeatedField, type, typeClass); }
private String layoutInspect() { ThreadContext context = getRuntime().getCurrentContext(); StringBuilder sb = new StringBuilder(); for (Descriptors.FieldDescriptor fdef : descriptor.getFields()) { sb.append(Utils.unescapeIdentifier(fdef.getName())); sb.append(": "); sb.append(getField(context, fdef).inspect()); sb.append(", "); } return sb.substring(0, sb.length() - 2); }
protected IRubyObject deepCopy(ThreadContext context) { RubyMessage copy = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { if (fdef.isRepeated()) { copy.addRepeatedField(fdef, this.getRepeatedField(context, fdef).deepCopy(context)); } else if (fields.containsKey(fdef)) { copy.fields.put(fdef, fields.get(fdef)); } else if (this.builder.hasField(fdef)) { copy.fields.put(fdef, wrapField(context, fdef, this.builder.getField(fdef))); } } return copy; }
private IRubyObject getDescriptorForField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) { RubyDescriptor thisRbDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); return thisRbDescriptor.lookup(fieldDescriptor.getName()).getSubType(context); }
private RubyMap newMapForField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) { RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); Descriptors.FieldDescriptor keyField = fieldDescriptor.getMessageType().findFieldByNumber(1); Descriptors.FieldDescriptor valueField = fieldDescriptor.getMessageType().findFieldByNumber(2); IRubyObject keyType = RubySymbol.newSymbol(context.runtime, keyField.getType().name()); IRubyObject valueType = RubySymbol.newSymbol(context.runtime, valueField.getType().name()); if (valueField.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { RubyFieldDescriptor rubyFieldDescriptor = (RubyFieldDescriptor) mapDescriptor.lookup(context, context.runtime.newString("value")); RubyDescriptor rubyDescriptor = (RubyDescriptor) rubyFieldDescriptor.getSubType(context); return (RubyMap) cMap.newInstance(context, keyType, valueType, rubyDescriptor.msgclass(context), Block.NULL_BLOCK); } else { return (RubyMap) cMap.newInstance(context, keyType, valueType, Block.NULL_BLOCK); } }
private RubyRepeatedField rubyToRepeatedField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, IRubyObject value) { RubyArray arr = value.convertToArray(); RubyRepeatedField repeatedField = repeatedFieldForFieldDescriptor(context, fieldDescriptor); RubyClass typeClass = null; if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { RubyDescriptor descriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); typeClass = (RubyClass) descriptor.msgclass(context); } for (int i = 0; i < arr.size(); i++) { IRubyObject row = arr.eltInternal(i); if (row instanceof RubyHash && typeClass != null) { row = (IRubyObject) typeClass.newInstance(context, row, Block.NULL_BLOCK); } repeatedField.push(context, row); } return repeatedField; }
private MessageType(Descriptors.Descriptor descriptor) { this.fieldDescriptors = descriptor.getFields().toArray(new Descriptors.FieldDescriptor[descriptor.getFields().size()]); this.doesWrapRepeated = fieldDescriptors.length == 1 && fieldDescriptors[0].isRepeated() && descriptor.getName().equalsIgnoreCase(fieldDescriptors[0].getName()); }
/** * call-seq: * Message.dup => new_message * Performs a shallow copy of this message and returns the new copy. */ @JRubyMethod public IRubyObject dup(ThreadContext context) { RubyMessage dup = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK); IRubyObject value; for (Descriptors.FieldDescriptor fieldDescriptor : this.descriptor.getFields()) { if (fieldDescriptor.isRepeated()) { dup.addRepeatedField(fieldDescriptor, this.getRepeatedField(context, fieldDescriptor)); } else if (fields.containsKey(fieldDescriptor)) { dup.fields.put(fieldDescriptor, fields.get(fieldDescriptor)); } else if (this.builder.hasField(fieldDescriptor)) { dup.fields.put(fieldDescriptor, wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor))); } } for (Descriptors.FieldDescriptor fieldDescriptor : maps.keySet()) { dup.maps.put(fieldDescriptor, maps.get(fieldDescriptor)); } return dup; }