Java Code Examples for com.google.protobuf.DescriptorProtos#FieldDescriptorProto

The following examples show how to use com.google.protobuf.DescriptorProtos#FieldDescriptorProto . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: metastore   File: ShadowApply.java    License: Apache License 2.0 6 votes vote down vote up
private void applyFieldResults(
    MessageResult messageResult,
    Descriptors.Descriptor messageDescriptor,
    DescriptorProtos.DescriptorProto.Builder newDescriptorProtoBuilder) {
  HashMap<Integer, Integer> fieldNumberToIndexMap = getFieldNumberToIndexMap(messageDescriptor);
  for (FieldResult fieldResult : messageResult.getFieldResultsList()) {
    if (fieldResult.getOptionChangeCount() > 0) {
      Descriptors.FieldDescriptor fieldDescriptor =
          messageDescriptor.findFieldByNumber(fieldResult.getNumber());
      DescriptorProtos.FieldDescriptorProto newFieldDescriptorProto =
          applyFieldOptionChanges(fieldDescriptor, fieldResult.getOptionChangeList());
      newDescriptorProtoBuilder.setField(
          fieldNumberToIndexMap.get(fieldResult.getNumber()), newFieldDescriptorProto);
    }
  }
}
 
Example 2
Source Project: rejoiner   File: DescriptorSet.java    License: Apache License 2.0 6 votes vote down vote up
private static Optional<String> append(
    DescriptorProtos.DescriptorProto messageDescriptor, List<Integer> path, String fullName) {
  switch (path.get(0)) {
    case DescriptorProtos.DescriptorProto.NESTED_TYPE_FIELD_NUMBER:
      DescriptorProtos.DescriptorProto nestedMessage =
          messageDescriptor.getNestedType(path.get(1));
      return appendMessage(nestedMessage, path, fullName);
    case DescriptorProtos.DescriptorProto.ENUM_TYPE_FIELD_NUMBER:
      DescriptorProtos.EnumDescriptorProto enumDescriptor =
          messageDescriptor.getEnumType(path.get(1));
      return Optional.of(appendEnum(enumDescriptor, path, fullName));
    case DescriptorProtos.DescriptorProto.FIELD_FIELD_NUMBER:
      DescriptorProtos.FieldDescriptorProto fieldDescriptor =
          messageDescriptor.getField(path.get(1));
      return Optional.of(fullName + appendNameComponent(fieldDescriptor.getName()));
    default:
      return Optional.empty();
  }
}
 
Example 3
Source Project: xresloader   File: DataVerifyPbMsgField.java    License: MIT License 6 votes vote down vote up
public DataVerifyPbMsgField(DescriptorProtos.DescriptorProto desc) {
    super(desc.getName());

    for (DescriptorProtos.FieldDescriptorProto fd : desc.getFieldList()) {
        all_names.put(fd.getName(), (long) fd.getNumber());
        all_numbers.add((long) fd.getNumber());

        // alias extension
        if (fd.getOptions().hasExtension(Xresloader.fieldAlias)) {
            String alias_name = fd.getOptions().getExtension(Xresloader.fieldAlias);
            if (!alias_name.isEmpty()) {
                all_names.put(alias_name, (long) fd.getNumber());
            }
        }
    }
}
 
Example 4
@Test
public void extensionTest() throws Exception {
  DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
      DescriptorProtos.FileDescriptorProto.newBuilder()
          .setName("test")
          .setSyntax("proto3")
          .addDependency("google/protobuf/descriptor.proto");

  DescriptorProtos.FieldDescriptorProto extensionField =
      DescriptorProtos.FieldDescriptorProto.newBuilder()
          .setName("test_extension")
          .setNumber(66666700)
          .setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING)
          .setExtendee(".google.protobuf.FileOptions")
          .build();

  fileDescriptorProtoBuilder.addExtension(extensionField);

  Descriptors.FileDescriptor[] dependencies = new Descriptors.FileDescriptor[1];
  dependencies[0] = DescriptorProtos.getDescriptor();
  Descriptors.FileDescriptor fileDescriptor =
      Descriptors.FileDescriptor.buildFrom(fileDescriptorProtoBuilder.build(), dependencies);

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  ProtoLanguageFileWriter.write(fileDescriptor, outputStream);

  String expected =
      "syntax = \"proto3\";\n"
          + "\n"
          + "import \"google/protobuf/descriptor.proto\";\n"
          + "\n"
          + "\n"
          + "\n"
          + "extend google.protobuf.FileOptions {\n"
          + "\tstring test_extension = 66666700;\n"
          + "}\n"
          + "\n";
  Assert.assertEquals(expected, outputStream.toString());
}
 
Example 5
Source Project: metastore   File: ShadowApply.java    License: Apache License 2.0 5 votes vote down vote up
private DescriptorProtos.FieldDescriptorProto applyFieldOptionChanges(
    Descriptors.FieldDescriptor fieldDescriptor, List<OptionChangeInfo> optionChanges) {
  DescriptorProtos.FieldDescriptorProto.Builder newFieldDescriptorProtoBuilder =
      fieldDescriptor.toProto().toBuilder();
  UnknownFieldSet unknownFieldSet = buildUnknownFieldSet(optionChanges);

  DescriptorProtos.FieldOptions fieldOptions =
      DescriptorProtos.FieldOptions.newBuilder().setUnknownFields(unknownFieldSet).build();

  return newFieldDescriptorProtoBuilder.setOptions(fieldOptions).build();
}
 
Example 6
Source Project: beast   File: ProtoField.java    License: Apache License 2.0 5 votes vote down vote up
public ProtoField(DescriptorProtos.FieldDescriptorProto f) {
    this.fieldProto = f;
    this.name = f.getName();
    this.type = f.getType();
    this.label = f.getLabel();
    this.index = f.getNumber();
    this.fields = new ArrayList<>();
    this.typeName = f.getTypeName();
}
 
Example 7
/**
 * Add a field to a record type.
 *
 * @param metaDataBuilder the meta-data builder
 * @param recordType the record type to add the field to
 * @param field the field to be added
 */
public static void addField(@Nonnull RecordMetaDataProto.MetaData.Builder metaDataBuilder, @Nonnull String recordType, @Nonnull DescriptorProtos.FieldDescriptorProto field) {
    DescriptorProtos.DescriptorProto.Builder messageType = findMessageTypeByName(metaDataBuilder.getRecordsBuilder(), recordType);
    if (messageType == null) {
        throw new MetaDataException("Record type " + recordType + " does not exist");
    }
    DescriptorProtos.FieldDescriptorProto.Builder fieldBuilder = findFieldByName(messageType, field.getName());
    if (fieldBuilder != null) {
        throw new MetaDataException("Field " + field.getName() + " already exists in record type " + recordType);
    }
    messageType.addField(field);
}
 
Example 8
private static boolean hasField(@Nonnull DescriptorProtos.FileDescriptorProtoOrBuilder file,
                                @Nonnull DescriptorProtos.DescriptorProtoOrBuilder message,
                                @Nonnull DescriptorProtos.DescriptorProtoOrBuilder messageType) {
    for (DescriptorProtos.FieldDescriptorProto field : message.getFieldList()) {
        final String fullTypeName = fullyQualifiedTypeName(file, messageType.getName());
        FieldTypeMatch fieldTypeMatch = fieldIsType(file, message, field, fullTypeName);
        if (fieldTypeMatch.isAmbiguousMatch()) {
            throw new AmbiguousTypeNameException(file.getPackage(), message, field, fullTypeName);
        } else if (FieldTypeMatch.MATCHES.equals(fieldTypeMatch)) {
            return true;
        }
        // Nested matches do not count.
    }
    return false;
}
 
Example 9
@Nonnull
private FieldTypeMatch fieldIsType(@Nonnull DescriptorProtos.FileDescriptorProtoOrBuilder file,
                                   @Nonnull String messageName, @Nonnull String fieldName,
                                   @Nonnull String typeName) {

    final DescriptorProtos.DescriptorProto record = file.getMessageTypeList().stream()
            .filter(message -> message.getName().equals(messageName))
            .findAny()
            .get();
    final DescriptorProtos.FieldDescriptorProto field = record.getFieldList().stream()
            .filter(f -> f.getName().equals(fieldName))
            .findAny()
            .get();
    return MetaDataProtoEditor.fieldIsType(file, record, field, typeName);
}
 
Example 10
private void addField(@Nonnull String recordType, @Nonnull DescriptorProtos.FieldDescriptorProto field) {
    metaDataStore.mutateMetaData((metaDataProto) -> MetaDataProtoEditor.addField(metaDataProto, recordType, field));
}