com.google.protobuf.compiler.PluginProtos Java Examples

The following examples show how to use com.google.protobuf.compiler.PluginProtos. 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: ProtocPlugin.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static PluginProtos.CodeGeneratorResponse generate(
        @Nonnull List<Generator> generators,
        @Nonnull PluginProtos.CodeGeneratorRequest request) {
    checkNotNull(generators, "generators");
    checkArgument(!generators.isEmpty(), "generators.isEmpty()");
    checkNotNull(request, "request");

    // Run each file generator, collecting the output
    Stream<PluginProtos.CodeGeneratorResponse.File> oldWay = generators
            .stream()
            .flatMap(gen -> gen.generate(request));

    Stream<PluginProtos.CodeGeneratorResponse.File> newWay = generators
            .stream()
            .flatMap(gen -> gen.generateFiles(request).stream());

    // Send the files back to protoc
    return PluginProtos.CodeGeneratorResponse
            .newBuilder()
            .addAllFile(Stream.concat(oldWay, newWay).collect(Collectors.toList()))
            .build();
}
 
Example #2
Source File: ProtoTypeMapTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@BeforeClass
public static void buildProtoTypeMap() throws IOException {
    // Dump file generated during the maven generate-test-sources phase
    final String dumpPath = "target/generated-test-sources/protobuf/dump/descriptor_dump";

    byte[] generatorRequestBytes = ByteStreams.toByteArray(new FileInputStream(new File(dumpPath)));
    PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.parseFrom(
            generatorRequestBytes, ExtensionRegistry.newInstance());
    List<DescriptorProtos.FileDescriptorProto> fileProtos = request.getProtoFileList();
    protoTypeMap =  ProtoTypeMap.of(fileProtos);
}
 
Example #3
Source File: MutinyGrpcGenerator.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public List<PluginProtos.CodeGeneratorResponse.File> generateFiles(PluginProtos.CodeGeneratorRequest request)
        throws GeneratorException {
    ProtoTypeMap typeMap = ProtoTypeMap.of(request.getProtoFileList());

    List<DescriptorProtos.FileDescriptorProto> protosToGenerate = request.getProtoFileList().stream()
            .filter(protoFile -> request.getFileToGenerateList().contains(protoFile.getName()))
            .collect(Collectors.toList());

    List<ServiceContext> services = findServices(protosToGenerate, typeMap);
    return generateFiles(services);
}
 
Example #4
Source File: Generator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a protobuf file message from a given name and content.
 * @param fileName The name of the file to generate.
 * @param fileContent The content of the generated file.
 * @return The protobuf file.
 */
protected PluginProtos.CodeGeneratorResponse.File makeFile(String fileName, byte[] fileContent) {
    return PluginProtos.CodeGeneratorResponse.File
            .newBuilder()
            .setName(fileName)
            .setContentBytes(ByteString.copyFrom(fileContent))
            .build();
}
 
Example #5
Source File: Generator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a protobuf file message from a given name and content.
 * @param fileName The name of the file to generate.
 * @param fileContent The content of the generated file.
 * @return The protobuf file.
 */
protected PluginProtos.CodeGeneratorResponse.File makeFile(String fileName, String fileContent) {
    return PluginProtos.CodeGeneratorResponse.File
            .newBuilder()
            .setName(fileName)
            .setContent(fileContent)
            .build();
}
 
Example #6
Source File: Jdk8Generator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<PluginProtos.CodeGeneratorResponse.File> generateFiles(PluginProtos.CodeGeneratorRequest request) throws GeneratorException {
    final ProtoTypeMap protoTypeMap = ProtoTypeMap.of(request.getProtoFileList());
    List<PluginProtos.CodeGeneratorResponse.File> files = new ArrayList<>();

    for (DescriptorProtos.FileDescriptorProto protoFile : request.getProtoFileList()) {
        if (request.getFileToGenerateList().contains(protoFile.getName())) {
            for (ServiceContext ctx : extractContext(protoTypeMap, protoFile)) {
                files.add(buildFile(ctx));
            }
        }
    }

    return files;
}
 
Example #7
Source File: DumpGenerator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<PluginProtos.CodeGeneratorResponse.File> generateFiles(PluginProtos.CodeGeneratorRequest request) throws GeneratorException {
    try {
        return Arrays.asList(
                makeFile("descriptor_dump", request.toByteArray()),
                makeFile("descriptor_dump.json", JsonFormat.printer().print(request))
        );
    } catch (InvalidProtocolBufferException e) {
        throw new GeneratorException(e.getMessage());
    }
}
 
Example #8
Source File: ReactiveGrpcGenerator.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) {
    String content = applyTemplate(getClassPrefix() + "Stub.mustache", context);
    return PluginProtos.CodeGeneratorResponse.File
            .newBuilder()
            .setName(absoluteFileName(context))
            .setContent(content)
            .build();
}
 
Example #9
Source File: ReactiveGrpcGenerator.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<PluginProtos.CodeGeneratorResponse.File> generateFiles(PluginProtos.CodeGeneratorRequest request) throws GeneratorException {
    final ProtoTypeMap typeMap = ProtoTypeMap.of(request.getProtoFileList());

    List<FileDescriptorProto> protosToGenerate = request.getProtoFileList().stream()
            .filter(protoFile -> request.getFileToGenerateList().contains(protoFile.getName()))
            .collect(Collectors.toList());

    List<ServiceContext> services = findServices(protosToGenerate, typeMap);
    return generateFiles(services);
}
 
Example #10
Source File: AbstractGenerator.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构造文件了
 *
 * @param context
 * @return
 */
private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) {
    final String mustAcheFile = getClassPrefix() + getClassSuffix() + "Stub.mustache";
    String content = applyTemplate(mustAcheFile, context);
    return PluginProtos.CodeGeneratorResponse.File.newBuilder()
        .setName(constructAbsoluteFileName(context)).setContent(content).build();
}
 
Example #11
Source File: AbstractGenerator.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * generate file
 *
 * @param services
 * @return
 */
private List<PluginProtos.CodeGeneratorResponse.File> generateFiles(List<ServiceContext> services) {
    List<PluginProtos.CodeGeneratorResponse.File> list = new ArrayList<>();
    for (ServiceContext service : services) {
        PluginProtos.CodeGeneratorResponse.File file = buildFile(service);
        list.add(file);
    }
    return list;
}
 
Example #12
Source File: AbstractGenerator.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public List<PluginProtos.CodeGeneratorResponse.File> generateFiles(PluginProtos.CodeGeneratorRequest request)
        throws GeneratorException {
    final ProtoTypeMap typeMap = ProtoTypeMap.of(request.getProtoFileList());

    List<FileDescriptorProto> protosToGenerate = new ArrayList<>();
    for (FileDescriptorProto protoFile : request.getProtoFileList()) {
        if (request.getFileToGenerateList().contains(protoFile.getName())) {
            protosToGenerate.add(protoFile);
        }
    }

    List<ServiceContext> services = findServices(protosToGenerate, typeMap);
    return generateFiles(services);
}
 
Example #13
Source File: MutinyGrpcGenerator.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) {
    String content = applyTemplate("MutinyStub.mustache", context);
    return PluginProtos.CodeGeneratorResponse.File
            .newBuilder()
            .setName(absoluteFileName(context))
            .setContent(content)
            .build();
}
 
Example #14
Source File: ReactiveGrpcGenerator.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private List<PluginProtos.CodeGeneratorResponse.File> generateFiles(List<ServiceContext> services) {
    return services.stream()
            .map(this::buildFile)
            .collect(Collectors.toList());
}
 
Example #15
Source File: Jdk8Generator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) {
    return makeFile(absoluteFileName(context), applyTemplate("Jdk8Stub.mustache", context));
}
 
Example #16
Source File: MutinyGrpcGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private List<PluginProtos.CodeGeneratorResponse.File> generateFiles(List<ServiceContext> services) {
    return services.stream()
            .map(this::buildFile)
            .collect(Collectors.toList());
}
 
Example #17
Source File: Generator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Processes a generator request into a set of files to output.
 *
 * @deprecated use {@link #generateFiles(PluginProtos.CodeGeneratorRequest)} and return a List instead of a Stream.
 * @param request The raw generator request from protoc.
 * @return The completed files to write out.
 */
@Deprecated()
public Stream<PluginProtos.CodeGeneratorResponse.File> generate(PluginProtos.CodeGeneratorRequest request) throws GeneratorException {
    return Stream.empty();
}
 
Example #18
Source File: Generator.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Processes a generator request into a set of files to output.
 *
 * @param request The raw generator request from protoc.
 * @return The completed files to write out.
 */
public List<PluginProtos.CodeGeneratorResponse.File> generateFiles(PluginProtos.CodeGeneratorRequest request) throws GeneratorException {
    return Collections.emptyList();
}