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

The following examples show how to use com.google.protobuf.DescriptorProtos#FileDescriptorSet . 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: ProtobufParser.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
/**
 * Compile the protobuf and generate descriptor file.
 *
 * @param protoPath      protobuf file path
 * @param descriptorPath descriptor file path
 * @return {@link DescriptorProtos.FileDescriptorSet} object
 */
private static DescriptorProtos.FileDescriptorProto generateRootFileDescriptor(String protoPath,
                                                                              String descriptorPath) {
    String command = new ProtocCommandBuilder
            (protoPath, resolveProtoFolderPath(protoPath), descriptorPath).build();
    generateDescriptor(command);
    File initialFile = new File(descriptorPath);
    try (InputStream targetStream = new FileInputStream(initialFile)) {
        ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
        //to register all custom extensions in order to parse properly
        ExtensionHolder.registerAllExtensions(extensionRegistry);
        DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(targetStream,
                extensionRegistry);
        logger.debug("Descriptor file is parsed successfully. file:" , descriptorPath);
        if (set.getFileList().size() > 0) {
            return set.getFile(0);
        }
    } catch (IOException e) {
        throw new CLIInternalException("Error reading generated descriptor file '" + descriptorPath + "'.", e);
    }
    return null;
}
 
Example 2
Source File: DescriptorSet.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
/** Returns a map containing the comments for types and fields. */
public static ImmutableMap<String, String> getCommentsFromDescriptorFile(
    InputStream descriptorSetInputStream) {
  try {
    DescriptorProtos.FileDescriptorSet descriptors =
        DescriptorProtos.FileDescriptorSet.parseFrom(descriptorSetInputStream);
    return descriptors.getFileList().stream()
        .flatMap(
            fileDescriptorProto -> parseDescriptorFile(fileDescriptorProto).entrySet().stream())
        .collect(
            ImmutableMap.toImmutableMap(
                Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> value1));
  } catch (IOException ignored) {
  }
  return ImmutableMap.of();
}
 
Example 3
Source File: ServiceRegisterUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
public static List<DescriptorProtos.FileDescriptorSet> registerByIpAndPort(String host, int port) {
    HostAndPort hostAndPort = HostAndPort.fromParts(host, port);
    Channel channel = ManagedChannelBuilder.forAddress(hostAndPort.getHost(), hostAndPort.getPort())
            .usePlaintext()
            .build();

    List<DescriptorProtos.FileDescriptorSet> fileDescriptorSets = GrpcReflectionUtils.resolveServices(channel);
    fileDescriptorSets.forEach(fileDescriptorSet -> {
        ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
        parseDefinition(serviceResolver);
    });
    return fileDescriptorSets;
}
 
Example 4
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static ProtobufDescriptorMap from(URL fileDescriptorUrl) throws IOException {
  InputStream stream = fileDescriptorUrl.openStream();

  DescriptorProtos.FileDescriptorSet fileDescriptorSet =
      DescriptorProtos.FileDescriptorSet.parseFrom(stream);
  return from(fileDescriptorSet);
}
 
Example 5
Source File: ProtoDomain.java    From metastore with Apache License 2.0 5 votes vote down vote up
public DescriptorProtos.FileDescriptorSet toFileDescriptorSet() {
  DescriptorProtos.FileDescriptorSet.Builder setBuilder =
      DescriptorProtos.FileDescriptorSet.newBuilder();
  fileDescriptorMap.forEach(
      (name, fd) -> {
        DescriptorProtos.FileDescriptorProto fileDescriptorProto =
            DescriptorProtos.FileDescriptorProto.newBuilder(fd.toProto()).build();
        setBuilder.addFile(fileDescriptorProto);
      });
  return setBuilder.build();
}
 
Example 6
Source File: ProtobufTypeUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a protobuf descriptor instance from a FileDescriptor set.
 *
 * @param set               set of file descriptors
 * @param fileDescriptorMap map of message types to file descriptors
 * @param descriptorFile    descriptor file for message to be decoded
 * @param qualifiedMessageType       the name of the message to be decoded
 * @return protobuf descriptor instance
 * @throws StageException
 */
public static Descriptors.Descriptor getDescriptor(
    DescriptorProtos.FileDescriptorSet set,
    Map<String, Descriptors.FileDescriptor> fileDescriptorMap,
    String descriptorFile,
    String qualifiedMessageType
) throws StageException {

  // find the FileDescriptorProto which contains the message type
  // IF cannot find, then bail out
  String packageName = null;
  String messageType = qualifiedMessageType;
  int lastIndex = qualifiedMessageType.lastIndexOf('.');
  if (lastIndex != -1) {
    packageName = qualifiedMessageType.substring(0, lastIndex);
    messageType = qualifiedMessageType.substring(lastIndex + 1);
  }
  DescriptorProtos.FileDescriptorProto file = getFileDescProtoForMsgType(packageName, messageType, set);
  if (file == null) {
    // could not find the message type from all the proto files contained in the descriptor file
    throw new StageException(Errors.PROTOBUF_00, qualifiedMessageType, descriptorFile);
  }
  // finally get the FileDescriptor for the message type
  Descriptors.FileDescriptor fileDescriptor = fileDescriptorMap.get(file.getName());
  // create builder using the FileDescriptor
  // this can only find the top level message types
  return fileDescriptor.findMessageTypeByName(messageType);

}
 
Example 7
Source File: ServiceRegisterUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
public static List<DescriptorProtos.FileDescriptorSet> registerByIpAndPort(String host, int port) {
    HostAndPort hostAndPort = HostAndPort.fromParts(host, port);
    Channel channel = ManagedChannelBuilder.forAddress(hostAndPort.getHost(), hostAndPort.getPort())
            .usePlaintext()
            .build();

    List<DescriptorProtos.FileDescriptorSet> fileDescriptorSets = GrpcReflectionUtils.resolveServices(channel);
    fileDescriptorSets.forEach(fileDescriptorSet -> {
        ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
        parseDefinition(serviceResolver);
    });
    return fileDescriptorSets;
}
 
Example 8
Source File: ServiceRegisterUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
public static List<String> getServiceNames(List<DescriptorProtos.FileDescriptorSet> fileDescriptorSets) {
    List<String> serviceNames = new ArrayList<>();
    fileDescriptorSets.forEach(fileDescriptorSet -> {
        ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
        serviceResolver.listServices().forEach(serviceDescriptor -> {
            String serviceName = serviceDescriptor.getFullName();
            if (blockServiceSet.contains(serviceName.toLowerCase())) {
                return;
            }
            serviceNames.add(serviceName);
        });
    });
    return serviceNames;
}
 
Example 9
Source File: ServiceResolver.java    From karate-grpc with MIT License 5 votes vote down vote up
/**
 * Returns a map from descriptor proto name as found inside the descriptors to protos.
 */
private static ImmutableMap<String, DescriptorProtos.FileDescriptorProto> computeDescriptorProtoIndex(
        DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
    ImmutableMap.Builder<String, DescriptorProtos.FileDescriptorProto> resultBuilder = ImmutableMap.builder();

    List<DescriptorProtos.FileDescriptorProto> descriptorProtos = fileDescriptorSet.getFileList();
    descriptorProtos.forEach(descriptorProto -> resultBuilder.put(descriptorProto.getName(), descriptorProto));

    return resultBuilder.build();
}
 
Example 10
Source File: ServiceRegisterUtils.java    From grpc-swagger with MIT License 5 votes vote down vote up
public static List<String> getServiceNames(List<DescriptorProtos.FileDescriptorSet> fileDescriptorSets) {
    List<String> serviceNames = new ArrayList<>();
    fileDescriptorSets.forEach(fileDescriptorSet -> {
        ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
        serviceResolver.listServices().forEach(serviceDescriptor -> {
            String serviceName = serviceDescriptor.getFullName();
            if (blockServiceSet.contains(serviceName.toLowerCase())) {
                return;
            }
            serviceNames.add(serviceName);
        });
    });
    return serviceNames;
}
 
Example 11
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
public static ProtobufDescriptorMap from(String fileDescriptorPath) throws IOException {
  File file = new File(fileDescriptorPath);
  byte[] descriptorBytes = Files.readAllBytes(file.toPath());
  DescriptorProtos.FileDescriptorSet fileDescriptorSet =
      DescriptorProtos.FileDescriptorSet.parseFrom(descriptorBytes);
  return from(fileDescriptorSet);
}
 
Example 12
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
public static ProtobufDescriptorMap from(URL fileDescriptorUrl) throws IOException {
  InputStream stream = fileDescriptorUrl.openStream();

  DescriptorProtos.FileDescriptorSet fileDescriptorSet =
      DescriptorProtos.FileDescriptorSet.parseFrom(stream);
  return from(fileDescriptorSet);
}
 
Example 13
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
public static ProtobufDescriptorMap from(DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  Map<String, Descriptors.FileDescriptor> resolvedSet =
      FileDescriptorResolver.resolve(fileDescriptorSet);

  Map<String, Descriptors.GenericDescriptor> messageOrEnumDescriptors = new HashMap<>();

  for (Descriptors.FileDescriptor fileDescriptor : resolvedSet.values()) {
    addMessages(messageOrEnumDescriptors, fileDescriptor, packageName(fileDescriptor));
    addEnums(messageOrEnumDescriptors, fileDescriptor, packageName(fileDescriptor));
  }
  return new ProtobufDescriptorMap(messageOrEnumDescriptors);
}
 
Example 14
Source File: FileDescriptorResolver.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private FileDescriptorResolver(DescriptorProtos.FileDescriptorSet descriptorSet) {
  // dependencies are specified in the form of a proto filename, therefore we need to index the
  // FileDescriptorProto's in that file by their name.
  this.descriptorByName =
      descriptorSet.getFileList().stream()
          .map(FileDescriptorProtoWrapper::new)
          .collect(toMap(FileDescriptorProtoWrapper::name, Function.identity()));
}
 
Example 15
Source File: FileDescriptorResolver.java    From stateful-functions with Apache License 2.0 4 votes vote down vote up
static Map<String, Descriptors.FileDescriptor> resolve(
    DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  FileDescriptorResolver resolver = new FileDescriptorResolver(fileDescriptorSet);
  return resolver.resolve();
}
 
Example 16
Source File: ProtoDomain.java    From beam with Apache License 2.0 4 votes vote down vote up
private static Map<String, DescriptorProtos.FileDescriptorProto> extractProtoMap(
    DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  HashMap<String, DescriptorProtos.FileDescriptorProto> map = new HashMap<>();
  fileDescriptorSet.getFileList().forEach(fdp -> map.put(fdp.getName(), fdp));
  return map;
}
 
Example 17
Source File: ProtobufTypeUtil.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private static Set<Descriptors.FileDescriptor> getDependencies(
    Map<String, Set<Descriptors.FileDescriptor>> dependenciesMap,
    Map<String, Descriptors.FileDescriptor> fileDescriptorMap,
    DescriptorProtos.FileDescriptorProto file,
    DescriptorProtos.FileDescriptorSet set
) throws StageException {
  Set<Descriptors.FileDescriptor> result = new LinkedHashSet<>();
  for (String name : file.getDependencyList()) {
    DescriptorProtos.FileDescriptorProto fileDescriptorProto = null;
    for (DescriptorProtos.FileDescriptorProto fdp : set.getFileList()) {
      if (name.equals(fdp.getName())) {
        fileDescriptorProto = fdp;
        break;
      }
    }
    if (fileDescriptorProto == null) {
      // could not find the message type from all the proto files contained in the descriptor file
      throw new StageException(Errors.PROTOBUF_01, file.getName());
    }
    Descriptors.FileDescriptor fileDescriptor;
    if (fileDescriptorMap.containsKey(fileDescriptorProto.getName())) {
      fileDescriptor = fileDescriptorMap.get(fileDescriptorProto.getName());
    } else {
      Set<Descriptors.FileDescriptor> deps = new LinkedHashSet<>();
      if (dependenciesMap.containsKey(name)) {
        deps.addAll(dependenciesMap.get(name));
      } else {
        deps.addAll(getDependencies(dependenciesMap, fileDescriptorMap, fileDescriptorProto, set));
      }
      try {
        fileDescriptor = Descriptors.FileDescriptor.buildFrom(
            fileDescriptorProto,
            deps.toArray(new Descriptors.FileDescriptor[deps.size()])
        );
      } catch (Descriptors.DescriptorValidationException e) {
        throw new StageException(Errors.PROTOBUF_07, e.getDescription(), e);
      }
    }
    result.add(fileDescriptor);
  }
  return result;
}
 
Example 18
Source File: Convert.java    From metastore with Apache License 2.0 4 votes vote down vote up
static Map<String, Descriptors.FileDescriptor> convertFileDescriptorSet(
    DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  Map<String, DescriptorProtos.FileDescriptorProto> inMap = extractProtoMap(fileDescriptorSet);
  return convertToFileDescriptorMap(inMap);
}
 
Example 19
Source File: ProtoDomain.java    From metastore with Apache License 2.0 4 votes vote down vote up
public DescriptorProtos.FileDescriptorSet getFileDescriptorSet() {
  return fileDescriptorSet;
}
 
Example 20
Source File: ProtoDomain.java    From beam with Apache License 2.0 4 votes vote down vote up
public static ProtoDomain buildFrom(DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  return new ProtoDomain(fileDescriptorSet);
}