com.google.protobuf.Descriptors.FileDescriptor Java Examples

The following examples show how to use com.google.protobuf.Descriptors.FileDescriptor. 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: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void dropType() {
    FileDescriptor updatedFile = mutateFile(fileBuilder ->
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (message.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
                    message.removeField(1);
                }
            })
    );
    assertInvalid("record type removed from union", TestRecords1Proto.getDescriptor(), updatedFile);
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    MetaDataException e = assertThrows(MetaDataException.class, () -> replaceRecordsDescriptor(metaData1, updatedFile));
    assertThat(e.getMessage(), containsString("Unknown record type MyOtherRecord"));

    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile, protoBuilder -> {
        protoBuilder.removeRecordTypes(1);
        final List<RecordMetaDataProto.Index> indexes = protoBuilder.getIndexesList().stream()
                .filter(index -> !index.getRecordTypeList().contains("MyOtherRecord"))
                .collect(Collectors.toList());
        protoBuilder.clearIndexes().addAllIndexes(indexes);
    });
    assertInvalid("record type removed from union", metaData1, metaData2);
}
 
Example #2
Source File: GqlInputConverter.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
public GqlInputConverter build() {
  HashBiMap<String, Descriptor> mapping = HashBiMap.create();
  HashBiMap<String, EnumDescriptor> enumMapping = HashBiMap.create(getEnumMap(enumDescriptors));
  LinkedList<Descriptor> loop = new LinkedList<>(descriptors);

  Set<FileDescriptor> fileDescriptorSet = ProtoRegistry.extractDependencies(fileDescriptors);

  for (FileDescriptor fileDescriptor : fileDescriptorSet) {
    loop.addAll(fileDescriptor.getMessageTypes());
    enumMapping.putAll(getEnumMap(fileDescriptor.getEnumTypes()));
  }

  while (!loop.isEmpty()) {
    Descriptor descriptor = loop.pop();
    if (!mapping.containsKey(descriptor.getFullName())) {
      mapping.put(getReferenceName(descriptor), descriptor);
      loop.addAll(descriptor.getNestedTypes());
      enumMapping.putAll(getEnumMap(descriptor.getEnumTypes()));
    }
  }

  return new GqlInputConverter(
      ImmutableBiMap.copyOf(mapping), ImmutableBiMap.copyOf(enumMapping));
}
 
Example #3
Source File: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void processService(ServiceDescriptor service, FileDescriptor fd) {
  String serviceName = service.getFullName();
  checkState(
      !fileDescriptorsBySymbol.containsKey(serviceName),
      "Service already defined: %s",
      serviceName);
  fileDescriptorsBySymbol.put(serviceName, fd);
  for (MethodDescriptor method : service.getMethods()) {
    String methodName = method.getFullName();
    checkState(
        !fileDescriptorsBySymbol.containsKey(methodName),
        "Method already defined: %s",
        methodName);
    fileDescriptorsBySymbol.put(methodName, fd);
  }
}
 
Example #4
Source File: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private ServerReflectionResponse createServerReflectionResponse(
    ServerReflectionRequest request, FileDescriptor fd) {
  FileDescriptorResponse.Builder fdRBuilder = FileDescriptorResponse.newBuilder();

  Set<String> seenFiles = new HashSet<String>();
  Queue<FileDescriptor> frontier = new ArrayDeque<FileDescriptor>();
  seenFiles.add(fd.getName());
  frontier.add(fd);
  while (!frontier.isEmpty()) {
    FileDescriptor nextFd = frontier.remove();
    fdRBuilder.addFileDescriptorProto(nextFd.toProto().toByteString());
    for (FileDescriptor dependencyFd : nextFd.getDependencies()) {
      if (!seenFiles.contains(dependencyFd.getName())) {
        seenFiles.add(dependencyFd.getName());
        frontier.add(dependencyFd);
      }
    }
  }
  return ServerReflectionResponse.newBuilder()
      .setValidHost(request.getHost())
      .setOriginalRequest(request)
      .setFileDescriptorResponse(fdRBuilder)
      .build();
}
 
Example #5
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
            computeDescriptorProtoIndex(descriptorSet);
    Map<String, FileDescriptor> descriptorCache = new HashMap<>();

    ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
    for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
        try {
            result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
        } catch (DescriptorValidationException e) {
            logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #6
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
  ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
      computeDescriptorProtoIndex(descriptorSet);
  Map<String, FileDescriptor> descriptorCache = new HashMap<>();

  ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
  for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
    try {
      result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
    } catch (DescriptorValidationException e) {
      logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
      continue;
    }
  }
  return new ServiceResolver(result.build());
}
 
Example #7
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void processService(ServiceDescriptor service, FileDescriptor fd) {
  String serviceName = service.getFullName();
  checkState(
      !fileDescriptorsBySymbol.containsKey(serviceName),
      "Service already defined: %s",
      serviceName);
  fileDescriptorsBySymbol.put(serviceName, fd);
  for (MethodDescriptor method : service.getMethods()) {
    String methodName = method.getFullName();
    checkState(
        !fileDescriptorsBySymbol.containsKey(methodName),
        "Method already defined: %s",
        methodName);
    fileDescriptorsBySymbol.put(methodName, fd);
  }
}
 
Example #8
Source File: Bootstrap.java    From krpc with Apache License 2.0 6 votes vote down vote up
void loadProtos(RpcApp app, String proto) {

        if (isEmpty(proto)) {
            log.info("no dynamic proto resource need to load");
            return;
        }

        if (!proto.endsWith("/")) proto = proto + "/";

        try {
            InputStream basein = RpcMetas.class.getResourceAsStream("descriptor.proto.pb");
            FileDescriptorSet baseSet = FileDescriptorSet.parseFrom(basein);
            basein.close();
            FileDescriptor base = FileDescriptor.buildFrom(baseSet.getFile(0), new FileDescriptor[]{});

            List<String> files = getProtoFiles(proto);
            for (String file : files) {
                loadProtoFile(app, base, proto + file);
            }
        } catch (Exception e) {
            log.error("load dynamic proto resource failed", e);
        }
    }
 
Example #9
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
        FileDescriptorProto descriptorProto,
        ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
        Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
    // First, check the cache.
    String descriptorName = descriptorProto.getName();
    if (descriptorCache.containsKey(descriptorName)) {
        return descriptorCache.get(descriptorName);
    }

    // Then, fetch all the required dependencies recursively.
    ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
    for (String dependencyName : descriptorProto.getDependencyList()) {
        if (!descriptorProtoIndex.containsKey(dependencyName)) {
            throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
        }
        FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
        dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
    }

    // Finally, construct the actual descriptor.
    FileDescriptor[] empty = new FileDescriptor[0];
    return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #10
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
            computeDescriptorProtoIndex(descriptorSet);
    Map<String, FileDescriptor> descriptorCache = new HashMap<>();

    ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
    for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
        try {
            result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
        } catch (DescriptorValidationException e) {
            logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #11
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 6 votes vote down vote up
public void testInvalidPublicDependency() throws Exception {
  FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
      .setName("foo.proto") .build();
  FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
      .setName("boo.proto")
      .addDependency("foo.proto")
      .addPublicDependency(1)  // Error, should be 0.
      .build();
  FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto,
      new FileDescriptor[0]);
  try {
    Descriptors.FileDescriptor.buildFrom(barProto,
        new FileDescriptor[] {fooFile});
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    assertTrue(
        e.getMessage().indexOf("Invalid public dependency index.") != -1);
  }
}
 
Example #12
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void swapRecordTypes() {
    FileDescriptor updatedFile = mutateFile(fileBuilder -> {
        // Update the field of the union descriptor.
        fileBuilder.getMessageTypeBuilderList().forEach(messageType -> {
            if (messageType.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
                messageType.getFieldBuilderList().forEach(field -> {
                    if (field.getName().equals("_MyOtherRecord")) {
                        field.setTypeName("MySimpleRecord");
                    }
                    if (field.getName().equals("_MySimpleRecord")) {
                        field.setTypeName("MyOtherRecord");
                    }
                });
            }
        });
    });
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
    metaDataBuilder.addIndex("MyOtherRecord", "num_value_3_indexed");
    RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();

    // Swap is noticed as the two records are not of compatible forms
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("", metaData1, metaData2);
}
 
Example #13
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void dropField() {
    FileDescriptor updatedFile = mutateFile(fileBuilder ->
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (message.getName().equals("MySimpleRecord")) {
                    int fieldNumValue2Index = 0;
                    while (!message.getField(fieldNumValue2Index).getName().equals("num_value_2")) {
                        fieldNumValue2Index++;
                    }
                    message.removeField(fieldNumValue2Index);
                }
            })
    );
    assertInvalid("field removed from message descriptor", TestRecords1Proto.getDescriptor(), updatedFile);
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("field removed from message descriptor", metaData1, metaData2);
}
 
Example #14
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void renameField() {
    FileDescriptor updatedFile = mutateField("MySimpleRecord", "num_value_2",
            field -> field.setName("num_value_too"));
    assertInvalid("field renamed", TestRecords1Proto.getDescriptor(), updatedFile);
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("field renamed", metaData1, metaData2);

    // This updates both the field name and its indexes which means that this is actually okay.
    updatedFile = mutateField("MySimpleRecord", "str_value_indexed",
            field -> field.setName("str_value_still_indexed"));
    RecordMetaData metaData3 = replaceRecordsDescriptor(metaData1, updatedFile, protoBuilder ->
            protoBuilder.getIndexesBuilderList().forEach(index -> {
                if (index.getName().equals("MySimpleRecord$str_value_indexed")) {
                    index.setRootExpression(Key.Expressions.field("str_value_still_indexed").toKeyExpression());
                }
            })
    );
    assertInvalid("field renamed", metaData1, metaData3);
}
 
Example #15
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private ServerReflectionResponse createServerReflectionResponse(
    ServerReflectionRequest request, FileDescriptor fd) {
  FileDescriptorResponse.Builder fdRBuilder = FileDescriptorResponse.newBuilder();

  Set<String> seenFiles = new HashSet<>();
  Queue<FileDescriptor> frontier = new ArrayDeque<>();
  seenFiles.add(fd.getName());
  frontier.add(fd);
  while (!frontier.isEmpty()) {
    FileDescriptor nextFd = frontier.remove();
    fdRBuilder.addFileDescriptorProto(nextFd.toProto().toByteString());
    for (FileDescriptor dependencyFd : nextFd.getDependencies()) {
      if (!seenFiles.contains(dependencyFd.getName())) {
        seenFiles.add(dependencyFd.getName());
        frontier.add(dependencyFd);
      }
    }
  }
  return ServerReflectionResponse.newBuilder()
      .setValidHost(request.getHost())
      .setOriginalRequest(request)
      .setFileDescriptorResponse(fdRBuilder)
      .build();
}
 
Example #16
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void nestedTypeChangesName() {
    FileDescriptor updatedFile = mutateFile(TestRecordsWithHeaderProto.getDescriptor(), fileBuilder ->
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (message.getName().equals("HeaderRecord")) {
                    message.setName("Header");
                } else if (message.getName().equals("MyRecord")) {
                    message.getFieldBuilderList().forEach(field -> {
                        if (field.getName().equals("header")) {
                            field.setTypeName("." + fileBuilder.getPackage() + ".Header");
                        }
                    });
                }
            })
    );
    assertThat(updatedFile.getMessageTypes().stream().map(Descriptor::getName).collect(Collectors.toList()), containsInAnyOrder("MyRecord", RecordMetaDataBuilder.DEFAULT_UNION_NAME, "Header"));
    validator.validateUnion(TestRecordsWithHeaderProto.RecordTypeUnion.getDescriptor(), updatedFile.findMessageTypeByName(RecordMetaDataBuilder.DEFAULT_UNION_NAME));
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsWithHeaderProto.getDescriptor());
    metaDataBuilder.getRecordType("MyRecord").setPrimaryKey(Key.Expressions.field("header").nest(Key.Expressions.concatenateFields("path", "rec_no")));
    RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    validator.validate(metaData1, metaData2);
}
 
Example #17
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
    FileDescriptorProto descriptorProto,
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
    Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
  // First, check the cache.
  String descritorName = descriptorProto.getName();
  if (descriptorCache.containsKey(descritorName)) {
    return descriptorCache.get(descritorName);
  }

  // Then, fetch all the required dependencies recursively.
  ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
  for (String dependencyName : descriptorProto.getDependencyList()) {
    if (!descriptorProtoIndex.containsKey(dependencyName)) {
      throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
    }
    FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
    dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
  }

  // Finally, construct the actual descriptor.
  FileDescriptor[] empty = new FileDescriptor[0];
  return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #18
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 6 votes vote down vote up
@Test
public void swapUnionFields() {
    FileDescriptor updatedDescriptor = mutateFile(fileBuilder ->
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (message.getName().equals(RecordMetaDataBuilder.DEFAULT_UNION_NAME)) {
                    message.getFieldBuilderList().forEach(field -> {
                        if (field.getNumber() == 1) {
                            field.setNumber(2);
                        } else {
                            field.setNumber(1);
                        }
                    });
                }
            })
    );
    // The two record types do not have the same form, so swapping them should fail.
    // However, the exact way they fail isn't super important.
    RecordMetaData metaData1 = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedDescriptor);
    assertInvalid("", metaData1, metaData2);
}
 
Example #19
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void getFileByName(ServerReflectionRequest request) {
  String name = request.getFileByFilename();
  FileDescriptor fd = serverReflectionIndex.getFileDescriptorByName(name);
  if (fd != null) {
    serverCallStreamObserver.onNext(createServerReflectionResponse(request, fd));
  } else {
    sendErrorResponse(request, Status.Code.NOT_FOUND, "File not found.");
  }
}
 
Example #20
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Test
public void nestedTypeChangesFieldType() {
    FileDescriptor updatedFile = mutateField("HeaderRecord", "num", TestRecordsWithHeaderProto.getDescriptor(),
            field -> field.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_SFIXED32));
    assertInvalid("field type changed", TestRecordsWithHeaderProto.getDescriptor(), updatedFile);
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsWithHeaderProto.getDescriptor());
    metaDataBuilder.getRecordType("MyRecord").setPrimaryKey(Key.Expressions.field("header").nest(Key.Expressions.concatenateFields("path", "rec_no")));
    RecordMetaData metaData1 = metaDataBuilder.getRecordMetaData();
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("field type changed", metaData1, metaData2);
}
 
Example #21
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void getFileContainingSymbol(ServerReflectionRequest request) {
  String symbol = request.getFileContainingSymbol();
  FileDescriptor fd = serverReflectionIndex.getFileDescriptorBySymbol(symbol);
  if (fd != null) {
    serverCallStreamObserver.onNext(createServerReflectionResponse(request, fd));
  } else {
    sendErrorResponse(request, Status.Code.NOT_FOUND, "Symbol not found.");
  }
}
 
Example #22
Source File: MetaDataEvolutionValidatorTestV3.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
/**
 * Enums are different with proto3 syntax. In particular, they require one of their values have value 0,
 * and they introduce an "UNKNOWN" value all of the time.
 */
@Test
public void enumChanged() {
    RecordMetaData metaData1 = RecordMetaData.build(TestRecordsEnumProtoV3.getDescriptor());

    FileDescriptor updatedFile = mutateEnum(enumType ->
            enumType.addValue(DescriptorProtos.EnumValueDescriptorProto.newBuilder()
                    .setName("X_LARGE")
                    .setNumber(4))
    );
    assertNotNull(updatedFile.findMessageTypeByName("MyShapeRecord").findEnumTypeByName("Size").findValueByName("X_LARGE"));
    RecordMetaData metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    MetaDataEvolutionValidator.getDefaultInstance().validate(metaData1, metaData2);

    updatedFile = mutateEnum(enumType ->
            enumType.getValueBuilderList().forEach(enumValue -> {
                if (enumValue.getName().equals("SMALL")) {
                    enumValue.setName("PETIT");
                }
            })
    );
    assertNotNull(updatedFile.findMessageTypeByName("MyShapeRecord").findEnumTypeByName("Size").findValueByName("PETIT"));
    metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    MetaDataEvolutionValidator.getDefaultInstance().validate(metaData1, metaData2);

    updatedFile = mutateEnum(enumType ->
            enumType.removeValue(enumType.getValueCount() - 1)
    );
    assertNull(updatedFile.findMessageTypeByName("MyShapeRecord").findEnumTypeByName("Size").findValueByName("LARGE"));
    metaData2 = replaceRecordsDescriptor(metaData1, updatedFile);
    assertInvalid("enum removes value", metaData1, metaData2);
}
 
Example #23
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Used in proto1 generated code only.
 *
 * After enabling bridge, we can define proto2 extensions (the extended type
 * is a proto2 mutable message) in a proto1 .proto file. For these extensions
 * we should generate proto2 GeneratedExtensions.
 */
public static <ContainingType extends Message, Type>
   GeneratedExtension<ContainingType, Type>
   newFileScopedGeneratedExtension(
       final Class singularType, final Message defaultInstance,
       final String descriptorOuterClass, final String extensionName) {
  // For extensions scoped within a file, we load the descriptor outer
  // class and rely on it to get the FileDescriptor which then can be
  // used to obtain the extension's FieldDescriptor.
  return new GeneratedExtension<ContainingType, Type>(
      new CachedDescriptorRetriever() {
        protected FieldDescriptor loadDescriptor() {
          try {
            Class clazz =
                singularType.getClassLoader().loadClass(descriptorOuterClass);
            FileDescriptor file =
                (FileDescriptor) clazz.getField("descriptor").get(null);
            return file.findExtensionByName(extensionName);
          } catch (Exception e) {
            throw new RuntimeException(
                "Cannot load descriptors: " + descriptorOuterClass +
                " is not a valid descriptor class name", e);
          }
        }
      },
      singularType,
      defaultInstance,
      Extension.ExtensionType.MUTABLE);
}
 
Example #24
Source File: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Nullable
private FileDescriptor getFileDescriptorBySymbol(String symbol) {
  FileDescriptor fd = immutableServicesIndex.getFileDescriptorBySymbol(symbol);
  if (fd == null) {
    fd = mutableServicesIndex.getFileDescriptorBySymbol(symbol);
  }
  return fd;
}
 
Example #25
Source File: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Nullable
private FileDescriptor getFileDescriptorByExtensionAndNumber(String type, int extension) {
  FileDescriptor fd =
      immutableServicesIndex.getFileDescriptorByExtensionAndNumber(type, extension);
  if (fd == null) {
    fd = mutableServicesIndex.getFileDescriptorByExtensionAndNumber(type, extension);
  }
  return fd;
}
 
Example #26
Source File: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
FileDescriptorIndex(List<ServerServiceDefinition> services) {
  Queue<FileDescriptor> fileDescriptorsToProcess = new ArrayDeque<FileDescriptor>();
  Set<String> seenFiles = new HashSet<String>();
  for (ServerServiceDefinition service : services) {
    io.grpc.ServiceDescriptor serviceDescriptor = service.getServiceDescriptor();
    if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) {
      FileDescriptor fileDescriptor =
          ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor())
              .getFileDescriptor();
      String serviceName = serviceDescriptor.getName();
      checkState(
          !serviceNames.contains(serviceName), "Service already defined: %s", serviceName);
      serviceFileDescriptors.add(fileDescriptor);
      serviceNames.add(serviceName);
      if (!seenFiles.contains(fileDescriptor.getName())) {
        seenFiles.add(fileDescriptor.getName());
        fileDescriptorsToProcess.add(fileDescriptor);
      }
    }
  }

  while (!fileDescriptorsToProcess.isEmpty()) {
    FileDescriptor currentFd = fileDescriptorsToProcess.remove();
    processFileDescriptor(currentFd);
    for (FileDescriptor dependencyFd : currentFd.getDependencies()) {
      if (!seenFiles.contains(dependencyFd.getName())) {
        seenFiles.add(dependencyFd.getName());
        fileDescriptorsToProcess.add(dependencyFd);
      }
    }
  }
}
 
Example #27
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the DescriptorValidationException works as intended.
 */
public void testDescriptorValidatorException() throws Exception {
  FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder()
    .setName("foo.proto")
    .addMessageType(DescriptorProto.newBuilder()
    .setName("Foo")
      .addField(FieldDescriptorProto.newBuilder()
        .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
        .setType(FieldDescriptorProto.Type.TYPE_INT32)
        .setName("foo")
        .setNumber(1)
        .setDefaultValue("invalid")
        .build())
      .build())
    .build();
  try {
    Descriptors.FileDescriptor.buildFrom(fileDescriptorProto,
        new FileDescriptor[0]);
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    // Expected; check that the error message contains some useful hints
    assertTrue(e.getMessage().indexOf("foo") != -1);
    assertTrue(e.getMessage().indexOf("Foo") != -1);
    assertTrue(e.getMessage().indexOf("invalid") != -1);
    assertTrue(e.getCause() instanceof NumberFormatException);
    assertTrue(e.getCause().getMessage().indexOf("invalid") != -1);
  }
}
 
Example #28
Source File: MetaDataEvolutionValidatorTest.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Nonnull
static FileDescriptor mutateField(@Nonnull String messageName, @Nonnull String fieldName, @Nonnull FileDescriptor originalFile,
                                  @Nonnull Consumer<DescriptorProtos.FieldDescriptorProto.Builder> fieldMutation) {
    return mutateFile(originalFile, fileBuilder ->
            fileBuilder.getMessageTypeBuilderList().forEach(message -> {
                if (message.getName().equals(messageName)) {
                    message.getFieldBuilderList().forEach(field -> {
                        if (field.getName().equals(fieldName)) {
                            fieldMutation.accept(field);
                        }
                    });
                }
            })
    );
}
 
Example #29
Source File: PstFileDescriptor.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
private FileDescriptor asFileDescriptor(Class<?> clazz) {
  try {
    Method method = clazz.getMethod("getDescriptor");
    return (FileDescriptor) method.invoke(null);
  } catch (Exception e) {
    return null;
  }
}
 
Example #30
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void getFileByExtension(ServerReflectionRequest request) {
  ExtensionRequest extensionRequest = request.getFileContainingExtension();
  String type = extensionRequest.getContainingType();
  int extension = extensionRequest.getExtensionNumber();
  FileDescriptor fd =
      serverReflectionIndex.getFileDescriptorByExtensionAndNumber(type, extension);
  if (fd != null) {
    serverCallStreamObserver.onNext(createServerReflectionResponse(request, fd));
  } else {
    sendErrorResponse(request, Status.Code.NOT_FOUND, "Extension not found.");
  }
}