use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposer method composeLroSample.
static Sample composeLroSample(Method method, VariableExpr clientVarExpr, List<VariableExpr> rpcMethodArgVarExprs, List<Expr> bodyExprs) {
// Assign response variable with invoking client's LRO method.
// e.g. if return void, echoClient.waitAsync(ttl).get(); or,
// e.g. if return other type, WaitResponse response = echoClient.waitAsync(ttl).get();
MethodInvocationExpr invokeLroGetMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(String.format("%sAsync", JavaStyle.toLowerCamelCase(method.name()))).setArguments(rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).build();
invokeLroGetMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(invokeLroGetMethodExpr).setMethodName("get").setReturnType(method.lro().responseType()).build();
boolean returnsVoid = method.lro().responseType().isProtoEmptyType();
if (returnsVoid) {
bodyExprs.add(invokeLroGetMethodExpr);
} else {
VariableExpr responseVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("response").setType(method.lro().responseType()).build()).setIsDecl(true).build();
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(responseVarExpr).setValueExpr(invokeLroGetMethodExpr).build());
}
RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setOverloadDisambiguation(SampleComposerUtil.createOverloadDisambiguation(rpcMethodArgVarExprs)).build();
return Sample.builder().setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).setRegionTag(regionTag).build();
}
use of com.google.api.generator.engine.ast.Variable 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.Variable in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_chained.
@Test
public void writeMethodInvocationExpr_chained() {
Variable variable = Variable.builder().setType(TypeNode.INT).setName("libraryClient").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
MethodInvocationExpr firstMethodExpr = MethodInvocationExpr.builder().setMethodName("streamBooksCallable").setExprReferenceExpr(varExpr).build();
MethodInvocationExpr secondMethodExpr = MethodInvocationExpr.builder().setMethodName("doAnotherThing").setExprReferenceExpr(firstMethodExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("call").setExprReferenceExpr(secondMethodExpr).build();
methodExpr.accept(writerVisitor);
assertEquals("libraryClient.streamBooksCallable().doAnotherThing().call()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeCastExpr_nested.
@Test
public void writeCastExpr_nested() {
Variable variable = Variable.builder().setType(TypeNode.STRING).setName("str").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(varExpr).build();
castExpr = CastExpr.builder().setType(TypeNode.STRING).setExpr(castExpr).build();
castExpr.accept(writerVisitor);
assertEquals("((String) ((Object) str))", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.Variable in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeInstanceofExprImports_basic.
@Test
public void writeInstanceofExprImports_basic() {
TypeNode exprType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class));
TypeNode assignExprType = TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class));
Variable variable = Variable.builder().setName("x").setType(assignExprType).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).build();
InstanceofExpr expr = InstanceofExpr.builder().setExpr(variableExpr).setCheckType(exprType).build();
expr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.Expr;\n\n"), writerVisitor.write());
}
Aggregations