use of com.google.api.generator.engine.ast.VariableExpr 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.VariableExpr 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.VariableExpr 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.VariableExpr in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposer method composeSample.
static Sample composeSample(Method method, VariableExpr clientVarExpr, List<VariableExpr> rpcMethodArgVarExprs, List<Expr> bodyExprs) {
// Invoke current method based on return type.
// e.g. if return void, echoClient.echo(..); or,
// e.g. if return other type, EchoResponse response = echoClient.echo(...);
boolean returnsVoid = method.outputType().isProtoEmptyType();
MethodInvocationExpr clientRpcMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(method.name())).setArguments(rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).setReturnType(method.outputType()).build();
if (returnsVoid) {
bodyExprs.add(clientRpcMethodInvocationExpr);
} else {
VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(method.outputType()).build());
bodyExprs.add(AssignmentExpr.builder().setVariableExpr(responseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(clientRpcMethodInvocationExpr).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.VariableExpr 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();
}
Aggregations