Search in sources :

Example 11 with JavaWriterVisitor

use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.

the class Writer method write.

public static CodeGeneratorResponse write(GapicContext context, List<GapicClass> clazzes, GapicPackageInfo gapicPackageInfo, String outputFilePath) {
    ByteString.Output output = ByteString.newOutput();
    JavaWriterVisitor codeWriter = new JavaWriterVisitor();
    JarOutputStream jos;
    try {
        jos = new JarOutputStream(output);
    } catch (IOException e) {
        throw new GapicWriterException(e.getMessage(), e);
    }
    for (GapicClass gapicClazz : clazzes) {
        String classPath = writeClazz(gapicClazz, codeWriter, jos);
        writeSamples(gapicClazz, getSamplePackage(gapicClazz), classPath, jos);
    }
    writeMetadataFile(context, writePackageInfo(gapicPackageInfo, codeWriter, jos), jos);
    try {
        jos.finish();
        jos.flush();
    } catch (IOException e) {
        throw new GapicWriterException(e.getMessage(), e);
    }
    CodeGeneratorResponse.Builder response = CodeGeneratorResponse.newBuilder();
    response.setSupportedFeatures(CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL_VALUE).addFileBuilder().setName(outputFilePath).setContentBytes(output.toByteString());
    return response.build();
}
Also used : ByteString(com.google.protobuf.ByteString) GapicClass(com.google.api.generator.gapic.model.GapicClass) JarOutputStream(java.util.jar.JarOutputStream) CodeGeneratorResponse(com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString)

Example 12 with JavaWriterVisitor

use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.

the class ServiceSettingsClassComposerTest method generateServiceClasses.

@Test
public void generateServiceClasses() {
    GapicContext context = RestTestProtoLoader.instance().parseCompliance();
    Service echoProtoService = context.services().get(0);
    GapicClass clazz = ServiceSettingsClassComposer.instance().generate(context, echoProtoService);
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "ComplianceSettings.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "ComplianceSettings.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) Service(com.google.api.generator.gapic.model.Service) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) GapicContext(com.google.api.generator.gapic.model.GapicContext) Test(org.junit.Test)

Example 13 with JavaWriterVisitor

use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.

the class ServiceClientTestClassComposerTest method generateServiceClasses.

@Test
public void generateServiceClasses() {
    GapicContext context = GrpcRestTestProtoLoader.instance().parseShowcaseEcho();
    Service echoProtoService = context.services().get(0);
    GapicClass clazz = ServiceClientTestClassComposer.instance().generate(context, echoProtoService);
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "EchoClientTest.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "EchoClientTest.golden");
    assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) Service(com.google.api.generator.gapic.model.Service) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) GapicContext(com.google.api.generator.gapic.model.GapicContext) Test(org.junit.Test)

Example 14 with JavaWriterVisitor

use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.

the class ResourceNameHelperClassComposerTest method generateResourceNameClass_testingSessionOnePattern.

@Test
public void generateResourceNameClass_testingSessionOnePattern() {
    FileDescriptor testingFileDescriptor = TestingOuterClass.getDescriptor();
    ServiceDescriptor testingService = testingFileDescriptor.getServices().get(0);
    assertEquals(testingService.getName(), "Testing");
    Map<String, Message> messageTypes = Parser.parseMessages(testingFileDescriptor);
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(testingFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    Parser.parseService(testingFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    ResourceName sessionResname = resourceNames.get("showcase.googleapis.com/Session");
    assertThat(outputResourceNames).contains(sessionResname);
    GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(sessionResname, TestProtoLoader.instance().parseShowcaseTesting());
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "SessionName.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "SessionName.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) ResourceName(com.google.api.generator.gapic.model.ResourceName) GapicClass(com.google.api.generator.gapic.model.GapicClass) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with JavaWriterVisitor

use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.

the class ResourceNameHelperClassComposerTest method generateResourceNameClass_echoFoobarMultiplePatterns.

@Test
public void generateResourceNameClass_echoFoobarMultiplePatterns() {
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    ResourceName foobarResname = resourceNames.get("showcase.googleapis.com/Foobar");
    assertThat(outputResourceNames).contains(foobarResname);
    GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(foobarResname, TestProtoLoader.instance().parseShowcaseEcho());
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "FoobarName.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "FoobarName.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) GapicClass(com.google.api.generator.gapic.model.GapicClass) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

JavaWriterVisitor (com.google.api.generator.engine.writer.JavaWriterVisitor)25 Path (java.nio.file.Path)22 GapicClass (com.google.api.generator.gapic.model.GapicClass)21 Test (org.junit.Test)21 GapicContext (com.google.api.generator.gapic.model.GapicContext)15 Service (com.google.api.generator.gapic.model.Service)14 ResourceName (com.google.api.generator.gapic.model.ResourceName)5 Message (com.google.api.generator.gapic.model.Message)4 HashSet (java.util.HashSet)4 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)3 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)3 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)1 Statement (com.google.api.generator.engine.ast.Statement)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 ByteString (com.google.protobuf.ByteString)1 CodeGeneratorResponse (com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse)1