Java Code Examples for com.google.protobuf.DescriptorProtos.FileDescriptorSet#getFileList()

The following examples show how to use com.google.protobuf.DescriptorProtos.FileDescriptorSet#getFileList() . 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: 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 2
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 3
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 4
Source File: CommonProto2Java.java    From saluki with Apache License 2.0 6 votes vote down vote up
public void generateFile(String protoPath) {
  try {
    if (pojoTypes == null) {
      pojoTypes = Maps.newHashMap();
    }
  } finally {
    if (!new File(protoPath).exists()) {
      logger.warn("protoPath:" + protoPath
          + " not exist, it may be in the third party jars, so it can't be generate");
      return;
    }
    FileDescriptorSet fileDescriptorSet = commondProtoc.invoke(protoPath);
    for (FileDescriptorProto fdp : fileDescriptorSet.getFileList()) {
      Pair<String, String> packageClassName = this.packageClassName(fdp.getOptions());
      if (packageClassName == null) {
        continue;
      }
      ProtocolStringList dependencyList = fdp.getDependencyList();
      for (Iterator<String> it = dependencyList.iterator(); it.hasNext();) {
        String dependencyPath = discoveryRoot + "/" + it.next();
        generateFile(dependencyPath);
      }
      doPrint(fdp, packageClassName.getLeft(), packageClassName.getRight());
    }
  }
}
 
Example 5
Source File: ServiceResolver.java    From grpc-swagger 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, FileDescriptorProto> computeDescriptorProtoIndex(
        FileDescriptorSet fileDescriptorSet) {
    ImmutableMap.Builder<String, FileDescriptorProto> resultBuilder = ImmutableMap.builder();
    for (FileDescriptorProto descriptorProto : fileDescriptorSet.getFileList()) {
        resultBuilder.put(descriptorProto.getName(), descriptorProto);
    }
    return resultBuilder.build();
}
 
Example 6
Source File: ServiceResolver.java    From milkman 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, FileDescriptorProto> computeDescriptorProtoIndex(
    FileDescriptorSet fileDescriptorSet) {
  ImmutableMap.Builder<String, FileDescriptorProto> resultBuilder = ImmutableMap.builder();
  for (FileDescriptorProto descriptorProto : fileDescriptorSet.getFileList()) {
    resultBuilder.put(descriptorProto.getName(), descriptorProto);
  }
  return resultBuilder.build();
}
 
Example 7
Source File: ServiceResolver.java    From grpc-swagger 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, FileDescriptorProto> computeDescriptorProtoIndex(
        FileDescriptorSet fileDescriptorSet) {
    ImmutableMap.Builder<String, FileDescriptorProto> resultBuilder = ImmutableMap.builder();
    for (FileDescriptorProto descriptorProto : fileDescriptorSet.getFileList()) {
        resultBuilder.put(descriptorProto.getName(), descriptorProto);
    }
    return resultBuilder.build();
}
 
Example 8
Source File: Bootstrap.java    From krpc with Apache License 2.0 5 votes vote down vote up
private void loadProtoFile(RpcApp app, FileDescriptor base, String file) throws IOException, DescriptorValidationException {

        InputStream in = getResource(file);
        FileDescriptorSet descriptorSet = FileDescriptorSet.parseFrom(in);
        in.close();

        Map<String, Descriptor> descriptors = new HashMap<>();
        for (FileDescriptorProto fdp : descriptorSet.getFileList()) {
            FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[]{base});

            for (Descriptor descriptor : fd.getMessageTypes()) {
                String className = descriptor.getName();
                descriptors.put(className, descriptor);
            }
            for (ServiceDescriptor svr : fd.getServices()) {
                Field f = svr.getOptions().getUnknownFields().getField(KrpcExt.SERVICEID_FIELD_NUMBER);
                String serviceName = svr.getName();
                int serviceId = f.getVarintList().get(0).intValue();
                for (MethodDescriptor m : svr.getMethods()) {
                    String msgName = m.getName();
                    Field f2 = m.getOptions().getUnknownFields().getField(KrpcExt.MSGID_FIELD_NUMBER);
                    int msgId = f2.getVarintList().get(0).intValue();
                    log.info(String.format("dynamic proto resource loaded, serviceId=%d,msgId=%d,serviceName=%s,msgName=%s", serviceId, msgId, serviceName, msgName));
                    Descriptor reqDesc = m.getInputType();
                    Descriptor resDesc = m.getOutputType();
                    app.serviceMetas.addDynamic(serviceId, msgId, reqDesc, resDesc, serviceName, msgName);
                }
            }
        }
    }
 
Example 9
Source File: ExtensionPool.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
public Builder setFileDescriptorSet(FileDescriptorSet descriptorSet) {
  Preconditions.checkState(this.descriptor == null, "can only add one FileDescriptorSet");
  this.descriptor = descriptorSet;
  for (FileDescriptorProto fileDescriptor : descriptorSet.getFileList()) {
    add(fileDescriptor);
  }
  return this;
}
 
Example 10
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
public void decompile(FileDescriptorSet setDescriptor) throws IOException {
    for (FileDescriptorProto fileDescriptor : setDescriptor.getFileList()) {
        newline();
        format("===== %s =====", fileDescriptor.getName());
        newline();
        decompile(fileDescriptor);
    }
}