Search in sources :

Example 6 with CommentStatement

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

the class JavaWriterVisitorTest method writeBlockCommentStatement_basic.

/**
 * =============================== COMMENT ===============================
 */
@Test
public void writeBlockCommentStatement_basic() {
    String content = "this is a test comment";
    BlockComment blockComment = BlockComment.builder().setComment(content).build();
    CommentStatement commentStatement = CommentStatement.withComment(blockComment);
    String expected = LineFormatter.lines("/*\n", "* this is a test comment\n", "*/\n");
    commentStatement.accept(writerVisitor);
    assertEquals(expected, writerVisitor.write());
}
Also used : BlockComment(com.google.api.generator.engine.ast.BlockComment) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) Test(org.junit.Test)

Example 7 with CommentStatement

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

the class JavaWriterVisitorTest method writeClassDefinition_basicWithFileHeader.

@Test
public void writeClassDefinition_basicWithFileHeader() {
    List<CommentStatement> fileHeader = Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License")));
    ClassDefinition classDef = ClassDefinition.builder().setFileHeader(fileHeader).setPackageString("com.google.example.library.v1.stub").setName("LibraryServiceStub").setScope(ScopeNode.PUBLIC).build();
    classDef.accept(writerVisitor);
    assertEquals(LineFormatter.lines("/*\n", " * Apache License\n", " */\n\n", "package com.google.example.library.v1.stub;\n", "\n", "public class LibraryServiceStub {}\n"), writerVisitor.write());
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Test(org.junit.Test)

Example 8 with CommentStatement

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

the class ServiceClientCallableMethodSampleComposer method composePagedCallableMethod.

// Compose sample code for the method where it is CallableMethodKind.PAGED.
public static Sample composePagedCallableMethod(Method method, TypeNode clientType, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
    VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
    // Assign method's request variable with the default value.
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
    Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
    Preconditions.checkNotNull(requestMessage, String.format("Could not find the message type %s.", method.inputType().reference().fullName()));
    Expr requestBuilderExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes);
    AssignmentExpr requestAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(requestBuilderExpr).build();
    List<Expr> bodyExprs = new ArrayList<>();
    bodyExprs.add(requestAssignmentExpr);
    // Find the repeated field.
    Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
    Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapPaginatedRepeatedField();
    Preconditions.checkNotNull(repeatedPagedResultsField, String.format("No repeated field found on message %s for method %s", methodOutputMessage.name(), method.name()));
    TypeNode repeatedResponseType = repeatedPagedResultsField.type();
    // Create ApiFuture Variable Expression with assign value by invoking client paged callable
    // method.
    // e.g. ApiFuture<ListExclusionsPagedResponse> future =
    // configServiceV2Client.listExclusionsPagedCallable().futureCall(request);
    TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(repeatedResponseType.reference()).build());
    VariableExpr apiFutureVarExpr = VariableExpr.withVariable(Variable.builder().setName("future").setType(apiFutureType).build());
    MethodInvocationExpr pagedCallableFutureMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(String.format("%sPagedCallable", JavaStyle.toLowerCamelCase(method.name()))).build();
    pagedCallableFutureMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedCallableFutureMethodExpr).setMethodName("futureCall").setArguments(requestVarExpr).setReturnType(apiFutureType).build();
    AssignmentExpr apiFutureAssignmentExpr = AssignmentExpr.builder().setVariableExpr(apiFutureVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(pagedCallableFutureMethodExpr).build();
    bodyExprs.add(apiFutureAssignmentExpr);
    List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
    bodyExprs.clear();
    // Add line comment
    bodyStatements.add(CommentStatement.withComment(LineComment.withComment("Do something.")));
    // For-loop on repeated response element
    // e.g. for (ListExclusionsResponse element : future.get().iterateAll()) {
    // // doThingsWith(element);
    // }
    VariableExpr repeatedResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("element").setType(repeatedResponseType).build());
    MethodInvocationExpr futureGetIterateAllMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(apiFutureVarExpr).setMethodName("get").build();
    futureGetIterateAllMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(futureGetIterateAllMethodExpr).setMethodName("iterateAll").setReturnType(repeatedResponseType).build();
    CommentStatement lineCommentStatement = CommentStatement.withComment(LineComment.withComment("doThingsWith(element);"));
    ForStatement repeatedResponseForStatement = ForStatement.builder().setLocalVariableExpr(repeatedResponseVarExpr.toBuilder().setIsDecl(true).build()).setCollectionExpr(futureGetIterateAllMethodExpr).setBody(Arrays.asList(lineCommentStatement)).build();
    bodyStatements.add(repeatedResponseForStatement);
    List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
    RegionTag regionTag = RegionTag.builder().setServiceName(clientType.reference().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) OperationFuture(com.google.api.gax.longrunning.OperationFuture) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) Operation(com.google.longrunning.Operation) Variable(com.google.api.generator.engine.ast.Variable) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Strings(com.google.common.base.Strings) Expr(com.google.api.generator.engine.ast.Expr) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) Method(com.google.api.generator.gapic.model.Method) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ServerStream(com.google.api.gax.rpc.ServerStream) LineComment(com.google.api.generator.engine.ast.LineComment) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Sample(com.google.api.generator.gapic.model.Sample) Collectors(java.util.stream.Collectors) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ApiFuture(com.google.api.core.ApiFuture) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) Statement(com.google.api.generator.engine.ast.Statement) List(java.util.List) BidiStream(com.google.api.gax.rpc.BidiStream) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) Message(com.google.api.generator.gapic.model.Message) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Message(com.google.api.generator.gapic.model.Message) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) RegionTag(com.google.api.generator.gapic.model.RegionTag) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Field(com.google.api.generator.gapic.model.Field) ApiFuture(com.google.api.core.ApiFuture) Expr(com.google.api.generator.engine.ast.Expr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ForStatement(com.google.api.generator.engine.ast.ForStatement)

Example 9 with CommentStatement

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

the class JavaWriterVisitorTest method writeJavaDocCommentStatement_allComponents.

@Test
public void writeJavaDocCommentStatement_allComponents() {
    String content = "this is a test comment";
    String deprecatedText = "Use the {@link ArchivedBookName} class instead.";
    String paramName = "shelfName";
    String paramDescription = "The name of the shelf where books are published to.";
    String paragraph1 = "This class provides the ability to make remote calls to the backing service through" + " method calls that map to API methods. Sample code to get started:";
    String paragraph2 = "The surface of this class includes several types of Java methods for each of the API's" + " methods:";
    String sampleCode = createSampleCode();
    List<String> orderedlList = Arrays.asList("A flattened method.", " A request object method.", "A callable method.");
    String throwsType = "com.google.api.gax.rpc.ApiException";
    String throwsDescription = "if the remote call fails.";
    JavaDocComment javaDocComment = JavaDocComment.builder().addComment(content).addParagraph(paragraph1).addSampleCode(sampleCode).addParagraph(paragraph2).addOrderedList(orderedlList).addSampleCode(sampleCode).addParam(paramName, paramDescription).setThrows(throwsType, throwsDescription).setDeprecated(deprecatedText).build();
    CommentStatement commentStatement = CommentStatement.withComment(javaDocComment);
    String expected = LineFormatter.lines("/**\n", "* this is a test comment\n", "* <p> This class provides the ability to make remote calls to the backing service" + " through method calls that map to API methods. Sample code to get started:\n", "* <pre>{@code\n", "* try (boolean condition = false) {\n", "* int x = 3;\n", "* }\n", "* }</pre>\n", "* <p> The surface of this class includes several types of Java methods for each of" + " the API's methods:\n", "* <ol>\n", "* <li> A flattened method.\n", "* <li>  A request object method.\n", "* <li> A callable method.\n", "* </ol>\n", "* <pre>{@code\n", "* try (boolean condition = false) {\n", "* int x = 3;\n", "* }\n", "* }</pre>\n", "* @param shelfName The name of the shelf where books are published to.\n", "* @throws com.google.api.gax.rpc.ApiException if the remote call fails.\n", "* @deprecated Use the {@link ArchivedBookName} class instead.\n", "*/\n");
    commentStatement.accept(writerVisitor);
    assertEquals(expected, writerVisitor.write());
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Test(org.junit.Test)

Aggregations

CommentStatement (com.google.api.generator.engine.ast.CommentStatement)9 Sample (com.google.api.generator.gapic.model.Sample)4 ArrayList (java.util.ArrayList)4 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)3 JavaDocComment (com.google.api.generator.engine.ast.JavaDocComment)3 Test (org.junit.Test)3 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)2 LineComment (com.google.api.generator.engine.ast.LineComment)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)2 TypeStore (com.google.api.generator.gapic.composer.store.TypeStore)2 Message (com.google.api.generator.gapic.model.Message)2 Preconditions (com.google.common.base.Preconditions)2 Strings (com.google.common.base.Strings)2 ApiFuture (com.google.api.core.ApiFuture)1 OperationFuture (com.google.api.gax.longrunning.OperationFuture)1 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 BidiStream (com.google.api.gax.rpc.BidiStream)1 ServerStream (com.google.api.gax.rpc.ServerStream)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1