/** Recursive helper implementing {@link #findMissingFields(Message)}. */ private static void findMissingFields( final MessageOrBuilder message, final String prefix, final List<String> results) { for (final FieldDescriptor field : message.getDescriptorForType().getFields()) { if (field.isRequired() && !message.hasField(field)) { results.add(prefix + field.getName()); } } for (final Map.Entry<FieldDescriptor, Object> entry : message.getAllFields().entrySet()) { final FieldDescriptor field = entry.getKey(); final Object value = entry.getValue(); if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { int i = 0; for (final Object element : (List) value) { findMissingFields( (MessageOrBuilder) element, subMessagePrefix(prefix, field, i++), results); } } else { if (message.hasField(field)) { findMissingFields( (MessageOrBuilder) value, subMessagePrefix(prefix, field, -1), results); } } } } }
/** * Assigns all fields in {@link JsonNode} instance to a {@link Builder}. * * @param builder the builder to be populated * @param root the JSON object * @return the original builder, populated with any fields that were discovered */ public static Builder toProto(final Builder builder, final JsonNode root) { Descriptor type = builder.getDescriptorForType(); for (final FieldDescriptor fieldDesc : type.getFields()) { final String attrName = fieldDesc.getName(); final JsonNode node = root.get(attrName); if (node == null) { continue; } if (node.isNull()) { continue; } if (fieldDesc.isRepeated()) { final Iterator<JsonNode> iter = node.getElements(); while (iter.hasNext()) { builder.addRepeatedField(fieldDesc, toJavaObj(builder, fieldDesc, iter.next())); } } else { builder.setField(fieldDesc, toJavaObj(builder, fieldDesc, node)); } } return builder; }
private void printSingleField(final FieldDescriptor field, final Object value, final TextGenerator generator) throws IOException { if (field.isExtension()) { generator.print("["); // We special-case MessageSet elements for compatibility with proto1. if (field.getContainingType().getOptions().getMessageSetWireFormat() && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional()) // object equality && (field.getExtensionScope() == field.getMessageType())) { generator.print(field.getMessageType().getFullName()); } else { generator.print(field.getFullName()); } generator.print("]"); } else { if (field.getType() == FieldDescriptor.Type.GROUP) { // Groups must be serialized with their original capitalization. generator.print(field.getMessageType().getName()); } else { generator.print(field.getName()); } } if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (singleLineMode) { generator.print(" { "); } else { generator.print(" {\n"); generator.indent(); } } else { generator.print(": "); } printFieldValue(field, value, generator); if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (singleLineMode) { generator.print("} "); } else { generator.outdent(); generator.print("}\n"); } } else { if (singleLineMode) { generator.print(" "); } else { generator.print("\n"); } } }
private static String subMessagePrefix( final String prefix, final FieldDescriptor field, final int index) { final StringBuilder result = new StringBuilder(prefix); if (field.isExtension()) { result.append('(').append(field.getFullName()).append(')'); } else { result.append(field.getName()); } if (index != -1) { result.append('[').append(index).append(']'); } result.append('.'); return result.toString(); }
private static void getRepeatedFieldNamesInternal(Descriptor d, Map<String, Set<String>> result) { Set<String> repeatedFields = Sets.newHashSet(); for (FieldDescriptor fd : d.getFields()) { if (fd.isRepeated()) { repeatedFields.add(javaCase(fd.getName())); } if (fd.getType() == FieldDescriptor.Type.MESSAGE) { getRepeatedFieldNamesInternal(fd.getMessageType(), result); } } if (!repeatedFields.isEmpty()) { result.put(javaCase(d.getName()), repeatedFields); } }
private static RequiredFieldList evenFields(List<FieldDescriptor> protoFields) { RequiredFieldList reqList = new RequiredFieldList(); int i = 0; for (FieldDescriptor fd : protoFields) { if (i % 2 == 0) { RequiredField field = new RequiredField(); field.setAlias(fd.getName()); field.setIndex(i); // field.setType() type is not used reqList.add(field); } i++; } return reqList; }
/** * Returns the CAP element name given a proto FieldDescriptor. * * <p>This method takes care of differences in casing between CAP and proto. * * @param fd the field descriptor for which the name is returned * @return the element name of the field */ public static String getElementName(FieldDescriptor fd) { return javaCase(fd.getName()); }