use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposer method composePagedSample.
static Sample composePagedSample(Method method, VariableExpr clientVarExpr, List<VariableExpr> rpcMethodArgVarExprs, List<Expr> bodyExprs, Map<String, Message> messageTypes) {
// Find the repeated field.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
Preconditions.checkNotNull(methodOutputMessage, "Output message %s not found, keys: ", method.outputType().reference().fullName(), messageTypes.keySet().toString());
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();
// For loop paged response item on iterateAll method.
// e.g. for (LogEntry element : loggingServiceV2Client.ListLogs(parent).iterateAll()) {
// //doThingsWith(element);
// }
MethodInvocationExpr clientMethodIterateAllExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(method.name())).setArguments(rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).build();
clientMethodIterateAllExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientMethodIterateAllExpr).setMethodName("iterateAll").setReturnType(repeatedResponseType).build();
ForStatement loopIteratorStatement = ForStatement.builder().setLocalVariableExpr(VariableExpr.builder().setVariable(Variable.builder().setName("element").setType(repeatedResponseType).build()).setIsDecl(true).build()).setCollectionExpr(clientMethodIterateAllExpr).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("doThingsWith(element);")))).build();
List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
bodyExprs.clear();
bodyStatements.add(loopIteratorStatement);
RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setOverloadDisambiguation(SampleComposerUtil.createOverloadDisambiguation(rpcMethodArgVarExprs)).build();
return Sample.builder().setBody(bodyStatements).setRegionTag(regionTag).build();
}
use of com.google.api.generator.engine.ast.Expr 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.Expr in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method createOperationsStubInitExpr.
@Override
protected List<Expr> createOperationsStubInitExpr(Service service, Expr thisExpr, VariableExpr operationsStubClassVarExpr, VariableExpr clientContextVarExpr, VariableExpr callableFactoryVarExpr) {
TypeNode operationsStubType = getTransportOperationsStubType(service);
String standardOpStub = HttpJsonOperationsStub.class.getName();
List<Expr> arguments = new ArrayList<>(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr));
if (standardOpStub.equals(operationsStubType.reference().fullName())) {
arguments.add(TYPE_REGISTRY_VAR_EXPR);
}
return Collections.singletonList(AssignmentExpr.builder().setVariableExpr(operationsStubClassVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(operationsStubType).setMethodName("create").setArguments(arguments).setReturnType(operationsStubClassVarExpr.type()).build()).build());
}
use of com.google.api.generator.engine.ast.Expr 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.Expr in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createStartStaticServerMethod.
@Override
protected MethodDefinition createStartStaticServerMethod(Service service, GapicContext context, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore, String newBuilderMethod) {
VariableExpr mockServiceVarExpr = classMemberVarExprs.get(MOCK_SERVICE_VAR_NAME);
VariableExpr clientVarExpr = classMemberVarExprs.get(CLIENT_VAR_NAME);
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
TypeNode transportStubType = typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
Function<Expr, BiFunction<String, Expr, MethodInvocationExpr>> methodBuilderFn = methodExpr -> (mName, argExpr) -> MethodInvocationExpr.builder().setExprReferenceExpr(methodExpr).setMethodName(mName).setArguments(Arrays.asList(argExpr)).build();
Expr initMockServiceExpr = AssignmentExpr.builder().setVariableExpr(mockServiceVarExpr).setValueExpr(NewObjectExpr.builder().setType(mockServiceVarExpr.type()).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(transportStubType).setMethodName("getMethodDescriptors").build(), MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("getDefaultEndpoint").build()).build()).build();
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(settingsType).build());
Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName(newBuilderMethod).build();
Expr transportChannelProviderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("defaultHttpJsonTransportProviderBuilder").build();
transportChannelProviderExpr = methodBuilderFn.apply(transportChannelProviderExpr).apply("setHttpTransport", mockServiceVarExpr);
transportChannelProviderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(transportChannelProviderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setTransportChannelProvider", transportChannelProviderExpr);
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setCredentialsProvider", MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("NoCredentialsProvider")).setMethodName("create").build());
settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
Expr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderExpr).build();
Expr initClientExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceClientClassName(service))).setMethodName("create").setArguments(Arrays.asList(localSettingsVarExpr)).setReturnType(clientVarExpr.type()).build()).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("BeforeClass")))).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("startStaticServer").setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setIsStatic(true).setBody(Arrays.asList(initMockServiceExpr, initLocalSettingsExpr, initClientExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
Aggregations