use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class GrpcServiceStubClassComposer method createRequestParamsExtractorClassInstance.
private LambdaExpr createRequestParamsExtractorClassInstance(Method method, List<Statement> classStatements) {
List<Statement> bodyStatements = new ArrayList<>();
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
TypeNode returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference()).build());
MethodInvocationExpr.Builder returnExpr = MethodInvocationExpr.builder().setReturnType(returnType);
// the implicit routing headers specified in the google.api.http annotation should not be sent
if (method.routingHeaderRule() == null) {
createRequestParamsExtractorBodyForHttpBindings(method, requestVarExpr, bodyStatements, returnExpr);
} else {
createRequestParamsExtractorBodyForRoutingHeaders(method, requestVarExpr, classStatements, bodyStatements, returnExpr);
}
// https://github.com/googleapis/gax-java/blob/8d45d186e36ae97b789a6f89d80ae5213a773b65/gax/src/main/java/com/google/api/gax/rpc/RequestParamsExtractor.java#L55
return LambdaExpr.builder().setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(returnExpr.build()).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposer method composeShowcaseMethodSample.
public static Sample composeShowcaseMethodSample(Method method, TypeNode clientType, List<MethodArgument> arguments, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(clientType.reference().name())).setType(clientType).build());
// Assign method's arguments variable with the default values.
List<VariableExpr> rpcMethodArgVarExprs = createArgumentVariableExprs(arguments);
List<Expr> rpcMethodArgDefaultValueExprs = createArgumentDefaultValueExprs(arguments, resourceNames);
List<Expr> rpcMethodArgAssignmentExprs = createAssignmentsForVarExprsWithValueExprs(rpcMethodArgVarExprs, rpcMethodArgDefaultValueExprs);
List<Expr> bodyExprs = new ArrayList<>();
bodyExprs.addAll(rpcMethodArgAssignmentExprs);
List<Statement> bodyStatements = new ArrayList<>();
RegionTag regionTag;
if (method.isPaged()) {
Sample unaryPagedRpc = ServiceClientMethodSampleComposer.composePagedSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes);
bodyStatements.addAll(unaryPagedRpc.body());
regionTag = unaryPagedRpc.regionTag();
} else if (method.hasLro()) {
Sample unaryLroRpc = ServiceClientMethodSampleComposer.composeLroSample(method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs);
bodyStatements.addAll(unaryLroRpc.body());
regionTag = unaryLroRpc.regionTag();
} else {
// e.g. echoClient.echo(), echoClient.echo(...)
Sample unaryRpc = ServiceClientMethodSampleComposer.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).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposer method composeSetCredentialsSample.
public static Sample composeSetCredentialsSample(TypeNode clientType, TypeNode settingsType) {
// Initialize clientSettings with builder() method.
// e.g. EchoSettings echoSettings =
// EchoSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create("myCredentials")).build();
String settingsName = JavaStyle.toLowerCamelCase(settingsType.reference().name());
String clientName = JavaStyle.toLowerCamelCase(clientType.reference().name());
TypeNode myCredentialsType = TypeNode.withReference(VaporReference.builder().setName("myCredentials").setPakkage(clientType.reference().pakkage()).build());
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(settingsName).setType(settingsType).build());
MethodInvocationExpr newBuilderMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
TypeNode fixedCredentialProvideType = TypeNode.withReference(ConcreteReference.withClazz(FixedCredentialsProvider.class));
MethodInvocationExpr credentialArgExpr = MethodInvocationExpr.builder().setStaticReferenceType(fixedCredentialProvideType).setArguments(VariableExpr.withVariable(Variable.builder().setName("myCredentials").setType(myCredentialsType).build())).setMethodName("create").build();
MethodInvocationExpr credentialsMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderMethodExpr).setArguments(credentialArgExpr).setMethodName("setCredentialsProvider").build();
MethodInvocationExpr buildMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(credentialsMethodExpr).setReturnType(settingsType).setMethodName("build").build();
Expr initSettingsVarExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(buildMethodExpr).build();
// Initialized client with create() method.
// e.g. EchoClient echoClient = EchoClient.create(echoSettings);
VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(clientName).setType(clientType).build());
MethodInvocationExpr createMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(clientType).setArguments(settingsVarExpr).setMethodName("create").setReturnType(clientType).build();
String rpcName = createMethodExpr.methodIdentifier().name();
Expr initClientVarExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createMethodExpr).build();
List<Statement> sampleBody = Arrays.asList(ExprStatement.withExpr(initSettingsVarExpr), ExprStatement.withExpr(initClientVarExpr));
RegionTag regionTag = RegionTag.builder().setServiceName(clientName).setRpcName(rpcName).setOverloadDisambiguation("setCredentialsProvider").build();
return Sample.builder().setBody(sampleBody).setRegionTag(regionTag).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class SettingsSampleComposer method composeSettingsSample.
public static Optional<Sample> composeSettingsSample(Optional<String> methodNameOpt, String settingsClassName, TypeNode classType) {
if (!methodNameOpt.isPresent()) {
return Optional.empty();
}
// Initialize services settingsBuilder with newBuilder()
// e.g. FoobarSettings.Builder foobarSettingsBuilder = FoobarSettings.newBuilder();
TypeNode builderType = TypeNode.withReference(VaporReference.builder().setEnclosingClassNames(classType.reference().name()).setName("Builder").setPakkage(classType.reference().pakkage()).build());
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(String.format("%sBuilder", settingsClassName))).setType(builderType).build());
MethodInvocationExpr settingsBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setStaticReferenceType(classType).setMethodName("newBuilder").setReturnType(builderType).build();
AssignmentExpr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderMethodInvocationExpr).build();
// Builder with set value method
// e.g foobarSettingBuilder.fooSetting().setRetrySettings(
// echoSettingsBuilder.echoSettings().getRetrySettings().toBuilder().setTotalTimeout(Duration.ofSeconds(30)).build());
MethodInvocationExpr settingBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(localSettingsVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sSettings", methodNameOpt.get()))).build();
String disambiguation = "Settings";
MethodInvocationExpr retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingBuilderMethodInvocationExpr).setMethodName("getRetrySettings").build();
retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("toBuilder").build();
MethodInvocationExpr ofSecondMethodInvocationExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Duration.class))).setMethodName("ofSeconds").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("30").build())).build();
retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("setTotalTimeout").setArguments(ofSecondMethodInvocationExpr).build();
retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("build").build();
settingBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingBuilderMethodInvocationExpr).setMethodName("setRetrySettings").setArguments(retrySettingsArgExpr).build();
// Initialize clientSetting with builder() method.
// e.g: Foobar<Stub>Settings foobarSettings = foobarSettingsBuilder.build();
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setType(classType).setName(JavaStyle.toLowerCamelCase(settingsClassName)).build());
AssignmentExpr settingBuildAssignmentExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(localSettingsVarExpr).setMethodName("build").setReturnType(classType).build()).build();
List<Statement> statements = Arrays.asList(initLocalSettingsExpr, settingBuilderMethodInvocationExpr, settingBuildAssignmentExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
RegionTag regionTag = RegionTag.builder().setServiceName(classType.reference().name()).setRpcName(methodNameOpt.get()).build();
return Optional.of(Sample.builder().setBody(statements).setRegionTag(regionTag).build());
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createStreamingRpcTestMethod.
@Override
protected MethodDefinition createStreamingRpcTestMethod(Service service, Method method, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
TypeNode methodOutputType = method.hasLro() ? method.lro().responseType() : method.outputType();
List<Expr> methodExprs = new ArrayList<>();
VariableExpr expectedResponseVarExpr = VariableExpr.withVariable(Variable.builder().setType(methodOutputType).setName("expectedResponse").build());
Expr expectedResponseValExpr = null;
if (messageTypes.containsKey(methodOutputType.reference().fullName())) {
expectedResponseValExpr = DefaultValueComposer.createSimpleMessageBuilderValue(messageTypes.get(methodOutputType.reference().fullName()), resourceNames, messageTypes);
} else {
// Wrap this in a field so we don't have to split the helper into lots of different methods,
// or duplicate it for VariableExpr.
expectedResponseValExpr = DefaultValueComposer.createValue(Field.builder().setType(methodOutputType).setIsMessage(true).setName("expectedResponse").build());
}
methodExprs.add(AssignmentExpr.builder().setVariableExpr(expectedResponseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(expectedResponseValExpr).build());
String mockServiceVarName = getMockServiceVarName(service);
if (method.hasLro()) {
VariableExpr resultOperationVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_GRPC_TYPESTORE.get("Operation")).setName("resultOperation").build());
methodExprs.add(AssignmentExpr.builder().setVariableExpr(resultOperationVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(DefaultValueComposer.createSimpleOperationBuilderValue(String.format("%sTest", JavaStyle.toLowerCamelCase(method.name())), expectedResponseVarExpr)).build());
methodExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(mockServiceVarName)).setMethodName("addResponse").setArguments(resultOperationVarExpr).build());
} else {
methodExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(mockServiceVarName)).setMethodName("addResponse").setArguments(expectedResponseVarExpr).build());
}
// Construct the request or method arguments.
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Expr valExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes);
methodExprs.add(AssignmentExpr.builder().setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(valExpr).build());
List<Statement> methodStatements = new ArrayList<>();
methodStatements.addAll(methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();
methodStatements.add(EMPTY_LINE_STATEMENT);
// Construct the mock stream observer.
VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(FIXED_GRPC_TYPESTORE.get("MockStreamObserver").reference().copyAndSetGenerics(Arrays.asList(method.outputType().reference())))).setName("responseObserver").build());
methodStatements.add(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(responseObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_GRPC_TYPESTORE.get("MockStreamObserver")).setIsGeneric(true).build()).build()));
methodStatements.add(EMPTY_LINE_STATEMENT);
// Build the callable variable and assign it.
VariableExpr callableVarExpr = VariableExpr.withVariable(Variable.builder().setType(getCallableType(method)).setName("callable").build());
Expr streamingCallExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get("client")).setMethodName(String.format("%sCallable", JavaStyle.toLowerCamelCase(method.name()))).setReturnType(callableVarExpr.type()).build();
methodExprs.add(AssignmentExpr.builder().setVariableExpr(callableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(streamingCallExpr).build());
// Call the streaming-variant callable method.
if (method.stream().equals(Method.Stream.SERVER)) {
methodExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(callableVarExpr).setMethodName("serverStreamingCall").setArguments(requestVarExpr, responseObserverVarExpr).build());
} else {
VariableExpr requestObserverVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(FIXED_TYPESTORE.get("ApiStreamObserver").reference().copyAndSetGenerics(Arrays.asList(method.inputType().reference())))).setName("requestObserver").build());
List<Expr> callableMethodArgs = new ArrayList<>();
if (!method.stream().equals(Method.Stream.BIDI) && !method.stream().equals(Method.Stream.CLIENT)) {
callableMethodArgs.add(requestVarExpr);
}
callableMethodArgs.add(responseObserverVarExpr);
methodExprs.add(AssignmentExpr.builder().setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(callableVarExpr).setMethodName(getCallableMethodName(method)).setArguments(callableMethodArgs).setReturnType(requestObserverVarExpr.type()).build()).build());
methodStatements.addAll(methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();
methodStatements.add(EMPTY_LINE_STATEMENT);
methodExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(requestObserverVarExpr).setMethodName("onNext").setArguments(requestVarExpr).build());
methodExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(requestObserverVarExpr).setMethodName("onCompleted").build());
}
methodStatements.addAll(methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();
methodStatements.add(EMPTY_LINE_STATEMENT);
// Check the actual responses.
VariableExpr actualResponsesVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).setName("actualResponses").build());
Expr getFutureResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseObserverVarExpr).setMethodName("future").build();
getFutureResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getFutureResponseExpr).setMethodName("get").setReturnType(actualResponsesVarExpr.type()).build();
methodExprs.add(AssignmentExpr.builder().setVariableExpr(actualResponsesVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(getFutureResponseExpr).build());
// Assert the size is equivalent.
methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build()), MethodInvocationExpr.builder().setExprReferenceExpr(actualResponsesVarExpr).setMethodName("size").setReturnType(TypeNode.INT).build()).build());
// Assert the responses are equivalent.
Expr zeroExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build());
Expr actualResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(actualResponsesVarExpr).setMethodName("get").setArguments(zeroExpr).build();
methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(expectedResponseVarExpr, actualResponseExpr).build());
methodStatements.addAll(methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();
methodStatements.add(EMPTY_LINE_STATEMENT);
String testMethodName = String.format("%sTest", JavaStyle.toLowerCamelCase(method.name()));
return MethodDefinition.builder().setAnnotations(Arrays.asList(TEST_ANNOTATION)).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(testMethodName).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(Exception.class))).setBody(methodStatements).build();
}
Aggregations