com.google.protobuf.DescriptorProtos Java Examples

The following examples show how to use com.google.protobuf.DescriptorProtos. 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 File: MetaDataProtoEditor.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a default union descriptor for the given file descriptor if missing.
 *
 * <p>
 * If the given file descriptor is missing a union message, this method will add one before updating the meta-data.
 * The generated union descriptor is constructed by adding any non-{@code NESTED} types in the file descriptor to the
 * union descriptor from the currently stored meta-data. A new field is not added if a field of the given type already
 * exists, and the order of any existing fields is preserved. Note that types are identified by name, so renaming
 * top-level message types may result in validation errors when trying to update the record descriptor.
 * </p>
 *
 * @param fileDescriptor the file descriptor to create a union for
 * @param baseUnionDescriptor the base union descriptor
 * @return the builder for the union
 */
@Nonnull
public static Descriptors.FileDescriptor addDefaultUnionIfMissing(@Nonnull Descriptors.FileDescriptor fileDescriptor, @Nonnull Descriptors.Descriptor baseUnionDescriptor) {
    if (MetaDataProtoEditor.hasUnion(fileDescriptor)) {
        return fileDescriptor;
    }
    DescriptorProtos.FileDescriptorProto fileDescriptorProto = fileDescriptor.toProto();
    DescriptorProtos.FileDescriptorProto.Builder fileBuilder = fileDescriptorProto.toBuilder();
    DescriptorProtos.DescriptorProto.Builder unionDescriptorBuilder = createSyntheticUnion(fileDescriptor, baseUnionDescriptor);
    for (DescriptorProtos.DescriptorProto.Builder messageType : fileBuilder.getMessageTypeBuilderList()) {
        RecordMetaDataOptionsProto.RecordTypeOptions.Usage messageTypeUsage = getMessageTypeUsage(messageType);
        if (messageTypeUsage != RecordMetaDataOptionsProto.RecordTypeOptions.Usage.NESTED
                && !hasField(fileBuilder, unionDescriptorBuilder, messageType)) {
            addFieldToUnion(unionDescriptorBuilder, fileBuilder, messageType);
        }
    }
    fileBuilder.addMessageType(unionDescriptorBuilder);
    try {
        return Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), fileDescriptor.getDependencies().toArray(new Descriptors.FileDescriptor[0]));
    } catch (Descriptors.DescriptorValidationException e) {
        throw new MetaDataException("Failed to add a default union", e);
    }
}
 
Example #2
Source File: ProtoDiff.java    From metastore with Apache License 2.0 6 votes vote down vote up
private void diffOptionsFromMethod(
    Descriptors.MethodDescriptor descriptorRef, Descriptors.MethodDescriptor descriptorNew) {
  DescriptorProtos.MethodOptions optionsRef = descriptorRef.getOptions();
  DescriptorProtos.MethodOptions optionsNew = descriptorNew.getOptions();
  diffExtensionOptions(
      OptionChangeInfo.OptionType.METHOD_OPTION,
      descriptorRef,
      optionsRef.getAllFields(),
      descriptorNew,
      optionsNew.getAllFields());
  diffUnknownOptions(
      OptionChangeInfo.OptionType.METHOD_OPTION,
      descriptorRef,
      optionsRef.getUnknownFields(),
      descriptorNew,
      optionsNew.getUnknownFields());
}
 
Example #3
Source File: ProtoDomain.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Builder replacePackagePrefixBinary(
    String packagePrefix, Collection<ByteString> updateBytes)
    throws InvalidProtocolBufferException {
  Map<String, DescriptorProtos.FileDescriptorProto> updated = toMap(updateBytes);
  List<String> removing = new ArrayList<>();
  // clear the package prefix
  fileDescriptorMap.forEach(
      (fileName, fdp) -> {
        if (isInPackagePrefix(fdp, packagePrefix)) {
          removing.add(fileName);
        }
      });
  removing.forEach(f -> fileDescriptorMap.remove(f));
  // only add in package
  updated.forEach(
      (fileName, fdp) -> {
        if (isInPackagePrefix(fdp, packagePrefix)) {
          fileDescriptorMap.put(fileName, fdp);
        }
      });
  return this;
}
 
Example #4
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void fieldLabelChanged() {
    FileDescriptor oldFile = TestRecords1Proto.getDescriptor();
    List<DescriptorProtos.FieldDescriptorProto.Label> labels = Arrays.asList(
            DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL,
            DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED,
            DescriptorProtos.FieldDescriptorProto.Label.LABEL_REQUIRED
    );
    for (int i = 0; i < labels.size(); i++) {
        final int itr = i;
        final DescriptorProtos.FieldDescriptorProto.Label label = labels.get(itr);
        final String labelText = label.name().substring(label.name().indexOf('_') + 1).toLowerCase();
        final String errMsg = String.format("%s field is no longer %s", labelText, labelText);
        FileDescriptor updatedFile = mutateField("MySimpleRecord", "str_value_indexed", oldFile,
                field -> field.setLabel(labels.get((itr + 1) % labels.size())));
        assertInvalid(errMsg, oldFile, updatedFile);

        oldFile = updatedFile;
    }
}
 
Example #5
Source File: ProtoDomain.java    From metastore with Apache License 2.0 6 votes vote down vote up
public Builder replacePackageBinary(String packageName, Collection<ByteString> updateBytes)
    throws InvalidProtocolBufferException {
  Map<String, DescriptorProtos.FileDescriptorProto> updated = toMap(updateBytes);
  List<String> removing = new ArrayList<>();
  // clear the package
  fileDescriptorMap.forEach(
      (k, v) -> {
        if (v.getPackage().equals(packageName)) {
          removing.add(k);
        }
      });
  removing.forEach(f -> fileDescriptorMap.remove(f));
  // only add with package prefix
  updated.forEach(
      (fileName, fdp) -> {
        if (fdp.getPackage().equals(packageName)) {
          fileDescriptorMap.put(fileName, fdp);
        }
      });
  return this;
}
 
Example #6
Source File: ProtoDiff.java    From metastore with Apache License 2.0 6 votes vote down vote up
private void diffOptionsFromField(
    Descriptors.FieldDescriptor descriptorRef, Descriptors.FieldDescriptor descriptorNew) {
  DescriptorProtos.FieldOptions optionsRef = descriptorRef.getOptions();
  DescriptorProtos.FieldOptions optionsNew = descriptorNew.getOptions();
  diffExtensionOptions(
      OptionChangeInfo.OptionType.FIELD_OPTION,
      descriptorRef,
      optionsRef.getAllFields(),
      descriptorNew,
      optionsNew.getAllFields());
  diffUnknownOptions(
      OptionChangeInfo.OptionType.FIELD_OPTION,
      descriptorRef,
      optionsRef.getUnknownFields(),
      descriptorNew,
      optionsNew.getUnknownFields());
}
 
Example #7
Source File: ProtoDiff.java    From metastore with Apache License 2.0 6 votes vote down vote up
private void diffOptionsFromFile(
    Descriptors.FileDescriptor descriptorRef, Descriptors.FileDescriptor descriptorNew) {
  DescriptorProtos.FileOptions optionsRef = descriptorRef.getOptions();
  DescriptorProtos.FileOptions optionsNew = descriptorNew.getOptions();
  diffExtensionOptions(
      OptionChangeInfo.OptionType.FILE_OPTION,
      descriptorRef,
      optionsRef.getAllFields(),
      descriptorNew,
      optionsNew.getAllFields());
  diffUnknownOptions(
      OptionChangeInfo.OptionType.FILE_OPTION,
      descriptorRef,
      optionsRef.getUnknownFields(),
      descriptorNew,
      optionsNew.getUnknownFields());
}
 
Example #8
Source File: ConverterTest.java    From beast with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldTestShouldCreateNestedMapping() throws IOException {
    ProtoField protoField = new ProtoField(new ArrayList<ProtoField>() {{
        add(new ProtoField("order_number", 1));
        add(new ProtoField("order_url", "some.type.name", DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE, 2, new ArrayList<ProtoField>() {{
            add(new ProtoField("host", 1));
            add(new ProtoField("url", 2));
        }}));
        add(new ProtoField("order_details", 3));
    }});

    ObjectNode objNode = JsonNodeFactory.instance.objectNode();
    ObjectNode innerObjNode = JsonNodeFactory.instance.objectNode();
    innerObjNode.put("1", "host");
    innerObjNode.put("2", "url");
    innerObjNode.put("record_name", "order_url");
    objNode.put("1", "order_number");
    objNode.put("2", innerObjNode);
    objNode.put("3", "order_details");


    String columnMapping = converter.generateColumnMappings(protoField.getFields());
    String expectedProtoMapping = objectMapper.writeValueAsString(objNode);
    assertEquals(expectedProtoMapping, columnMapping);
}
 
Example #9
Source File: MetaDataProtoEditorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void fieldIsType() {
    final DescriptorProtos.FileDescriptorProto file = TestRecords1Proto.getDescriptor().toProto();
    assertEquals(FieldTypeMatch.MATCHES,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord"));
    assertEquals(FieldTypeMatch.MATCHES,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord"));
    assertEquals(FieldTypeMatch.MATCHES_AS_NESTED,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord.MyNestedRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord.MyNestedRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test2.MySimpleRecord"));
    assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
            fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MyOtherRecord"));
}
 
Example #10
Source File: MutinyGrpcGenerator.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private List<ServiceContext> findServices(List<DescriptorProtos.FileDescriptorProto> protos, ProtoTypeMap typeMap) {
    List<ServiceContext> contexts = new ArrayList<>();

    protos.forEach(fileProto -> {
        for (int serviceNumber = 0; serviceNumber < fileProto.getServiceCount(); serviceNumber++) {
            ServiceContext serviceContext = buildServiceContext(
                    fileProto.getService(serviceNumber),
                    typeMap,
                    fileProto.getSourceCodeInfo().getLocationList(),
                    serviceNumber);
            serviceContext.protoName = fileProto.getName();
            serviceContext.packageName = extractPackageName(fileProto);
            contexts.add(serviceContext);
        }
    });

    return contexts;
}
 
Example #11
Source File: ProtoDiff.java    From metastore with Apache License 2.0 6 votes vote down vote up
private void diffMessageType(
    Descriptors.Descriptor descriptorRef, Descriptors.Descriptor descriptorNew) {
  DescriptorProtos.MessageOptions optionsRef = descriptorRef.getOptions();
  DescriptorProtos.MessageOptions optionsNew = descriptorNew.getOptions();
  diffExtensionOptions(
      OptionChangeInfo.OptionType.MESSAGE_OPTION,
      descriptorRef,
      optionsRef.getAllFields(),
      descriptorNew,
      optionsNew.getAllFields());
  diffUnknownOptions(
      OptionChangeInfo.OptionType.MESSAGE_OPTION,
      descriptorRef,
      optionsRef.getUnknownFields(),
      descriptorNew,
      optionsNew.getUnknownFields());
  diffFields(descriptorRef, descriptorNew);
}
 
Example #12
Source File: ProtoLocation.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
public ProtoLocation(
    DescriptorProtos.SourceCodeInfo.Location location, final ProtoElement element) {
  // Spit out "?:?" for line:column if there's no "span" set in the location.  This can happen
  // when (for example) a proto transform tool synthesizes a field that doesn't appear in the
  // source *.proto files.
  if (location.getSpanCount() > 0) {
    this.displayString =
        String.format(
            "%s:%d:%d",
            element.getFile().getLocation().getDisplayString(),
            location.getSpan(0) + 1,
            location.getSpan(1) + 1);
  } else {
    this.displayString =
        String.format("%s:?:?", element.getFile().getLocation().getDisplayString());
  }
  this.element = element;
}
 
Example #13
Source File: ShadowApply.java    From metastore with 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 #14
Source File: MetaDataProtoEditor.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
private static void renameRecordTypeUsages(@Nonnull String namespace,
                                           @Nonnull DescriptorProtos.DescriptorProto.Builder messageTypeBuilder,
                                           @Nonnull String fullOldRecordTypeName, @Nonnull String fullNewRecordTypeName) {
    // Rename any fields within the record type to the new type name
    for (DescriptorProtos.FieldDescriptorProto.Builder field : messageTypeBuilder.getFieldBuilderList()) {
        final FieldTypeMatch fieldTypeMatch = fieldIsType(namespace, messageTypeBuilder, field, fullOldRecordTypeName);
        if (fieldTypeMatch.isAmbiguousMatch()) {
            throw new AmbiguousTypeNameException(namespace, messageTypeBuilder, field, fullOldRecordTypeName);
        } else if (FieldTypeMatch.MATCHES.equals(fieldTypeMatch)) {
            field.setTypeName(fullNewRecordTypeName);
        } else if (FieldTypeMatch.MATCHES_AS_NESTED.equals(fieldTypeMatch)) {
            final String messageNamespace = (namespace.isEmpty()) ? messageTypeBuilder.getName() : (namespace + "." + messageTypeBuilder.getName());
            final String fieldTypeName = fullyQualifiedTypeName(messageNamespace, field.getTypeName());
            final String newFieldTypeName = fullNewRecordTypeName + fieldTypeName.substring(fullOldRecordTypeName.length());
            field.setTypeName(newFieldTypeName);
        }
    }
    // Rename the record type if used within any nested message types
    if (messageTypeBuilder.getNestedTypeCount() > 0) {
        final String nestedNamespace = namespace.isEmpty() ? messageTypeBuilder.getName() : (namespace + "." + messageTypeBuilder.getName());
        for (DescriptorProtos.DescriptorProto.Builder nestedTypeBuilder : messageTypeBuilder.getNestedTypeBuilderList()) {
            renameRecordTypeUsages(nestedNamespace, nestedTypeBuilder, fullOldRecordTypeName, fullNewRecordTypeName);
        }
    }
}
 
Example #15
Source File: ProtoLanguageFileWriterTest.java    From metastore with Apache License 2.0 6 votes vote down vote up
@Test
public void writeMessage() throws Exception {
  DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
      DescriptorProtos.FileDescriptorProto.newBuilder().setName("test").setSyntax("proto3");

  DescriptorProtos.DescriptorProto.Builder descriptor =
      DescriptorProtos.DescriptorProto.newBuilder()
          .setName("Proto3Message")
          .setOptions(TestProto.MESSAGE_OPTIONS)
          .addField(
              DescriptorProtos.FieldDescriptorProto.newBuilder()
                  .setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL)
                  .setNumber(1)
                  .setName("field_1")
                  .setOptions(TestProto.FIELD_OPTIONS)
                  .build());

  fileDescriptorProtoBuilder.addMessageType(descriptor);
  assertMessage(fileDescriptorProtoBuilder.build(), null);
}
 
Example #16
Source File: ProtoDiffTest.java    From metastore with Apache License 2.0 6 votes vote down vote up
@Test
public void addEnum() throws Exception {
  ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
  DescriptorProtos.FileDescriptorProto fd =
      FILE_V1
          .toBuilder()
          .addEnumType(
              DescriptorProtos.EnumDescriptorProto.newBuilder()
                  .setName("Enum2")
                  .addValue(
                      DescriptorProtos.EnumValueDescriptorProto.newBuilder()
                          .setName("ENUM_VALUE2_UNSET")
                          .setNumber(0)
                          .build())
                  .build())
          .build();

  ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
  Report report = diff(dRef, dNew);
  EnumResult result = report.getEnumResultsMap().get("package.v1.Enum2");
  Assert.assertEquals("package.v1.Enum2", result.getChange().getToName());
  Assert.assertEquals(ChangeType.ADDITION, result.getChange().getChangeType());
}
 
Example #17
Source File: DataVerifyPbEnum.java    From xresloader with MIT License 6 votes vote down vote up
public DataVerifyPbEnum(DescriptorProtos.EnumDescriptorProto desc) {
    super(desc.getName());

    for (DescriptorProtos.EnumValueDescriptorProto val_desc : desc.getValueList()) {
        all_names.put(val_desc.getName(), (long) val_desc.getNumber());
        all_numbers.add((long) val_desc.getNumber());

        // alias extension
        if (val_desc.getOptions().hasExtension(Xresloader.enumAlias)) {
            String alias_name = val_desc.getOptions().getExtension(Xresloader.enumAlias);
            if (!alias_name.isEmpty()) {
                all_names.put(alias_name, (long) val_desc.getNumber());
            }
        }
    }
}
 
Example #18
Source File: ProtoDiffTest.java    From metastore with Apache License 2.0 6 votes vote down vote up
@Test
public void removeMethod() throws Exception {
  ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
  DescriptorProtos.FileDescriptorProto fd =
      FILE_V1
          .toBuilder()
          .setService(0, FILE_V1.getService(0).toBuilder().removeMethod(0))
          .build();

  ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
  Report report = diff(dRef, dNew);
  ServiceResult result = report.getServiceResultsMap().get("package.v1.Service1");
  Assert.assertEquals(ChangeType.UNCHANGED, result.getChange().getChangeType());
  Assert.assertEquals(ChangeType.REMOVAL, result.getMethodResults(0).getChange().getChangeType());
  Assert.assertEquals("Method1", result.getMethodResults(0).getName());
  Assert.assertEquals("Method1", result.getMethodResults(0).getChange().getFromName());
  Assert.assertEquals("", result.getMethodResults(0).getChange().getToName());
}
 
Example #19
Source File: ProtoDiffTest.java    From metastore with Apache License 2.0 6 votes vote down vote up
@Test
public void renameEnumValue() throws Exception {
  ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
  DescriptorProtos.FileDescriptorProto fd =
      FILE_V1
          .toBuilder()
          .setEnumType(
              0,
              FILE_V1
                  .getEnumType(0)
                  .toBuilder()
                  .setValue(1, FILE_V1.getEnumType(0).getValue(1).toBuilder().setName("FOO")))
          .build();

  ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
  Report report = diff(dRef, dNew);
  EnumResult result = report.getEnumResultsMap().get("package.v1.Enum1");
  Assert.assertEquals(ChangeType.UNCHANGED, result.getChange().getChangeType());
  Assert.assertEquals(ChangeType.CHANGED, result.getValueResults(0).getChange().getChangeType());
  Assert.assertEquals(1, result.getValueResults(0).getNumber());
  Assert.assertEquals("FOO", result.getValueResults(0).getName());
  Assert.assertEquals("ENUM_VALUE1_VALUE1", result.getValueResults(0).getChange().getFromName());
  Assert.assertEquals("FOO", result.getValueResults(0).getChange().getToName());
}
 
Example #20
Source File: ProtoLanguageFileWriterTest.java    From metastore with Apache License 2.0 6 votes vote down vote up
@Test
public void noPackageSetTest() throws Exception {
  DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
      DescriptorProtos.FileDescriptorProto.newBuilder().setName("test").setSyntax("proto3");

  DescriptorProtos.DescriptorProto.Builder descriptor =
      DescriptorProtos.DescriptorProto.newBuilder();
  descriptor.setName("TestMessage");
  fileDescriptorProtoBuilder.addMessageType(descriptor);

  testOutput(
      fileDescriptorProtoBuilder.build(),
      null,
      "syntax = \"proto3\";\n"
          + "\n"
          + "import \"test/v1/option.proto\";\n"
          + "\n"
          + "\n"
          + "\n"
          + "message TestMessage {\n"
          + "\n"
          + "}\n");
}
 
Example #21
Source File: DataDstPb.java    From xresloader with MIT License 6 votes vote down vote up
static Descriptors.FileDescriptor try_get_inner_deile_desc(String name) {
    if (inner_file_descs != null) {
        return inner_file_descs.getOrDefault(name.replace('\\', '/').toLowerCase(), null);
    }

    Descriptors.FileDescriptor[] inner_descs = new Descriptors.FileDescriptor[] { Xresloader.getDescriptor(),
            XresloaderUe.getDescriptor(), PbHeaderV3.getDescriptor(), DescriptorProtos.getDescriptor(),
            DurationProto.getDescriptor(), TimestampProto.getDescriptor(), AnyProto.getDescriptor(),
            ApiProto.getDescriptor(), EmptyProto.getDescriptor(), FieldMaskProto.getDescriptor(),
            StructProto.getDescriptor(), TypeProto.getDescriptor(), WrappersProto.getDescriptor(),
            SourceContextProto.getDescriptor(), };

    inner_file_descs = new HashMap<String, Descriptors.FileDescriptor>();
    for (Descriptors.FileDescriptor innerFileDesc : inner_descs) {
        inner_file_descs.put(innerFileDesc.getName().replace('\\', '/').toLowerCase(), innerFileDesc);
    }

    return inner_file_descs.getOrDefault(name.toLowerCase(), null);
}
 
Example #22
Source File: MetaDataProtoEditor.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
private static void setDeprecated(DescriptorProtos.FieldDescriptorProto.Builder fieldBuilder) {
    if (fieldBuilder.hasOptions()) {
        fieldBuilder.getOptionsBuilder().setDeprecated(true);
    } else {
        fieldBuilder.setOptions(DescriptorProtos.FieldOptions.newBuilder().setDeprecated(true).build());
    }
}
 
Example #23
Source File: ProtoToAvroSchemaTest.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplexWrapperValues() throws IOException, StatusRuntimeException {
  String node = getJsonNode("testComplexWrapperValues");

  final DescriptorProtos.FileDescriptorProto fileDescriptorProto =
      TestWrapperTypes.getDescriptor().getFile().toProto();
  ProtoDomain protoDomain = ProtoDomain.builder().add(fileDescriptorProto).build();

  String avroSchema =
      ProtoToAvroSchema.convert(
          protoDomain, String.format("%s.TestWrapperTypes", fileDescriptorProto.getPackage()));

  Assert.assertEquals(node, avroSchema);
}
 
Example #24
Source File: MetaDataProtoEditor.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
private static void renameRecordTypeUsages(@Nonnull DescriptorProtos.FileDescriptorProto.Builder recordsBuilder,
                                           @Nonnull String oldRecordTypeName, @Nonnull String newRecordTypeName) {
    final String namespace = recordsBuilder.getPackage();
    final String fullOldRecordTypeName = fullyQualifiedTypeName(namespace, oldRecordTypeName);
    final String fullNewRecordTypeName = fullyQualifiedTypeName(namespace, newRecordTypeName);

    // Rename the record type within the file
    for (DescriptorProtos.DescriptorProto.Builder messageTypeBuilder : recordsBuilder.getMessageTypeBuilderList()) {
        // Change any fields referencing the old type so that they now reference the new type
        renameRecordTypeUsages(namespace, messageTypeBuilder, fullOldRecordTypeName, fullNewRecordTypeName);
        if (messageTypeBuilder.getName().equals(oldRecordTypeName)) {
            // If renaming the union type, be sure that the record.usage option is set to UNION.
            if (isUnion(messageTypeBuilder)) {
                RecordMetaDataOptionsProto.RecordTypeOptions recordOptions = null;
                if (messageTypeBuilder.getOptions().hasExtension(RecordMetaDataOptionsProto.record)) {
                    recordOptions = messageTypeBuilder.getOptionsBuilder().getExtension(RecordMetaDataOptionsProto.record);
                }
                if (recordOptions == null || !recordOptions.hasUsage() ||
                        !recordOptions.getUsage().equals(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.UNION)) {
                    RecordMetaDataOptionsProto.RecordTypeOptions.Builder recordOptionsBuilder = recordOptions == null
                                                                                                ? RecordMetaDataOptionsProto.RecordTypeOptions.newBuilder()
                                                                                                : recordOptions.toBuilder();
                    recordOptionsBuilder.setUsage(RecordMetaDataOptionsProto.RecordTypeOptions.Usage.UNION);
                    messageTypeBuilder.getOptionsBuilder().setExtension(RecordMetaDataOptionsProto.record, recordOptionsBuilder.build());
                }
            }
            messageTypeBuilder.setName(newRecordTypeName);
        }
    }
}
 
Example #25
Source File: ProtoDomain.java    From beam with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream ois) throws IOException {
  byte[] buffer = new byte[ois.readInt()];
  ois.readFully(buffer);
  fileDescriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(buffer);
  hashCode = java.util.Arrays.hashCode(buffer);
  crosswire();
}
 
Example #26
Source File: MetaDataProtoEditor.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
@Nonnull
static FieldTypeMatch fieldIsType(@Nonnull DescriptorProtos.FileDescriptorProtoOrBuilder file,
                                  @Nonnull DescriptorProtos.DescriptorProtoOrBuilder message,
                                  @Nonnull DescriptorProtos.FieldDescriptorProtoOrBuilder field,
                                  @Nonnull String typeName) {
    return fieldIsType(file.getPackage(), message, field, fullyQualifiedTypeName(file, typeName));
}
 
Example #27
Source File: ProtoDiffTest.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Test
public void renameMethod() throws Exception {
  ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
  DescriptorProtos.FileDescriptorProto fd =
      FILE_V1
          .toBuilder()
          .setService(
              0,
              FILE_V1
                  .getService(0)
                  .toBuilder()
                  .setMethod(
                      0, FILE_V1.getService(0).getMethod(0).toBuilder().setName("MethodX")))
          .build();

  ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
  Report report = diff(dRef, dNew);
  ServiceResult result = report.getServiceResultsMap().get("package.v1.Service1");
  Assert.assertEquals(ChangeType.UNCHANGED, result.getChange().getChangeType());

  Assert.assertEquals(
      ChangeType.ADDITION, result.getMethodResults(0).getChange().getChangeType());
  Assert.assertEquals("MethodX", result.getMethodResults(0).getName());
  Assert.assertEquals("", result.getMethodResults(0).getChange().getFromName());
  Assert.assertEquals("MethodX", result.getMethodResults(0).getChange().getToName());

  Assert.assertEquals(ChangeType.REMOVAL, result.getMethodResults(1).getChange().getChangeType());
  Assert.assertEquals("Method1", result.getMethodResults(1).getName());
  Assert.assertEquals("Method1", result.getMethodResults(1).getChange().getFromName());
  Assert.assertEquals("", result.getMethodResults(1).getChange().getToName());
}
 
Example #28
Source File: ServiceRegisterUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
public static List<DescriptorProtos.FileDescriptorSet> registerByIpAndPort(String hostAndPort) {
    String[] strings = hostAndPort.split(":");
    if (strings.length != 2) {
        return emptyList();
    }
    return registerByIpAndPort(strings[0], Integer.parseInt(strings[1]));
}
 
Example #29
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Test
public void enumFieldChanged() {
    // Add an enum field
    FileDescriptor updatedFile = mutateFile(TestRecordsEnumProto.getDescriptor(), fileBuilder ->
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (message.getName().equals("MyShapeRecord")) {
                    message.getEnumTypeBuilderList().forEach(enumType -> {
                        if (enumType.getName().equals("Size")) {
                            enumType.addValue(DescriptorProtos.EnumValueDescriptorProto.newBuilder()
                                    .setName("X_LARGE")
                                    .setNumber(TestRecordsEnumProto.MyShapeRecord.Size.getDescriptor().getValues().stream()
                                                       .mapToInt(Descriptors.EnumValueDescriptor::getNumber).max().getAsInt() + 1));
                        }
                    });
                }
            })
    );
    validator.validateUnion(TestRecordsEnumProto.RecordTypeUnion.getDescriptor(), updatedFile.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
    RecordMetaData metaData1 = RecordMetaData.build(TestRecordsEnumProto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    validator.validate(metaData1, metaData2);

    // Dropping a value is not allowed
    assertInvalid("enum removes value", updatedFile, TestRecordsEnumProto.getDescriptor());
    RecordMetaData metaData3 = RecordMetaData.build(updatedFile);
    RecordMetaData metaData4 = replaceRecordsDescriptor(metaData3, TestRecordsEnumProto.getDescriptor());
    assertInvalid("enum removes value", metaData3, metaData4);

    // Changing the value name is okay
    updatedFile = mutateFile(TestRecordsEnumProto.getDescriptor(), fileBuilder ->
            fileBuilder.getEnumTypeBuilderList().forEach(enumType -> {
                if (enumType.getName().equals("Size")) {
                    enumType.getValueBuilder(0).setName("PETIT");
                }
            })
    );
    validator.validateUnion(TestRecordsEnumProto.RecordTypeUnion.getDescriptor(), updatedFile.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
    RecordMetaData metaData5 = replaceRecordsDescriptor(metaData1, updatedFile);
    validator.validate(metaData1, metaData5);
}
 
Example #30
Source File: ProfileTest.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Test
public void profileAllowAdd_RemoveFieldV1() throws Exception {
  ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
  DescriptorProtos.FileDescriptorProto fd =
      FILE_V1
          .toBuilder()
          .setMessageType(0, FILE_V1.getMessageType(0).toBuilder().removeField(0))
          .build();

  ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
  Report report = diff(dRef, dNew);
  report = new ProfileAllowAdd().validate(report);
  assertEquals(1, report.getMessageResultsCount());
  assertEquals(1, report.getResultCount().getDiffErrors());
}