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

The following examples show how to use com.google.protobuf.DescriptorProtos.FileDescriptorSet#parseFrom() . 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: 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 2
Source File: ModelBuilder.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private FileDescriptorSet parseFileAsDescriptorSet(
    FileWrapper inputFile, ModelBuildOverrides registry, DiagCollector diagCollector) {
  ByteString extensionFile = inputFile.getFileContents();
  try {
    return FileDescriptorSet.parseFrom(extensionFile, registry.getPlatformExtensions());
  } catch (InvalidProtocolBufferException e) {

    diagCollector.addDiag(
        Diag.error(
            SimpleLocation.TOPLEVEL,
            "Cannot read file descriptor file '%s': %s",
            inputFile.getFilename(),
            e.getMessage()));
    return null;
  }
}
 
Example 3
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 4
Source File: ToolProtoUtil.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
public static FileDescriptorSet openDescriptorSet(String fileName, ExtensionRegistry registry) {
  try {
    return FileDescriptorSet.parseFrom(new FileInputStream(fileName), registry);
  } catch (IOException e) {
    throw new RuntimeException(String.format("Cannot open+parse input file '%s'", fileName), e);
  }
}
 
Example 5
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ProtobufDecompiler decompiler = new ProtobufDecompiler((Appendable)System.out);
    FileDescriptorSet set;
    try (FileInputStream istr = new FileInputStream(args[0])) {
        set = FileDescriptorSet.parseFrom(istr);
    }
    decompiler.decompile(set);
}
 
Example 6
Source File: TestConfig.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/** Returns the file descriptor set generated from the sources of this api. */
public FileDescriptorSet getDescriptor() throws IOException {
  return FileDescriptorSet.parseFrom(Files.newInputStream(descriptorFile), EXTENSIONS);
}
 
Example 7
Source File: DynamicSchema.java    From protobuf-dynamic with Apache License 2.0 2 votes vote down vote up
/**
 * Parses a serialized schema descriptor (from byte array)
 * 
 * @param schemaDescBuf the descriptor byte array
 * @return the schema object
 * @throws DescriptorValidationException
 * @throws IOException
 */
public static DynamicSchema parseFrom(byte[] schemaDescBuf) throws DescriptorValidationException, IOException {
	return new DynamicSchema(FileDescriptorSet.parseFrom(schemaDescBuf));
}