Search in sources :

Example 1 with SourceCodeInfoLocation

use of com.google.api.generator.gapic.model.SourceCodeInfoLocation 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 SourceCodeInfoLocation

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

the class SourceCodeInfoParser method getLocation.

/**
 * Gets the location of a field, if available.
 */
@Nullable
public SourceCodeInfoLocation getLocation(FieldDescriptor field) {
    FileDescriptor file = field.getFile();
    if (!file.toProto().hasSourceCodeInfo()) {
        return null;
    }
    Location fieldLocation = getLocation(file, buildPath(field));
    return SourceCodeInfoLocation.create(fieldLocation);
}
Also used : FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Location(com.google.protobuf.DescriptorProtos.SourceCodeInfo.Location) SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) Nullable(javax.annotation.Nullable)

Example 3 with SourceCodeInfoLocation

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

the class SourceCodeInfoParserTest method getOuterEnumInfo.

@Test
public void getOuterEnumInfo() {
    EnumDescriptor protoEnum = protoFile.findEnumTypeByName("OuterEnum");
    SourceCodeInfoLocation location = parser.getLocation(protoEnum);
    assertEquals("This is an outer enum.", location.getLeadingComments());
    // Enum fields.
    location = parser.getLocation(protoEnum.findValueByName("VALUE_UNSPECIFIED"));
    assertEquals("Another unspecified value.", location.getLeadingComments());
}
Also used : SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) Test(org.junit.Test)

Example 4 with SourceCodeInfoLocation

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

the class SourceCodeInfoParserTest method getInnerMessageInfo.

@Test
public void getInnerMessageInfo() {
    Descriptor message = protoFile.findMessageTypeByName("FooMessage");
    assertThat(message).isNotNull();
    message = message.findNestedTypeByName("BarMessage");
    SourceCodeInfoLocation location = parser.getLocation(message);
    assertEquals("This is an inner message description for BarMessage.", location.getLeadingComments());
    // Fields.
    location = parser.getLocation(message.findFieldByName("field_three"));
    assertEquals("A third leading comment for field_three.", location.getLeadingComments());
    location = parser.getLocation(message.findFieldByName("field_two"));
    assertEquals("This is a block comment for field_two.", location.getLeadingComments());
}
Also used : SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) Descriptor(com.google.protobuf.Descriptors.Descriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) OneofDescriptor(com.google.protobuf.Descriptors.OneofDescriptor) Test(org.junit.Test)

Example 5 with SourceCodeInfoLocation

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

the class SourceCodeInfoParserTest method getServiceInfo.

@Test
public void getServiceInfo() {
    SourceCodeInfoLocation location = parser.getLocation(protoFile.findServiceByName("FooService"));
    assertEquals("This is a service description.\n It takes up multiple lines, like so.", location.getLeadingComments());
    location = parser.getLocation(protoFile.findServiceByName("BarService"));
    assertEquals("This is another service description.", location.getLeadingComments());
}
Also used : SourceCodeInfoLocation(com.google.api.generator.gapic.model.SourceCodeInfoLocation) Test(org.junit.Test)

Aggregations

SourceCodeInfoLocation (com.google.api.generator.gapic.model.SourceCodeInfoLocation)10 Test (org.junit.Test)7 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)6 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)6 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)6 Descriptor (com.google.protobuf.Descriptors.Descriptor)5 OneofDescriptor (com.google.protobuf.Descriptors.OneofDescriptor)4 ResourceDescriptor (com.google.api.ResourceDescriptor)2 Field (com.google.api.generator.gapic.model.Field)2 ResourceReference (com.google.api.generator.gapic.model.ResourceReference)2 ClientProto (com.google.api.ClientProto)1 DocumentationRule (com.google.api.DocumentationRule)1 HttpRule (com.google.api.HttpRule)1 ResourceProto (com.google.api.ResourceProto)1 TypeNode (com.google.api.generator.engine.ast.TypeNode)1 VaporReference (com.google.api.generator.engine.ast.VaporReference)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