Search in sources :

Example 11 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class TypeParserTest method parseMessageType_basic.

// TODO(miraleung): Backfill with more tests (e.g. field, message, methods) for Parser.java.
@Test
public void parseMessageType_basic() {
    FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    ServiceDescriptor echoService = echoFileDescriptor.getServices().get(0);
    assertEquals("Echo", echoService.getName());
    MethodDescriptor echoMethodDescriptor = echoService.getMethods().get(0);
    Reference reference = TypeParser.parseMessageReference(echoMethodDescriptor.getInputType());
    assertEquals("com.google.showcase.v1beta1.EchoRequest", reference.fullName());
}
Also used : ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) Reference(com.google.api.generator.engine.ast.Reference) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Example 12 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeClassDefinition_commentsStatementsAndMethods.

@Test
public void writeClassDefinition_commentsStatementsAndMethods() {
    LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND CLASS");
    JavaDocComment javaDocComment = JavaDocComment.builder().addComment("Class to configure an instance of {@link LibraryServiceStub}.").addParagraph("The default instance has everything set to sensible defaults:").addUnorderedList(Arrays.asList("The default service address (library-example.googleapis.com) and default port" + " (1234) are used.", "Credentials are acquired automatically through Application Default" + " Credentials.", "Retries are configured for idempotent methods but not for non-idempotent" + " methods.")).build();
    List<Reference> subGenerics = Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(MethodDefinition.class));
    Reference mapEntryReference = ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(subGenerics).build();
    List<Reference> generics = Arrays.asList(ConcreteReference.withClazz(ClassDefinition.class), mapEntryReference);
    Reference mapReference = ConcreteReference.builder().setClazz(Map.class).setGenerics(generics).build();
    List<Statement> statements = Arrays.asList(ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).setIsDecl(true).setScope(ScopeNode.PRIVATE).build()), ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapReference))).setIsDecl(true).setScope(ScopeNode.PROTECTED).build()));
    MethodDefinition methodOne = MethodDefinition.builder().setName("open").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setReturnExpr(ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build()).build()).build();
    MethodDefinition methodTwo = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
    List<MethodDefinition> methods = Arrays.asList(methodOne, methodTwo);
    ClassDefinition nestedClassDef = ClassDefinition.builder().setName("IAmANestedClass").setIsNested(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setMethods(Arrays.asList(methodOne)).build();
    ClassDefinition classDef = ClassDefinition.builder().setPackageString("com.google.example.library.v1.stub").setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setName("LibraryServiceStub").setScope(ScopeNode.PUBLIC).setStatements(statements).setMethods(methods).setNestedClasses(Arrays.asList(nestedClassDef)).build();
    classDef.accept(writerVisitor);
    String expected = LineFormatter.lines("package com.google.example.library.v1.stub;\n", "\n", "import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.ClassDefinition;\n", "import com.google.api.generator.engine.ast.MethodDefinition;\n", "import java.util.Map;\n", "\n", "// AUTO-GENERATED DOCUMENTATION AND CLASS\n", "/**\n", " * Class to configure an instance of {{@literal @}link LibraryServiceStub}.\n", " *\n", " * <p>The default instance has everything set to sensible defaults:\n", " *\n", " * <ul>\n", " *   <li>The default service address (library-example.googleapis.com) and default" + " port (1234) are\n", " *       used.\n", " *   <li>Credentials are acquired automatically through Application Default" + " Credentials.\n", " *   <li>Retries are configured for idempotent methods but not for non-idempotent" + " methods.\n", " * </ul>\n", " */\n", "public class LibraryServiceStub {\n", "  private AssignmentExpr x;\n", "  protected Map<ClassDefinition, Map.Entry<String, MethodDefinition>> y;\n\n", "  public boolean open() {\n", "    return true;\n", "  }\n\n", "  public void close() {\n", "    boolean foobar = false;\n", "  }\n", "\n", "  private static class IAmANestedClass {\n\n", "    public boolean open() {\n", "      return true;\n", "    }\n", "  }\n", "}\n");
    assertEquals(expected, writerVisitor.write());
}
Also used : JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) LineComment(com.google.api.generator.engine.ast.LineComment) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_pagedRpcWithNoMethodArguments.

@Test
public void valid_composeShowcaseMethodSample_pagedRpcWithNoMethodArguments() {
    Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("ListContentRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("ListContentResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    List<MethodArgument> arguments = Collections.emptyList();
    Method method = Method.builder().setName("ListContent").setMethodSignatures(Arrays.asList(arguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    Reference repeatedResponseReference = VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_NAME).build();
    Field repeatedField = Field.builder().setName("responses").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(repeatedResponseReference).build())).setIsMessage(true).setIsRepeated(true).build();
    Field nextPagedTokenField = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
    Message listContentResponseMessage = Message.builder().setName("ListContentResponse").setFullProtoName("google.showcase.v1beta1.ListContentResponse").setType(outputType).setFields(Arrays.asList(repeatedField, nextPagedTokenField)).build();
    messageTypes.put("com.google.showcase.v1beta1.ListContentResponse", listContentResponseMessage);
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  for (Content element : echoClient.listContent().iterateAll()) {\n", "    // doThingsWith(element);\n", "  }\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) VaporReference(com.google.api.generator.engine.ast.VaporReference) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Method(com.google.api.generator.gapic.model.Method) Field(com.google.api.generator.gapic.model.Field) List(java.util.List) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 14 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_pagedRpcWithMultipleMethodArguments.

/*Testing composeShowcaseMethodSample*/
@Test
public void valid_composeShowcaseMethodSample_pagedRpcWithMultipleMethodArguments() {
    Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("ListContentRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("ListContentResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode resourceNameType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(ConcreteReference.withClazz(String.class)).build());
    List<MethodArgument> arguments = Arrays.asList(MethodArgument.builder().setName("resourceName").setType(resourceNameType).setField(Field.builder().setName("resourceName").setType(resourceNameType).setIsRepeated(true).build()).build(), MethodArgument.builder().setName("filter").setType(TypeNode.STRING).setField(Field.builder().setName("filter").setType(TypeNode.STRING).build()).build());
    Method method = Method.builder().setName("ListContent").setMethodSignatures(Arrays.asList(arguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    Reference repeatedResponseReference = VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_NAME).build();
    Field repeatedField = Field.builder().setName("responses").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(repeatedResponseReference).build())).setIsMessage(true).setIsRepeated(true).build();
    Field nextPagedTokenField = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
    Message listContentResponseMessage = Message.builder().setName("ListContentResponse").setFullProtoName("google.showcase.v1beta1.ListContentResponse").setType(outputType).setFields(Arrays.asList(repeatedField, nextPagedTokenField)).build();
    messageTypes.put("com.google.showcase.v1beta1.ListContentResponse", listContentResponseMessage);
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  List<String> resourceName = new ArrayList<>();\n", "  String filter = \"filter-1274492040\";\n", "  for (Content element : echoClient.listContent(resourceName, filter).iterateAll())" + " {\n", "    // doThingsWith(element);\n", "  }\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) VaporReference(com.google.api.generator.engine.ast.VaporReference) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Method(com.google.api.generator.gapic.model.Method) Field(com.google.api.generator.gapic.model.Field) List(java.util.List) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 15 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeTryCatchStatement_sampleCodeWithCatch.

@Test
public void writeTryCatchStatement_sampleCodeWithCatch() {
    Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
    TypeNode type = TypeNode.withReference(exceptionReference);
    VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
    TryCatchStatement tryCatch = TryCatchStatement.builder().setIsSampleCode(true).setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))).addCatch(variableExpr, Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))).build();
    tryCatch.accept(writerVisitor);
    assertEquals(String.format("%s%s%s%s%s", "try (boolean aBool = false) {\n", "int y = 4;\n", "} catch (IllegalArgumentException e) {\n", "int foobar = 123;\n", "}\n"), writerVisitor.write());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Aggregations

Reference (com.google.api.generator.engine.ast.Reference)29 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)27 TypeNode (com.google.api.generator.engine.ast.TypeNode)21 VaporReference (com.google.api.generator.engine.ast.VaporReference)21 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)17 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)13 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)12 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)12 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)11 Expr (com.google.api.generator.engine.ast.Expr)11 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)11 Statement (com.google.api.generator.engine.ast.Statement)11 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)11 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)10 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)10 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)10 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)10 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)10