java类com.google.protobuf.Descriptors.FileDescriptor的实例源码

SoyProtoTypeProvider.java 文件源码 项目:closure-templates 阅读 23 收藏 0 点赞 0 评论 0
private static FileDescriptor buildDescriptor(
    String name,
    Map<String, FileDescriptor> descriptors,
    Map<String, FileDescriptorProto> protos)
    throws DescriptorValidationException {
  FileDescriptor file = descriptors.get(name);
  if (file != null) {
    return file;
  }
  FileDescriptorProto proto = protos.get(name);
  FileDescriptor[] deps = new FileDescriptor[proto.getDependencyCount()];
  for (int i = 0; i < proto.getDependencyCount(); i++) {
    deps[i] = buildDescriptor(proto.getDependency(i), descriptors, protos);
  }
  file = FileDescriptor.buildFrom(proto, deps);
  descriptors.put(name, file);
  return file;
}
DescriptorsTest.java 文件源码 项目:bazel 阅读 20 收藏 0 点赞 0 评论 0
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);
  }
}
ProtoReflectionService.java 文件源码 项目:grpc-java 阅读 19 收藏 0 点赞 0 评论 0
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();
}
ProtoReflectionService.java 文件源码 项目:grpc-java 阅读 20 收藏 0 点赞 0 评论 0
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);
  }
}
DescriptorsTest.java 文件源码 项目:bazel 阅读 25 收藏 0 点赞 0 评论 0
public void testDependencyOrder() throws Exception {
  FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
      .setName("foo.proto").build();
  FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
      .setName("bar.proto")
      .addDependency("foo.proto")
      .build();
  FileDescriptorProto bazProto = FileDescriptorProto.newBuilder()
      .setName("baz.proto")
      .addDependency("foo.proto")
      .addDependency("bar.proto")
      .addPublicDependency(0)
      .addPublicDependency(1)
      .build();
  FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto,
      new FileDescriptor[0]);
  FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto,
      new FileDescriptor[] {fooFile});

  // Items in the FileDescriptor array can be in any order.
  Descriptors.FileDescriptor.buildFrom(bazProto,
      new FileDescriptor[] {fooFile, barFile});
  Descriptors.FileDescriptor.buildFrom(bazProto,
      new FileDescriptor[] {barFile, fooFile});
}
DescriptorsTest.java 文件源码 项目:bazel 阅读 23 收藏 0 点赞 0 评论 0
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);
  }
}
DescriptorsTest.java 文件源码 项目:sstore-soft 阅读 26 收藏 0 点赞 0 评论 0
/**
 * 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);
  }
}
ProtoRegistry.java 文件源码 项目:rejoiner 阅读 23 收藏 0 点赞 0 评论 0
private static BiMap<String, GraphQLType> getMap(
    List<FileDescriptor> fileDescriptors,
    List<Descriptor> descriptors,
    List<EnumDescriptor> enumDescriptors,
    GraphQLInterfaceType nodeInterface) {
  HashBiMap<String, GraphQLType> mapping = HashBiMap.create(getEnumMap(enumDescriptors));
  LinkedList<Descriptor> loop = new LinkedList<>(descriptors);

  Set<FileDescriptor> fileDescriptorSet = extractDependencies(fileDescriptors);

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

  while (!loop.isEmpty()) {
    Descriptor descriptor = loop.pop();
    if (!mapping.containsKey(descriptor.getFullName())) {
      mapping.put(
          ProtoToGql.getReferenceName(descriptor),
          ProtoToGql.convert(descriptor, nodeInterface));
      GqlInputConverter inputConverter =
          GqlInputConverter.newBuilder().add(descriptor.getFile()).build();
      mapping.put(
          GqlInputConverter.getReferenceName(descriptor),
          inputConverter.getInputType(descriptor));
      loop.addAll(descriptor.getNestedTypes());

      mapping.putAll(getEnumMap(descriptor.getEnumTypes()));
    }
  }
  return ImmutableBiMap.copyOf(mapping);
}
SchemaProviderModule.java 文件源码 项目:rejoiner 阅读 17 收藏 0 点赞 0 评论 0
@Inject
public SchemaImpl(
    @Annotations.Queries Set<GraphQLFieldDefinition> queryFields,
    @Annotations.Mutations Set<GraphQLFieldDefinition> mutationFields,
    @Annotations.GraphModifications Set<TypeModification> modifications,
    @Annotations.ExtraTypes Set<FileDescriptor> fileDescriptors,
    @Annotations.Queries Set<NodeDataFetcher> nodeDataFetchers) {
  this.queryFields = queryFields;
  this.mutationFields = mutationFields;
  this.modifications = modifications;
  this.fileDescriptors = fileDescriptors;
  this.nodeDataFetchers = nodeDataFetchers;
}
DescriptorsTest.java 文件源码 项目:s-store 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 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);
  }
}


问题


面经


文章

微信
公众号

扫码关注公众号