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();
}
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();
}
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());
}
Aggregations