Search in sources :

Example 1 with RoutingHeaderRule

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

the class Parser method parseMethods.

@VisibleForTesting
static List<Method> parseMethods(ServiceDescriptor serviceDescriptor, String servicePackage, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, Optional<GapicServiceConfig> serviceConfigOpt, Set<ResourceName> outputArgResourceNames, Transport transport) {
    List<Method> methods = new ArrayList<>();
    for (MethodDescriptor protoMethod : serviceDescriptor.getMethods()) {
        // Parse the method.
        TypeNode inputType = TypeParser.parseType(protoMethod.getInputType());
        Method.Builder methodBuilder = Method.builder();
        if (protoMethod.getFile().toProto().hasSourceCodeInfo()) {
            SourceCodeInfoLocation protoMethodLocation = SOURCE_CODE_INFO_PARSER.getLocation(protoMethod);
            if (!Objects.isNull(protoMethodLocation) && !Strings.isNullOrEmpty(protoMethodLocation.getLeadingComments())) {
                methodBuilder.setDescription(protoMethodLocation.getLeadingComments());
            }
        }
        boolean isDeprecated = false;
        if (protoMethod.getOptions().hasDeprecated()) {
            isDeprecated = protoMethod.getOptions().getDeprecated();
        }
        Message inputMessage = messageTypes.get(inputType.reference().fullName());
        Preconditions.checkNotNull(inputMessage, String.format("No message found for %s", inputType.reference().fullName()));
        HttpBindings httpBindings = HttpRuleParser.parse(protoMethod, inputMessage, messageTypes);
        boolean isBatching = !serviceConfigOpt.isPresent() ? false : serviceConfigOpt.get().hasBatchingSetting(/* protoPakkage */
        protoMethod.getFile().getPackage(), serviceDescriptor.getName(), protoMethod.getName());
        boolean operationPollingMethod = protoMethod.getOptions().hasExtension(ExtendedOperationsProto.operationPollingMethod) ? protoMethod.getOptions().getExtension(ExtendedOperationsProto.operationPollingMethod) : false;
        RoutingHeaderRule routingHeaderRule = RoutingRuleParser.parse(protoMethod, inputMessage, messageTypes);
        methods.add(methodBuilder.setName(protoMethod.getName()).setInputType(inputType).setOutputType(TypeParser.parseType(protoMethod.getOutputType())).setStream(Method.toStream(protoMethod.isClientStreaming(), protoMethod.isServerStreaming())).setLro(parseLro(servicePackage, protoMethod, messageTypes)).setMethodSignatures(MethodSignatureParser.parseMethodSignatures(protoMethod, servicePackage, inputType, messageTypes, resourceNames, outputArgResourceNames)).setHttpBindings(httpBindings).setRoutingHeaderRule(routingHeaderRule).setIsBatching(isBatching).setPageSizeFieldName(parsePageSizeFieldName(protoMethod, messageTypes, transport)).setIsDeprecated(isDeprecated).setOperationPollingMethod(operationPollingMethod).build());
        // Any input type that has a resource reference will need a resource name helper class.
        for (Field field : inputMessage.fields()) {
            if (field.hasResourceReference()) {
                String resourceTypeString = field.resourceReference().resourceTypeString();
                ResourceName resourceName = null;
                // versus example.com/FooBar.
                if (resourceTypeString.indexOf(SLASH) < 0) {
                    Optional<String> actualResourceTypeNameOpt = resourceNames.keySet().stream().filter(k -> k.substring(k.lastIndexOf(SLASH) + 1).equals(resourceTypeString)).findFirst();
                    if (actualResourceTypeNameOpt.isPresent()) {
                        resourceName = resourceNames.get(actualResourceTypeNameOpt.get());
                    }
                } else {
                    resourceName = resourceNames.get(resourceTypeString);
                }
                if (ResourceNameConstants.WILDCARD_PATTERN.equals(resourceTypeString)) {
                    resourceName = WILDCARD_RESOURCE_NAME;
                } else {
                    Preconditions.checkNotNull(resourceName, String.format("Resource name %s not found; parsing field %s in message %s in method %s", resourceTypeString, field.name(), inputMessage.name(), protoMethod.getName()));
                }
                outputArgResourceNames.add(resourceName);
            }
        }
    }
    return methods;
}
Also used : HttpBindings(com.google.api.generator.gapic.model.HttpBindings) CodeGeneratorRequest(com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest) Arrays(java.util.Arrays) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) OperationInfo(com.google.longrunning.OperationInfo) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Field(com.google.api.generator.gapic.model.Field) Method(com.google.api.generator.gapic.model.Method) HttpRule(com.google.api.HttpRule) LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Map(java.util.Map) ResourceProto(com.google.api.ResourceProto) FieldOptions(com.google.protobuf.DescriptorProtos.FieldOptions) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) BiMap(com.google.common.collect.BiMap) ResourceDescriptor(com.google.api.ResourceDescriptor) ImmutableSet(com.google.common.collect.ImmutableSet) ResourceReference(com.google.api.generator.gapic.model.ResourceReference) Collection(java.util.Collection) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) OperationsProto(com.google.longrunning.OperationsProto) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) DocumentationRule(com.google.api.DocumentationRule) Optional(java.util.Optional) Transport(com.google.api.generator.gapic.model.Transport) GapicContext(com.google.api.generator.gapic.model.GapicContext) GapicLanguageSettings(com.google.api.generator.gapic.model.GapicLanguageSettings) IntStream(java.util.stream.IntStream) VaporReference(com.google.api.generator.engine.ast.VaporReference) TypeNode(com.google.api.generator.engine.ast.TypeNode) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings) Descriptor(com.google.protobuf.Descriptors.Descriptor) HashMap(java.util.HashMap) Function(java.util.function.Function) OperationResponseMapping(com.google.cloud.OperationResponseMapping) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Maps(com.google.common.collect.Maps) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) HashBiMap(com.google.common.collect.HashBiMap) ServiceOptions(com.google.protobuf.DescriptorProtos.ServiceOptions) ExtendedOperationsProto(com.google.cloud.ExtendedOperationsProto) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) Preconditions(com.google.common.base.Preconditions) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) OperationResponse(com.google.api.generator.gapic.model.OperationResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Message(com.google.api.generator.gapic.model.Message) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) Collections(java.util.Collections) ClientProto(com.google.api.ClientProto) MessageOptions(com.google.protobuf.DescriptorProtos.MessageOptions) SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) Message(com.google.api.generator.gapic.model.Message) HttpBindings(com.google.api.generator.gapic.model.HttpBindings) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) Method(com.google.api.generator.gapic.model.Method) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) Field(com.google.api.generator.gapic.model.Field) SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) TypeNode(com.google.api.generator.engine.ast.TypeNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with RoutingHeaderRule

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

the class RoutingRuleParserTest method parse_shouldParseRoutingRulesWithOneParameter.

@Test
public void parse_shouldParseRoutingRulesWithOneParameter() {
    RoutingHeaderRule actual = getRoutingHeaders(4);
    RoutingHeaderParam expected = RoutingHeaderParam.create("name", "rename", "/v1beta1/{rename=tests/*}");
    assertThat(actual.routingHeaderParams()).containsExactly(expected);
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) Test(org.junit.Test)

Example 3 with RoutingHeaderRule

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

the class RoutingRuleParserTest method parse_shouldReturnNullRoutingHeadersIfMethodHasNoRoutingRules.

@Test
public void parse_shouldReturnNullRoutingHeadersIfMethodHasNoRoutingRules() {
    RoutingHeaderRule actual = getRoutingHeaders(0);
    assertThat(actual).isNull();
}
Also used : RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) Test(org.junit.Test)

Example 4 with RoutingHeaderRule

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

the class RoutingRuleParserTest method parse_shouldParseRoutingRulesWithNestedFields.

@Test
public void parse_shouldParseRoutingRulesWithNestedFields() {
    RoutingHeaderRule actual = getRoutingHeaders(6);
    RoutingHeaderParam expectedHeader1 = RoutingHeaderParam.create("account.name", "rename", "/v1beta1/{rename=tests/*}");
    assertThat(actual.routingHeaderParams()).containsExactly(expectedHeader1);
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) Test(org.junit.Test)

Example 5 with RoutingHeaderRule

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

the class RoutingRuleParser method parse.

public static RoutingHeaderRule parse(MethodDescriptor protoMethod, Message inputMessage, Map<String, Message> messageTypes) {
    MethodOptions methodOptions = protoMethod.getOptions();
    if (!methodOptions.hasExtension(RoutingProto.routing)) {
        return null;
    }
    RoutingHeaderRule.Builder routingHeaderRuleBuilder = RoutingHeaderRule.builder();
    RoutingRule routingRule = methodOptions.getExtension(RoutingProto.routing);
    for (RoutingParameter routingParameter : routingRule.getRoutingParametersList()) {
        String pathTemplate = routingParameter.getPathTemplate();
        String fieldName = routingParameter.getField();
        // validate if field exist in Message or nested Messages and the type of leaf level field
        inputMessage.validateField(fieldName, messageTypes, TypeNode.STRING);
        String key;
        if (Strings.isNullOrEmpty(pathTemplate)) {
            key = fieldName;
            pathTemplate = String.format("{%s=**}", key);
        } else {
            Set<String> namedSegments = PatternParser.getPattenBindings(pathTemplate);
            Preconditions.checkArgument(namedSegments.size() == 1, String.format("There needs to be one and only one named segment in path template %s", pathTemplate));
            key = namedSegments.iterator().next();
        }
        RoutingHeaderParam routingHeaderParam = RoutingHeaderParam.create(fieldName, key, pathTemplate);
        routingHeaderRuleBuilder.addParam(routingHeaderParam);
    }
    return routingHeaderRuleBuilder.build();
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) RoutingParameter(com.google.api.RoutingParameter) RoutingHeaderRule(com.google.api.generator.gapic.model.RoutingHeaderRule) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) RoutingRule(com.google.api.RoutingRule)

Aggregations

RoutingHeaderRule (com.google.api.generator.gapic.model.RoutingHeaderRule)7 RoutingHeaderParam (com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam)5 Test (org.junit.Test)5 ClientProto (com.google.api.ClientProto)1 DocumentationRule (com.google.api.DocumentationRule)1 HttpRule (com.google.api.HttpRule)1 ResourceDescriptor (com.google.api.ResourceDescriptor)1 ResourceProto (com.google.api.ResourceProto)1 RoutingParameter (com.google.api.RoutingParameter)1 RoutingRule (com.google.api.RoutingRule)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 VaporReference (com.google.api.generator.engine.ast.VaporReference)1 Field (com.google.api.generator.gapic.model.Field)1 GapicBatchingSettings (com.google.api.generator.gapic.model.GapicBatchingSettings)1 GapicContext (com.google.api.generator.gapic.model.GapicContext)1 GapicLanguageSettings (com.google.api.generator.gapic.model.GapicLanguageSettings)1 GapicLroRetrySettings (com.google.api.generator.gapic.model.GapicLroRetrySettings)1 GapicServiceConfig (com.google.api.generator.gapic.model.GapicServiceConfig)1 HttpBindings (com.google.api.generator.gapic.model.HttpBindings)1 LongrunningOperation (com.google.api.generator.gapic.model.LongrunningOperation)1