Search in sources :

Example 11 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class MethodSignatureParser method parseMethodSignatures.

/**
 * Parses a list of method signature annotations out of an RPC.
 */
public static List<List<MethodArgument>> parseMethodSignatures(MethodDescriptor methodDescriptor, String servicePackage, TypeNode methodInputType, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, Set<ResourceName> outputArgResourceNames) {
    List<String> stringSigs = methodDescriptor.getOptions().getExtension(ClientProto.methodSignature);
    List<List<MethodArgument>> signatures = new ArrayList<>();
    if (stringSigs.isEmpty()) {
        return signatures;
    }
    Map<String, ResourceName> patternsToResourceNames = ResourceParserHelpers.createPatternResourceNameMap(resourceNames);
    Message inputMessage = messageTypes.get(methodInputType.reference().fullName());
    // stringSigs: ["content,error", "content,error,info"].
    for (String stringSig : stringSigs) {
        if (Strings.isNullOrEmpty(stringSig)) {
            signatures.add(Collections.emptyList());
            continue;
        }
        List<String> argumentNames = new ArrayList<>();
        Map<String, List<MethodArgument>> argumentNameToOverloads = new HashMap<>();
        // stringSig.split: ["content", "error"].
        for (String argumentName : stringSig.split(METHOD_SIGNATURE_DELIMITER)) {
            // For resource names, this will be empty.
            List<Field> argumentFieldPathAcc = new ArrayList<>();
            // There should be more than one type returned only when we encounter a resource name.
            Map<TypeNode, Field> argumentTypes = parseTypeFromArgumentName(argumentName, servicePackage, inputMessage, messageTypes, resourceNames, patternsToResourceNames, argumentFieldPathAcc, outputArgResourceNames);
            int dotLastIndex = argumentName.lastIndexOf(DOT);
            String actualArgumentName = dotLastIndex < 0 ? argumentName : argumentName.substring(dotLastIndex + 1);
            argumentNames.add(actualArgumentName);
            argumentNameToOverloads.put(actualArgumentName, argumentTypes.entrySet().stream().map(e -> MethodArgument.builder().setName(actualArgumentName).setType(e.getKey()).setField(e.getValue()).setIsResourceNameHelper(argumentTypes.size() > 1 && !e.getKey().equals(TypeNode.STRING)).setNestedFields(argumentFieldPathAcc).build()).collect(Collectors.toList()));
        }
        signatures.addAll(flattenMethodSignatureVariants(argumentNames, argumentNameToOverloads));
    }
    // Make the method signature order deterministic, which helps with unit testing and per-version
    // diffs.
    List<List<MethodArgument>> sortedMethodSignatures = signatures.stream().sorted((s1, s2) -> {
        // Sort by number of arguments first.
        if (s1.size() != s2.size()) {
            return s1.size() - s2.size();
        }
        // Then by MethodSignature properties.
        for (int i = 0; i < s1.size(); i++) {
            int compareVal = s1.get(i).compareTo(s2.get(i));
            if (compareVal != 0) {
                return compareVal;
            }
        }
        return 0;
    }).collect(Collectors.toList());
    return sortedMethodSignatures;
}
Also used : TypeNode(com.google.api.generator.engine.ast.TypeNode) ResourceReference(com.google.api.generator.gapic.model.ResourceReference) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Field(com.google.api.generator.gapic.model.Field) List(java.util.List) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) Lists(com.google.common.collect.Lists) Map(java.util.Map) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Message(com.google.api.generator.gapic.model.Message) Collections(java.util.Collections) ClientProto(com.google.api.ClientProto) Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) Field(com.google.api.generator.gapic.model.Field) ArrayList(java.util.ArrayList) List(java.util.List) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 12 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_lroRpcWithReturnVoid.

@Test
public void valid_composeShowcaseMethodSample_lroRpcWithReturnVoid() {
    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("WaitRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build());
    TypeNode responseType = TypeNode.withReference(VaporReference.builder().setName("Empty").setPakkage(PROTO_PACKAGE_NAME).build());
    TypeNode metadataType = TypeNode.withReference(VaporReference.builder().setName("WaitMetadata").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    LongrunningOperation lro = LongrunningOperation.builder().setResponseType(responseType).setMetadataType(metadataType).build();
    TypeNode ttlTypeNode = TypeNode.withReference(VaporReference.builder().setName("Duration").setPakkage(PROTO_PACKAGE_NAME).build());
    MethodArgument ttl = MethodArgument.builder().setName("ttl").setType(ttlTypeNode).setField(Field.builder().setName("ttl").setType(ttlTypeNode).setIsMessage(true).setIsContainedInOneof(true).build()).build();
    List<MethodArgument> arguments = Arrays.asList(ttl);
    Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setLro(lro).setMethodSignatures(Arrays.asList(arguments)).build();
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  Duration ttl = Duration.newBuilder().build();\n", "  echoClient.waitAsync(ttl).get();\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Example 13 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method invalid_composeShowcaseMethodSample_noRepeatedResponseTypeInPagedMethod.

@Test
public void invalid_composeShowcaseMethodSample_noRepeatedResponseTypeInPagedMethod() {
    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("EchoRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("PagedResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    List<MethodArgument> methodArguments = Collections.emptyList();
    Method method = Method.builder().setName("simplePagedMethod").setMethodSignatures(Arrays.asList(methodArguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    Field responseField = Field.builder().setName("response").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(ConcreteReference.withClazz(String.class)).build())).setIsMessage(true).setIsRepeated(false).build();
    Field nextPageToken = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
    Message noRepeatedFieldMessage = Message.builder().setName("PagedResponse").setFullProtoName("google.showcase.v1beta1.PagedResponse").setType(outputType).setFields(Arrays.asList(responseField, nextPageToken)).build();
    messageTypes.put("PagedResponse", noRepeatedFieldMessage);
    Assert.assertThrows(NullPointerException.class, () -> ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, methodArguments, resourceNames, messageTypes));
}
Also used : Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) Method(com.google.api.generator.gapic.model.Method) Field(com.google.api.generator.gapic.model.Field) List(java.util.List) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 14 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method composeClassHeaderSample_firstMethodIsNotUnaryRpc.

@Test
public void composeClassHeaderSample_firstMethodIsNotUnaryRpc() {
    Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("WaitRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build());
    TypeNode responseType = TypeNode.withReference(VaporReference.builder().setName("WaitResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode metadataType = TypeNode.withReference(VaporReference.builder().setName("WaitMetadata").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    LongrunningOperation lro = LongrunningOperation.builder().setResponseType(responseType).setMetadataType(metadataType).build();
    TypeNode ttlTypeNode = TypeNode.withReference(VaporReference.builder().setName("Duration").setPakkage(PROTO_PACKAGE_NAME).build());
    MethodArgument ttl = MethodArgument.builder().setName("ttl").setType(ttlTypeNode).setField(Field.builder().setName("ttl").setType(ttlTypeNode).setIsMessage(true).setIsContainedInOneof(true).build()).build();
    Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setLro(lro).setMethodSignatures(Arrays.asList(Arrays.asList(ttl))).build();
    Service service = Service.builder().setName("Echo").setDefaultHost("localhost:7469").setOauthScopes(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")).setPakkage(SHOWCASE_PACKAGE_NAME).setProtoPakkage(SHOWCASE_PACKAGE_NAME).setOriginalJavaPackage(SHOWCASE_PACKAGE_NAME).setOverriddenName("Echo").setMethods(Arrays.asList(method)).build();
    TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeClassHeaderSample(service, clientType, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  Duration ttl = Duration.newBuilder().build();\n", "  WaitResponse response = echoClient.waitAsync(ttl).get();\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Example 15 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method invalid_composeShowcaseMethodSample_noMatchedRepeatedResponseTypeInPagedMethod.

@Test
public void invalid_composeShowcaseMethodSample_noMatchedRepeatedResponseTypeInPagedMethod() {
    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("EchoRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("PagedResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    List<MethodArgument> methodArguments = Collections.emptyList();
    Method method = Method.builder().setName("simplePagedMethod").setMethodSignatures(Arrays.asList(methodArguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    Assert.assertThrows(NullPointerException.class, () -> ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, methodArguments, resourceNames, messageTypes));
}
Also used : Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Aggregations

MethodArgument (com.google.api.generator.gapic.model.MethodArgument)19 TypeNode (com.google.api.generator.engine.ast.TypeNode)17 List (java.util.List)14 Message (com.google.api.generator.gapic.model.Message)13 Test (org.junit.Test)13 Field (com.google.api.generator.gapic.model.Field)12 ResourceName (com.google.api.generator.gapic.model.ResourceName)12 Method (com.google.api.generator.gapic.model.Method)10 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 VaporReference (com.google.api.generator.engine.ast.VaporReference)7 Descriptors (com.google.protobuf.Descriptors)7 ArrayList (java.util.ArrayList)7 Arrays (java.util.Arrays)7 Function (java.util.function.Function)7 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)5 LongrunningOperation (com.google.api.generator.gapic.model.LongrunningOperation)5 HashMap (java.util.HashMap)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)4