use of com.google.api.generator.engine.ast.Expr in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposer method composeStreamCallableMethod.
public static Sample composeStreamCallableMethod(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());
// Assign method's request variable with the default value.
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(method.inputType()).build());
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();
RegionTag regionTag = null;
List<Statement> bodyStatements = new ArrayList<>();
if (method.stream().equals(Method.Stream.SERVER)) {
// e.g. ServerStream<EchoResponse> stream = echoClient.expandCallable().call(request);
Sample streamServer = composeStreamServerSample(method, clientVarExpr, requestAssignmentExpr);
bodyStatements.addAll(streamServer.body());
regionTag = streamServer.regionTag();
} else if (method.stream().equals(Method.Stream.BIDI)) {
// e.g. echoClient.collect().clientStreamingCall(responseObserver);
Sample streamBidi = composeStreamBidiSample(method, clientVarExpr, requestAssignmentExpr);
bodyStatements.addAll(streamBidi.body());
regionTag = streamBidi.regionTag();
} else if (method.stream().equals(Method.Stream.CLIENT)) {
Sample streamClient = composeStreamClientSample(method, clientVarExpr, requestAssignmentExpr);
bodyStatements.addAll(streamClient.body());
regionTag = streamClient.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.Expr in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposer method composeStreamBidiSample.
private static Sample composeStreamBidiSample(Method method, VariableExpr clientVarExpr, AssignmentExpr requestAssignmentExpr) {
List<Expr> bodyExprs = new ArrayList<>();
// Create bidi stream variable expression and assign it with invoking client's bidi stream
// method.
// e.g. BidiStream<EchoRequest, EchoResponse> bidiStream = echoClient.chatCallable().call();
TypeNode bidiStreamType = TypeNode.withReference(ConcreteReference.builder().setClazz(BidiStream.class).setGenerics(method.inputType().reference(), method.outputType().reference()).build());
VariableExpr bidiStreamVarExpr = VariableExpr.withVariable(Variable.builder().setName("bidiStream").setType(bidiStreamType).build());
MethodInvocationExpr clientBidiStreamCallMethodInvoationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sCallable", method.name()))).build();
clientBidiStreamCallMethodInvoationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientBidiStreamCallMethodInvoationExpr).setMethodName("call").setReturnType(bidiStreamType).build();
AssignmentExpr bidiStreamAssignmentExpr = AssignmentExpr.builder().setVariableExpr(bidiStreamVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(clientBidiStreamCallMethodInvoationExpr).build();
bodyExprs.add(bidiStreamAssignmentExpr);
// Add request with default value expression.
bodyExprs.add(requestAssignmentExpr);
// Invoke send method with argument request.
// e.g. bidiStream.send(request);
MethodInvocationExpr sendMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(bidiStreamVarExpr).setArguments(requestAssignmentExpr.variableExpr().toBuilder().setIsDecl(false).build()).setMethodName("send").build();
bodyExprs.add(sendMethodInvocationExpr);
List<Statement> bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
// For-loop on bidi stream variable.
// e.g. for (EchoResponse response : bidiStream) {
// // Do something when reveive a response.
// }
ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(VariableExpr.builder().setVariable(Variable.builder().setType(method.outputType()).setName("response").build()).setIsDecl(true).build()).setCollectionExpr(bidiStreamVarExpr).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Do something when a response is received.")))).build();
bodyStatements.add(forStatement);
RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setIsAsynchronous(true).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 ServiceClientHeaderSampleComposer method composeTransportSample.
public static Sample composeTransportSample(TypeNode clientType, TypeNode settingsType, String transportProviderMethod) {
String settingsName = JavaStyle.toLowerCamelCase(settingsType.reference().name());
String clientName = JavaStyle.toLowerCamelCase(clientType.reference().name());
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(settingsName).setType(settingsType).build());
MethodInvocationExpr newBuilderMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
MethodInvocationExpr transportChannelProviderArg = MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName(transportProviderMethod).build()).setMethodName("build").build();
MethodInvocationExpr credentialsMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderMethodExpr).setArguments(transportChannelProviderArg).setMethodName("setTransportChannelProvider").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.Expr in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposer method composeSetEndpointSample.
public static Sample composeSetEndpointSample(TypeNode clientType, TypeNode settingsType) {
// Initialize client settings with builder() method.
// e.g. EchoSettings echoSettings = EchoSettings.newBuilder().setEndpoint("myEndpoint").build();
String settingsName = JavaStyle.toLowerCamelCase(settingsType.reference().name());
String clientName = JavaStyle.toLowerCamelCase(clientType.reference().name());
TypeNode myEndpointType = TypeNode.withReference(VaporReference.builder().setName("myEndpoint").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();
MethodInvocationExpr credentialsMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderMethodExpr).setArguments(VariableExpr.withVariable(Variable.builder().setName("myEndpoint").setType(myEndpointType).build())).setMethodName("setEndpoint").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();
// Initialize 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();
RegionTag regionTag = RegionTag.builder().setServiceName(clientName).setRpcName(rpcName).setOverloadDisambiguation("setEndpoint").build();
List<Statement> sampleBody = Arrays.asList(ExprStatement.withExpr(initSettingsVarExpr), ExprStatement.withExpr(initClientVarExpr));
return Sample.builder().setBody(sampleBody).setRegionTag(regionTag).build();
}
use of com.google.api.generator.engine.ast.Expr 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();
}
Aggregations