use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposer method composeCanonicalSample.
public static Sample composeCanonicalSample(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());
// Create request variable expression and assign with its default value.
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
List<VariableExpr> rpcMethodArgVarExprs = Arrays.asList(requestVarExpr);
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);
List<Statement> bodyStatements = new ArrayList<>();
RegionTag regionTag;
if (method.isPaged()) {
// e.g. echoClient.pagedExpand(request).iterateAll()
Sample unaryPagedRpc = composePagedSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes);
bodyStatements.addAll(unaryPagedRpc.body());
regionTag = unaryPagedRpc.regionTag();
} else if (method.hasLro()) {
Sample unaryLroRpc = composeLroSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
bodyStatements.addAll(unaryLroRpc.body());
regionTag = unaryLroRpc.regionTag();
} else {
// e.g. echoClient.echo(request)
Sample unaryRpc = composeSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
bodyStatements.addAll(unaryRpc.body());
regionTag = unaryRpc.regionTag();
}
List<Statement> body = Arrays.asList(TryCatchStatement.builder().setTryResourceExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)).setTryBody(bodyStatements).setIsSampleCode(true).build());
return Sample.builder().setBody(body).setRegionTag(regionTag).setIsCanonical(true).build();
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.
@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
assignmentExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeForStatement.
@Test
public void writeForStatement() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> body = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr varDeclExpr = createVariableDeclExpr("str", TypeNode.STRING);
Expr collectionExpr = MethodInvocationExpr.builder().setMethodName("getSomeStrings").build();
ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(varDeclExpr).setCollectionExpr(collectionExpr).setBody(body).build();
forStatement.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s", "for (String str : getSomeStrings()) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createOverrideCreateBookMethod.
private MethodDefinition createOverrideCreateBookMethod(VaporReference classRef, Expr bookKindDefaultExpr) {
VariableExpr thisBookKindVariableExpr = createVarExprFromRefVarExpr(bookKindVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
VariableExpr thisSeriesNumVariableExpr = createVarExprFromRefVarExpr(seriesNumVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
AssignmentExpr thisSeriesNumAssignExpr = createAssignmentExpr(thisSeriesNumVariableExpr, VariableExpr.withVariable(seriesNumVar));
AssignmentExpr thisBookKindAssignmentExpr = createAssignmentExpr(thisBookKindVariableExpr, bookKindDefaultExpr);
MethodDefinition overrideCreateBook = MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setArguments(Arrays.asList(createVarDeclExpr(seriesNumVar), createVarDeclExpr(bookKindVar))).setReturnType(TypeNode.VOID).setName("createBook").setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(thisSeriesNumAssignExpr), ExprStatement.withExpr(thisBookKindAssignmentExpr))).build();
return overrideCreateBook;
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createNestedClassShelf.
private ClassDefinition createNestedClassShelf() {
VariableExpr shelfNameDel = createVarPublicDeclExpr(shelfNameVar);
VariableExpr seriesNumDel = createVarPublicDeclExpr(seriesNumVar);
VariableExpr shelfServiceNameDel = createVarPublicDeclExpr(shelfServiceNameVar);
AssignmentExpr shelfServiceNameAssignmentExpr = createAssignmentExpr(shelfServiceNameDel, VariableExpr.withVariable(createVarFromType(TypeNode.STRING, "serviceName")));
MethodDefinition nestedShelfClassCtor = createNestedShelfClassCtor();
return ClassDefinition.builder().setIsNested(true).setMethods(Arrays.asList(nestedShelfClassCtor)).setStatements(Arrays.asList(ExprStatement.withExpr(shelfNameDel), ExprStatement.withExpr(seriesNumDel), ExprStatement.withExpr(shelfServiceNameAssignmentExpr))).setName("Shelf").setScope(ScopeNode.PUBLIC).build();
}
Aggregations