Search in sources :

Example 1 with FileDescriptorSet

use of com.google.protobuf.DescriptorProtos.FileDescriptorSet in project tesla by linking12.

the class ProtobufUtil method findDirectyprotobuf.

private static Pair<Descriptor, Descriptor> findDirectyprotobuf(final FilterRpcDO rpcDo) {
    byte[] protoContent = rpcDo.getProtoContext();
    FileDescriptorSet descriptorSet = null;
    if (protoContent != null && protoContent.length > 0) {
        try {
            descriptorSet = FileDescriptorSet.parseFrom(protoContent);
            ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(descriptorSet);
            ProtoMethodName protoMethodName = ProtoMethodName.parseFullGrpcMethodName(rpcDo.getServiceName() + "/" + rpcDo.getMethodName());
            MethodDescriptor protoMethodDesc = serviceResolver.resolveServiceMethod(protoMethodName);
            return new ImmutablePair<Descriptor, Descriptor>(protoMethodDesc.getInputType(), protoMethodDesc.getOutputType());
        } catch (InvalidProtocolBufferException e) {
            LOG.error(e.getMessage(), e);
            throw new RpcBizException("protobuf service definition is invalid,the descriptorSet is: " + descriptorSet, e);
        }
    }
    return null;
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) FileDescriptorSet(com.google.protobuf.DescriptorProtos.FileDescriptorSet) RpcBizException(com.quancheng.saluki.core.grpc.exception.RpcBizException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor)

Example 2 with FileDescriptorSet

use of com.google.protobuf.DescriptorProtos.FileDescriptorSet in project toolkit by googleapis.

the class ProtocGeneratorMain method parseOptions.

private static ToolOptions parseOptions(CodeGeneratorRequest request) throws Exception {
    List<FileDescriptorProto> fileDescriptorProtoList = request.getProtoFileList();
    FileDescriptorSet descriptorSet = FileDescriptorSet.newBuilder().addAllFile(fileDescriptorProtoList).build();
    // Write out DescriptorSet to temp file.
    File descriptorSetFile;
    descriptorSetFile = File.createTempFile("api", ".desc");
    FileOutputStream fileoutput = new FileOutputStream(descriptorSetFile);
    descriptorSet.writeTo(fileoutput);
    fileoutput.close();
    descriptorSetFile.deleteOnExit();
    List<String> parsedArgs = new LinkedList<>();
    parsedArgs.add("--descriptor_set");
    parsedArgs.add(descriptorSetFile.getAbsolutePath());
    List<String> protoPackages = getProtoPackageList(request);
    if (protoPackages.size() > 1) {
        // can be changed when the use case arises.
        throw new IllegalStateException(String.format("Not expecting more than one proto package." + " Found proto packages for generation: %s", protoPackages.toString()));
    } else if (protoPackages.size() == 0) {
        throw new IllegalStateException("No proto files given to generate.");
    }
    parsedArgs.add("--package");
    parsedArgs.add(protoPackages.get(0));
    // Parse plugin params, ignoring unknown params.
    String[] requestArgs = request.getParameter().split(",");
    for (String arg : requestArgs) {
        if (Strings.isNullOrEmpty(arg))
            continue;
        parsedArgs.add("--" + arg);
    }
    String[] argsArray = parsedArgs.toArray(new String[] {});
    return GeneratorMain.createCodeGeneratorOptionsFromProtoc(argsArray);
}
Also used : FileDescriptorSet(com.google.protobuf.DescriptorProtos.FileDescriptorSet) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) LinkedList(java.util.LinkedList)

Example 3 with FileDescriptorSet

use of com.google.protobuf.DescriptorProtos.FileDescriptorSet in project BIMserver by opensourceBIM.

the class ProtocolBuffersMetaData method load.

public void load(InputStream inputStream) {
    try {
        FileDescriptorSet descriptorSet = FileDescriptorSet.parseFrom(inputStream);
        List<FileDescriptorProto> fileList = descriptorSet.getFileList();
        FileDescriptorProto fileDescriptorProto = fileList.get(0);
        FileDescriptor[] dependencyDescriptors = getDependencyDescriptors(fileDescriptorProto);
        FileDescriptor fileDescriptor = FileDescriptor.buildFrom(fileDescriptorProto, dependencyDescriptors);
        loaded.put(fileDescriptor.getName(), fileDescriptor);
        fileDescriptor.getMessageTypes().forEach(descriptor -> this.messageDescriptors.put(descriptor.getName(), new MessageDescriptorContainer(descriptor)));
        fileDescriptor.getServices().forEach(serviceDescriptor -> this.serviceDescriptors.put(serviceDescriptor.getName(), new ServiceDescriptorContainer(serviceDescriptor)));
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (DescriptorValidationException e) {
        LOGGER.error("", e);
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            LOGGER.error("", e);
        }
    }
}
Also used : FileDescriptorSet(com.google.protobuf.DescriptorProtos.FileDescriptorSet) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) IOException(java.io.IOException) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor)

Aggregations

FileDescriptorSet (com.google.protobuf.DescriptorProtos.FileDescriptorSet)3 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)2 DescriptorValidationException (com.google.protobuf.Descriptors.DescriptorValidationException)1 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)1 MethodDescriptor (com.google.protobuf.Descriptors.MethodDescriptor)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 RpcBizException (com.quancheng.saluki.core.grpc.exception.RpcBizException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1