use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createMethodVariants.
private static List<MethodDefinition> createMethodVariants(Method method, String clientName, Map<String, Message> messageTypes, TypeStore typeStore, Map<String, ResourceName> resourceNames, List<Sample> samples) {
List<MethodDefinition> javaMethods = new ArrayList<>();
String methodName = JavaStyle.toLowerCamelCase(method.name());
TypeNode methodInputType = method.inputType();
TypeNode methodOutputType = method.isPaged() ? typeStore.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())) : method.outputType();
if (method.hasLro()) {
LongrunningOperation lro = method.lro();
methodOutputType = TypeNode.withReference(typeStore.get("OperationFuture").reference().copyAndSetGenerics(Arrays.asList(lro.responseType().reference(), lro.metadataType().reference())));
}
for (List<MethodArgument> signature : method.methodSignatures()) {
// Get the argument list.
List<VariableExpr> arguments = signature.stream().map(methodArg -> VariableExpr.builder().setVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(methodArg.name())).setType(methodArg.type()).build()).setIsDecl(true).build()).collect(Collectors.toList());
// Request proto builder.
VariableExpr requestVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("request").setType(methodInputType).build()).setIsDecl(true).build();
Expr requestBuilderExpr = createRequestBuilderExpr(method, signature, typeStore);
AssignmentExpr requestAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr).setValueExpr(requestBuilderExpr).build();
List<Statement> statements = new ArrayList<>();
statements.add(ExprStatement.withExpr(requestAssignmentExpr));
MethodInvocationExpr rpcInvocationExpr = MethodInvocationExpr.builder().setMethodName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)).setArguments(Arrays.asList(requestVarExpr.toBuilder().setIsDecl(false).build())).setReturnType(methodOutputType).build();
Optional<Sample> methodSample = Optional.of(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, typeStore.get(clientName), signature, resourceNames, messageTypes));
Optional<String> methodDocSample = Optional.empty();
if (methodSample.isPresent()) {
samples.add(methodSample.get());
methodDocSample = Optional.of(SampleCodeWriter.writeInlineSample(methodSample.get().body()));
}
MethodDefinition.Builder methodVariantBuilder = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createRpcMethodHeaderComment(method, signature, methodDocSample)).setScope(ScopeNode.PUBLIC).setIsFinal(true).setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)).setArguments(arguments);
if (isProtoEmptyType(methodOutputType)) {
statements.add(ExprStatement.withExpr(rpcInvocationExpr));
methodVariantBuilder = methodVariantBuilder.setReturnType(TypeNode.VOID);
} else {
methodVariantBuilder = methodVariantBuilder.setReturnType(methodOutputType).setReturnExpr(rpcInvocationExpr);
}
if (method.isDeprecated()) {
methodVariantBuilder = methodVariantBuilder.setAnnotations(Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
}
methodVariantBuilder = methodVariantBuilder.setBody(statements);
javaMethods.add(methodVariantBuilder.build());
}
return javaMethods;
}
use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method createRpcExceptionTestStatements.
protected List<Statement> createRpcExceptionTestStatements(Method method, List<MethodArgument> methodSignature, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
List<VariableExpr> argVarExprs = new ArrayList<>();
List<Expr> tryBodyExprs = new ArrayList<>();
if (methodSignature.isEmpty()) {
// Construct the actual request.
VariableExpr varExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
argVarExprs.add(varExpr);
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
Expr valExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes, valuePatterns);
tryBodyExprs.add(AssignmentExpr.builder().setVariableExpr(varExpr.toBuilder().setIsDecl(true).build()).setValueExpr(valExpr).build());
} else {
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
for (MethodArgument methodArg : methodSignature) {
String methodArgName = JavaStyle.toLowerCamelCase(methodArg.name());
VariableExpr varExpr = VariableExpr.withVariable(Variable.builder().setType(methodArg.type()).setName(methodArgName).build());
argVarExprs.add(varExpr);
Expr valExpr = DefaultValueComposer.createMethodArgValue(methodArg, resourceNames, messageTypes, valuePatterns);
tryBodyExprs.add(AssignmentExpr.builder().setVariableExpr(varExpr.toBuilder().setIsDecl(true).build()).setValueExpr(valExpr).build());
}
}
String rpcJavaName = JavaStyle.toLowerCamelCase(method.name());
if (method.hasLro()) {
rpcJavaName += "Async";
}
MethodInvocationExpr rpcJavaMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get("client")).setMethodName(rpcJavaName).setArguments(argVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).build();
if (method.hasLro()) {
rpcJavaMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(rpcJavaMethodInvocationExpr).setMethodName("get").build();
}
tryBodyExprs.add(rpcJavaMethodInvocationExpr);
VariableExpr catchExceptionVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.withExceptionClazz(method.hasLro() ? ExecutionException.class : InvalidArgumentException.class)).setName("e").build()).build();
List<Statement> catchBody = method.hasLro() ? createRpcLroExceptionTestCatchBody(catchExceptionVarExpr, false) : Arrays.asList(CommentStatement.withComment(LineComment.withComment("Expected exception.")));
// Assert a failure if no exception was raised.
tryBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("fail").setArguments(ValueExpr.withValue(StringObjectValue.withValue("No exception raised"))).build());
TryCatchStatement tryCatchBlock = TryCatchStatement.builder().setTryBody(tryBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).addCatch(catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), catchBody).build();
return Arrays.asList(EMPTY_LINE_STATEMENT, tryCatchBlock);
}
use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.
the class ServiceClientCommentComposer method createRpcMethodHeaderComment.
public static List<CommentStatement> createRpcMethodHeaderComment(Method method, List<MethodArgument> methodArguments, Optional<String> sampleCodeOpt) {
JavaDocComment.Builder methodJavadocBuilder = JavaDocComment.builder();
if (method.hasDescription()) {
methodJavadocBuilder = processProtobufComment(method.description(), methodJavadocBuilder, null);
}
if (sampleCodeOpt.isPresent()) {
methodJavadocBuilder.addParagraph(METHOD_DESCRIPTION_SAMPLE_CODE_SUMMARY_STRING);
methodJavadocBuilder.addSampleCode(sampleCodeOpt.get());
}
if (methodArguments.isEmpty()) {
methodJavadocBuilder.addParam("request", "The request object containing all of the parameters for the API call.");
} else {
for (MethodArgument argument : methodArguments) {
// TODO(miraleung): Remove the newline replacement when we support CommonMark.
String description = argument.field().hasDescription() ? argument.field().description() : EMPTY_STRING;
methodJavadocBuilder.addParam(JavaStyle.toLowerCamelCase(argument.name()), description);
}
}
methodJavadocBuilder.setThrows(API_EXCEPTION_TYPE_NAME, EXCEPTION_CONDITION);
if (method.isDeprecated()) {
methodJavadocBuilder.setDeprecated(CommentComposer.DEPRECATED_METHOD_STRING);
}
List<CommentStatement> comments = new ArrayList<>();
comments.add(CommentComposer.AUTO_GENERATED_METHOD_COMMENT);
if (!methodJavadocBuilder.emptyComments()) {
comments.add(CommentStatement.withComment(methodJavadocBuilder.build()));
}
return comments;
}
use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.
the class MethodSignatureParserTest method flattenMethodSignatures_manyToOne.
@Test
public void flattenMethodSignatures_manyToOne() {
String fooName = "fooName";
String anInt = "anInt";
TypeNode fooTypeOne = TypeNode.withReference(VaporReference.builder().setName("FooName").setPakkage("com.google.foobar").build());
TypeNode fooTypeTwo = TypeNode.withReference(VaporReference.builder().setName("FooTwoName").setPakkage("com.google.foobar").build());
List<String> argumentNames = Arrays.asList(fooName, anInt);
BiFunction<String, TypeNode, MethodArgument> methodArgFn = (name, type) -> MethodArgument.builder().setName(name).setType(type).setField(Field.builder().setName(name).setType(type).build()).build();
List<MethodArgument> fooArgs = Arrays.asList(TypeNode.STRING, fooTypeOne, fooTypeTwo).stream().map(t -> methodArgFn.apply(fooName, t)).collect(Collectors.toList());
Map<String, List<MethodArgument>> argumentNameToOverloads = new HashMap<>();
argumentNameToOverloads.put(fooName, fooArgs);
argumentNameToOverloads.put(anInt, Arrays.asList(methodArgFn.apply(anInt, TypeNode.INT)));
List<List<MethodArgument>> flattenedSignatures = MethodSignatureParser.flattenMethodSignatureVariants(argumentNames, argumentNameToOverloads);
assertEquals(3, flattenedSignatures.size());
assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT)));
assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT)));
assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT)));
}
use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_pagedRpcWithNoMethodArguments.
@Test
public void valid_composeShowcaseMethodSample_pagedRpcWithNoMethodArguments() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("ListContentRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("ListContentResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
List<MethodArgument> arguments = Collections.emptyList();
Method method = Method.builder().setName("ListContent").setMethodSignatures(Arrays.asList(arguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
Reference repeatedResponseReference = VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_NAME).build();
Field repeatedField = Field.builder().setName("responses").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(repeatedResponseReference).build())).setIsMessage(true).setIsRepeated(true).build();
Field nextPagedTokenField = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
Message listContentResponseMessage = Message.builder().setName("ListContentResponse").setFullProtoName("google.showcase.v1beta1.ListContentResponse").setType(outputType).setFields(Arrays.asList(repeatedField, nextPagedTokenField)).build();
messageTypes.put("com.google.showcase.v1beta1.ListContentResponse", listContentResponseMessage);
String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " for (Content element : echoClient.listContent().iterateAll()) {\n", " // doThingsWith(element);\n", " }\n", "}");
Assert.assertEquals(results, expected);
}
Aggregations