use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposer method composeEmptyServiceSample.
// Creates an example for an empty service (no API methods), which is a corner case but can
// happen. Generated example will only show how to instantiate the client class but will not call
// any API methods (because there are no API methods).
public static Sample composeEmptyServiceSample(TypeNode clientType) {
VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
List<Statement> bodyStatements = new ArrayList<>();
RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName("emtpy").build();
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.Statement 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.Statement in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createRpcExceptionTestMethod.
@Override
protected MethodDefinition createRpcExceptionTestMethod(Method method, Service service, List<MethodArgument> methodSignature, int variantIndex, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
VariableExpr exceptionVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_REST_TYPESTORE.get("ApiException")).setName("exception").build());
// First two assignment lines.
Expr exceptionAssignExpr = AssignmentExpr.builder().setVariableExpr(exceptionVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setMethodName("createException").setStaticReferenceType(FIXED_REST_TYPESTORE.get("ApiExceptionFactory")).setArguments(NewObjectExpr.withType(FIXED_REST_TYPESTORE.get("Exception")), MethodInvocationExpr.builder().setMethodName("of").setStaticReferenceType(FIXED_REST_TYPESTORE.get("FakeStatusCode")).setArguments(EnumRefExpr.builder().setName("INVALID_ARGUMENT").setType(FIXED_REST_TYPESTORE.get("Code")).build()).build(), ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build())).setReturnType(exceptionVarExpr.type()).build()).build();
Expr addExceptionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).setMethodName("addException").setArguments(exceptionVarExpr).build();
// Try-catch block. Build the method call.
String exceptionTestMethodName = String.format("%sExceptionTest%s", JavaStyle.toLowerCamelCase(method.name()), variantIndex > 0 ? variantIndex + 1 : "");
boolean isStreaming = !method.stream().equals(Method.Stream.NONE);
List<Statement> methodBody = new ArrayList<>();
methodBody.add(ExprStatement.withExpr(exceptionAssignExpr));
methodBody.add(ExprStatement.withExpr(addExceptionExpr));
if (isStreaming) {
methodBody.addAll(createStreamingRpcExceptionTestStatements(method, classMemberVarExprs, resourceNames, messageTypes));
} else {
methodBody.addAll(createRpcExceptionTestStatements(method, methodSignature, classMemberVarExprs, resourceNames, messageTypes));
}
return MethodDefinition.builder().setAnnotations(Arrays.asList(TEST_ANNOTATION)).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(exceptionTestMethodName).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))).setBody(methodBody).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createRpcExceptionTestMethod.
@Override
protected MethodDefinition createRpcExceptionTestMethod(Method method, Service service, List<MethodArgument> methodSignature, int variantIndex, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
VariableExpr exceptionVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_GRPC_TYPESTORE.get("StatusRuntimeException")).setName("exception").build());
// First two assignment lines.
Expr exceptionAssignExpr = AssignmentExpr.builder().setVariableExpr(exceptionVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_GRPC_TYPESTORE.get("StatusRuntimeException")).setArguments(EnumRefExpr.builder().setType(GRPC_STATUS_TYPE).setName("INVALID_ARGUMENT").build()).build()).build();
Expr addExceptionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).setMethodName("addException").setArguments(exceptionVarExpr).build();
// Try-catch block. Build the method call.
String exceptionTestMethodName = String.format("%sExceptionTest%s", JavaStyle.toLowerCamelCase(method.name()), variantIndex > 0 ? variantIndex + 1 : "");
boolean isStreaming = !method.stream().equals(Method.Stream.NONE);
List<Statement> methodBody = new ArrayList<>();
methodBody.add(ExprStatement.withExpr(exceptionAssignExpr));
methodBody.add(ExprStatement.withExpr(addExceptionExpr));
if (isStreaming) {
methodBody.addAll(createStreamingRpcExceptionTestStatements(method, classMemberVarExprs, resourceNames, messageTypes));
} else {
methodBody.addAll(createRpcExceptionTestStatements(method, methodSignature, classMemberVarExprs, resourceNames, messageTypes));
}
return MethodDefinition.builder().setAnnotations(Arrays.asList(TEST_ANNOTATION)).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(exceptionTestMethodName).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))).setBody(methodBody).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method createForStatement.
private static ForStatement createForStatement() {
Expr collectionExpr = MethodInvocationExpr.builder().setMethodName("getSomeStrings").build();
ExprStatement assignExprStatement = ExprStatement.withExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN));
List<Statement> body = Arrays.asList(assignExprStatement);
return ForStatement.builder().setLocalVariableExpr(createVariableDeclExpr("str", TypeNode.STRING)).setCollectionExpr(collectionExpr).setBody(body).build();
}
Aggregations