Search in sources :

Example 1 with WhileStatement

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

the class ServiceClientCallableMethodSampleComposer method composePagedCallableSample.

private static Sample composePagedCallableSample(Method method, VariableExpr clientVarExpr, VariableExpr requestVarExpr, Map<String, Message> messageTypes) {
    // 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();
    // Assign future variable by invoking paged callable method.
    // e.g. ApiFuture<PagedExpandPagedResponse> future =
    // echoClient.pagedExpandCallable().futureCall(request);
    VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(method.outputType()).build());
    MethodInvocationExpr pagedCallableMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sCallable", method.name()))).build();
    pagedCallableMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(pagedCallableMethodInvocationExpr).setMethodName("call").setArguments(requestVarExpr).setReturnType(method.outputType()).build();
    AssignmentExpr responseAssignmentExpr = AssignmentExpr.builder().setVariableExpr(responseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(pagedCallableMethodInvocationExpr).build();
    List<Statement> whileBodyStatements = new ArrayList<>();
    whileBodyStatements.add(ExprStatement.withExpr(responseAssignmentExpr));
    // For-loop on repeated response elements.
    // e.g. for (EchoResponse element : response.getResponsesList()) {
    // // doThingsWith(element);
    // }
    VariableExpr repeatedResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("element").setType(repeatedResponseType).build());
    MethodInvocationExpr getResponseListMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName("getResponsesList").build();
    ForStatement responseForStatements = ForStatement.builder().setLocalVariableExpr(repeatedResponseVarExpr.toBuilder().setIsDecl(true).build()).setCollectionExpr(getResponseListMethodInvocationExpr).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("doThingsWith(element);")))).build();
    whileBodyStatements.add(responseForStatements);
    // Create nextPageToken variable expression and assign it with a value by invoking
    // getNextPageToken method.
    // e.g. String nextPageToken = response.getNextPageToken();
    VariableExpr nextPageTokenVarExpr = VariableExpr.withVariable(Variable.builder().setName("nextPageToken").setType(TypeNode.STRING).build());
    MethodInvocationExpr getNextPageTokenMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseVarExpr).setMethodName("getNextPageToken").setReturnType(TypeNode.STRING).build();
    AssignmentExpr nextPageTokenAssignmentExpr = AssignmentExpr.builder().setVariableExpr(nextPageTokenVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(getNextPageTokenMethodInvocationExpr).build();
    whileBodyStatements.add(ExprStatement.withExpr(nextPageTokenAssignmentExpr));
    // If nextPageToken variable expression is not null or empty, assign request variable with a
    // value by invoking setPageToken method.
    // if (!Strings.isNullOrEmpty(nextPageToken)) {
    // request =  request.toBuilder().setPageToken(nextPageToken).build();
    // } else {
    // break;
    // }
    Expr conditionExpr = UnaryOperationExpr.logicalNotWithExpr(MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Strings.class))).setMethodName("isNullOrEmpty").setArguments(nextPageTokenVarExpr).setReturnType(TypeNode.BOOLEAN).build());
    MethodInvocationExpr setPageTokenMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr).setMethodName("toBuilder").build();
    setPageTokenMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(setPageTokenMethodInvocationExpr).setMethodName("setPageToken").setArguments(nextPageTokenVarExpr).build();
    setPageTokenMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(setPageTokenMethodInvocationExpr).setMethodName("build").setReturnType(method.inputType()).build();
    AssignmentExpr requestReAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr).setValueExpr(setPageTokenMethodInvocationExpr).build();
    IfStatement nextPageTokenIfStatement = IfStatement.builder().setConditionExpr(conditionExpr).setBody(Arrays.asList(ExprStatement.withExpr(requestReAssignmentExpr))).setElseBody(Arrays.asList(BreakStatement.create())).build();
    whileBodyStatements.add(nextPageTokenIfStatement);
    WhileStatement pagedWhileStatement = WhileStatement.builder().setConditionExpr(ValueExpr.withValue(PrimitiveValue.builder().setValue("true").setType(TypeNode.BOOLEAN).build())).setBody(whileBodyStatements).build();
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setOverloadDisambiguation("Paged").setIsAsynchronous(true).build();
    return Sample.builder().setBody(Arrays.asList(pagedWhileStatement)).setRegionTag(regionTag).build();
}
Also used : Message(com.google.api.generator.gapic.model.Message) 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) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) Strings(com.google.common.base.Strings) Field(com.google.api.generator.gapic.model.Field) IfStatement(com.google.api.generator.engine.ast.IfStatement) 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 2 with WhileStatement

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

the class JavaCodeGeneratorTest method createAddBooksContainsNovel.

private MethodDefinition createAddBooksContainsNovel() {
    ConcreteReference bookKindStackRef = ConcreteReference.builder().setClazz(Stack.class).setGenerics(Arrays.asList(bookKindRef)).build();
    Variable bookKindStackVar = createVarFromConcreteRef(bookKindStackRef, "stack");
    Variable containsNovelVar = createVarFromType(TypeNode.BOOLEAN, "containsNovel");
    Variable bookVar = createVarFromVaporRef(bookClassRef, "addedBook");
    TernaryExpr ternaryExpr = createTernaryExpr(containsNovelVar);
    AssignmentExpr setContainsNovelToFalse = createAssignmentExpr(createVarDeclExpr(containsNovelVar), ValueExpr.withValue(createBooleanValue("false")));
    MethodInvocationExpr stackIsEmpty = MethodInvocationExpr.builder().setMethodName("isEmpty").setExprReferenceExpr(VariableExpr.withVariable(bookKindStackVar)).setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr stackPop = methodExprWithRef(bookKindStackVar, "pop");
    MethodInvocationExpr addBookToShelfMethod = MethodInvocationExpr.builder().setMethodName("addBookToShelf").setArguments(stackPop, VariableExpr.withVariable(shelfVar)).setReturnType(TypeNode.withReference(bookClassRef)).build();
    AssignmentExpr createNewAddedBook = createAssignmentExpr(createVarDeclExpr(bookVar), addBookToShelfMethod);
    InstanceofExpr addedBookIsNovelInstance = InstanceofExpr.builder().setExpr(VariableExpr.withVariable(bookVar)).setCheckType(TypeNode.withReference(novelClassRef)).build();
    AssignmentExpr setContainsNovelToTrue = createAssignmentExpr(VariableExpr.withVariable(containsNovelVar), ValueExpr.withValue(createBooleanValue("true")));
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(addedBookIsNovelInstance).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToTrue))).build();
    // TODO: update the conditionExpr from `stack.isEmpty()` to `!stack.isEmpty()`
    WhileStatement whileStatement = WhileStatement.builder().setConditionExpr(stackIsEmpty).setBody(Arrays.asList(ExprStatement.withExpr(createNewAddedBook), ifStatement)).build();
    return MethodDefinition.builder().setHeaderCommentStatements(createPreMethodJavaDocComment()).setArguments(Arrays.asList(createVarDeclExpr(shelfVar), createVarDeclExpr(bookKindStackVar))).setName("addBooksContainsNovel").setReturnType(TypeNode.STRING).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToFalse), whileStatement)).setReturnExpr(ternaryExpr).build();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr)

Example 3 with WhileStatement

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

the class JavaWriterVisitorTest method writeWhileStatement_simple.

@Test
public void writeWhileStatement_simple() {
    AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
    Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
    List<Statement> whileBody = Arrays.asList(assignExprStatement, assignExprStatement);
    VariableExpr condExpr = createVariableExpr("condition", TypeNode.BOOLEAN);
    WhileStatement whileStatement = WhileStatement.builder().setConditionExpr(condExpr).setBody(whileBody).build();
    whileStatement.accept(writerVisitor);
    assertEquals(LineFormatter.lines("while (condition) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Also used : 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) Test(org.junit.Test)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)3 IfStatement (com.google.api.generator.engine.ast.IfStatement)3 WhileStatement (com.google.api.generator.engine.ast.WhileStatement)3 BreakStatement (com.google.api.generator.engine.ast.BreakStatement)2 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 ForStatement (com.google.api.generator.engine.ast.ForStatement)2 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)2 Statement (com.google.api.generator.engine.ast.Statement)2 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)2 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)2 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)1 BlockStatement (com.google.api.generator.engine.ast.BlockStatement)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 EmptyLineStatement (com.google.api.generator.engine.ast.EmptyLineStatement)1 Expr (com.google.api.generator.engine.ast.Expr)1 GeneralForStatement (com.google.api.generator.engine.ast.GeneralForStatement)1 InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)1 SynchronizedStatement (com.google.api.generator.engine.ast.SynchronizedStatement)1 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)1