Search in sources :

Example 76 with ResourceName

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

the class ResourceNameParser method parseResourceNamesFromMessages.

@VisibleForTesting
static Map<String, ResourceName> parseResourceNamesFromMessages(List<Descriptor> messageTypeDescriptors, String pakkage) {
    Map<String, ResourceName> resourceNames = new HashMap<>();
    for (Descriptor messageTypeDescriptor : messageTypeDescriptors) {
        Optional<ResourceName> resourceNameModelOpt = parseResourceNameFromMessageType(messageTypeDescriptor, pakkage);
        if (resourceNameModelOpt.isPresent()) {
            ResourceName resourceName = resourceNameModelOpt.get();
            resourceNames.put(resourceName.resourceTypeString(), resourceName);
        }
    }
    return resourceNames;
}
Also used : HashMap(java.util.HashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) ResourceDescriptor(com.google.api.ResourceDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 77 with ResourceName

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

the class ResourceNameParser method parseResourceNamesFromFile.

@VisibleForTesting
static Map<String, ResourceName> parseResourceNamesFromFile(FileDescriptor fileDescriptor, String javaPackage) {
    Map<String, ResourceName> typeStringToResourceNames = new HashMap<>();
    FileOptions fileOptions = fileDescriptor.getOptions();
    if (fileOptions.getExtensionCount(ResourceProto.resourceDefinition) <= 0) {
        return typeStringToResourceNames;
    }
    List<ResourceDescriptor> protoResources = fileOptions.getExtension(ResourceProto.resourceDefinition);
    for (ResourceDescriptor protoResource : protoResources) {
        Optional<ResourceName> resourceNameModelOpt = createResourceName(protoResource, javaPackage);
        if (!resourceNameModelOpt.isPresent()) {
            continue;
        }
        ResourceName resourceNameModel = resourceNameModelOpt.get();
        // Clobber anything if we're creating a new ResourceName from a proto.
        typeStringToResourceNames.put(resourceNameModel.resourceTypeString(), resourceNameModel);
    }
    return typeStringToResourceNames;
}
Also used : HashMap(java.util.HashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) ResourceDescriptor(com.google.api.ResourceDescriptor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 78 with ResourceName

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

the class ResourceReferenceParser method parseParentResourceName.

@VisibleForTesting
static Optional<ResourceName> parseParentResourceName(String pattern, String servicePackage, String resourcePackage, String resourceTypeString, @Nullable String description, Map<String, ResourceName> patternsToResourceNames) {
    Optional<String> parentPatternOpt = ResourceReferenceUtils.parseParentPattern(pattern);
    if (!parentPatternOpt.isPresent()) {
        return Optional.empty();
    }
    String parentPattern = parentPatternOpt.get();
    if (patternsToResourceNames.get(parentPattern) != null) {
        return Optional.of(patternsToResourceNames.get(parentPattern));
    }
    String[] tokens = parentPattern.split(SLASH);
    int numTokens = tokens.length;
    String lastToken = tokens[numTokens - 1];
    Set<String> variableNames = PathTemplate.create(parentPattern).vars();
    String parentVariableName = null;
    // E.g. Profile is the parent of users/{user}/profiles/{profile}/blurbs/{blurb}.
    for (String variableName : variableNames) {
        if (lastToken.contains(variableName)) {
            parentVariableName = variableName;
        }
    }
    // That is, they will not appear in the parent components under consideration.
    if (Strings.isNullOrEmpty(parentVariableName)) {
        String lowerTypeName = resourceTypeString.substring(resourceTypeString.indexOf(SLASH) + 1).toLowerCase();
        // We're curerntly at users/{user}/profile/blurbs.
        if ((lastToken.endsWith("s") || lastToken.contains(lowerTypeName)) && numTokens > 2) {
            // Not the singleton we're looking for, back up.
            parentVariableName = tokens[numTokens - 2];
        } else {
            // Check for the parent of users/{user}/profile/blurbs/{blurb}.
            // We're curerntly at users/{user}/profile.
            parentVariableName = lastToken;
        }
        parentVariableName = parentVariableName.replace(LEFT_BRACE, EMPTY_STRING).replace(RIGHT_BRACE, EMPTY_STRING);
    }
    Preconditions.checkNotNull(parentVariableName, String.format("Could not parse variable name from pattern %s", parentPattern));
    // Use the package where the resource was defined, only if that is a sub-package of the
    // current service (which is assumed to be the project's package).
    String pakkage = resolvePackages(resourcePackage, servicePackage);
    String parentResourceTypeString = String.format("%s/%s", resourceTypeString.substring(0, resourceTypeString.indexOf(SLASH)), JavaStyle.toUpperCamelCase(parentVariableName));
    ResourceName parentResourceName = ResourceName.builder().setVariableName(parentVariableName).setPakkage(pakkage).setResourceTypeString(parentResourceTypeString).setPatterns(Arrays.asList(parentPattern)).setDescription(description).build();
    patternsToResourceNames.put(parentPattern, parentResourceName);
    return Optional.of(parentResourceName);
}
Also used : ResourceName(com.google.api.generator.gapic.model.ResourceName) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 79 with ResourceName

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

the class ResourceReferenceParser method parseResourceNames.

public static List<ResourceName> parseResourceNames(ResourceReference resourceReference, String servicePackage, @Nullable String description, Map<String, ResourceName> resourceNames, Map<String, ResourceName> patternsToResourceNames) {
    ResourceName resourceName = null;
    if (resourceReference.isOnlyWildcard()) {
        resourceName = ResourceName.createWildcard("*", "com.google.api.wildcard.placeholder");
        resourceNames.put(resourceName.resourceTypeString(), resourceName);
    } else {
        resourceName = resourceNames.get(resourceReference.resourceTypeString());
    }
    // example.com/FooBar.
    if (resourceReference.resourceTypeString().indexOf(SLASH) < 0) {
        Optional<String> actualResourceTypeNameOpt = resourceNames.keySet().stream().filter(k -> k.substring(k.lastIndexOf(SLASH) + 1).equals(resourceReference.resourceTypeString())).findFirst();
        if (actualResourceTypeNameOpt.isPresent()) {
            resourceName = resourceNames.get(actualResourceTypeNameOpt.get());
        }
    } else {
        resourceName = resourceNames.get(resourceReference.resourceTypeString());
    }
    Preconditions.checkNotNull(resourceName, String.format("No resource definition found for reference with type %s", resourceReference.resourceTypeString()));
    if (!resourceReference.isChildType() || resourceName.isOnlyWildcard()) {
        return Arrays.asList(resourceName);
    }
    // Create a parent ResourceName for each pattern.
    List<ResourceName> parentResourceNames = new ArrayList<>();
    Set<String> resourceTypeStrings = new HashSet<>();
    for (String pattern : resourceName.patterns()) {
        Optional<ResourceName> parentResourceNameOpt = parseParentResourceName(pattern, servicePackage, resourceName.pakkage(), resourceName.resourceTypeString(), description, patternsToResourceNames);
        // Prevent duplicates.
        if (parentResourceNameOpt.isPresent() && !resourceTypeStrings.contains(parentResourceNameOpt.get().resourceTypeString())) {
            ResourceName parentResourceName = parentResourceNameOpt.get();
            parentResourceNames.add(parentResourceName);
            resourceTypeStrings.add(parentResourceName.resourceTypeString());
        }
    }
    return parentResourceNames;
}
Also used : Arrays(java.util.Arrays) ResourceReference(com.google.api.generator.gapic.model.ResourceReference) Set(java.util.Set) PathTemplate(com.google.api.pathtemplate.PathTemplate) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) List(java.util.List) Map(java.util.Map) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) Optional(java.util.Optional) ResourceReferenceUtils(com.google.api.generator.gapic.utils.ResourceReferenceUtils) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable) ResourceName(com.google.api.generator.gapic.model.ResourceName) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 80 with ResourceName

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

the class ServiceClientCallableMethodSampleComposerTest method invalid_composeStreamCallableMethod_bidiStreamNotExistRequest.

@Test
public void invalid_composeStreamCallableMethod_bidiStreamNotExistRequest() {
    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("NotExistRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("EchoResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    Method method = Method.builder().setName("chat").setInputType(inputType).setOutputType(outputType).setStream(Method.Stream.BIDI).build();
    Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(method, clientType, resourceNames, messageTypes));
}
Also used : Message(com.google.api.generator.gapic.model.Message) 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

ResourceName (com.google.api.generator.gapic.model.ResourceName)112 Message (com.google.api.generator.gapic.model.Message)82 Test (org.junit.Test)76 TypeNode (com.google.api.generator.engine.ast.TypeNode)58 Method (com.google.api.generator.gapic.model.Method)50 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)47 HashSet (java.util.HashSet)35 Descriptors (com.google.protobuf.Descriptors)30 List (java.util.List)29 Expr (com.google.api.generator.engine.ast.Expr)28 Service (com.google.api.generator.gapic.model.Service)28 HashMap (java.util.HashMap)28 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)20 ArrayList (java.util.ArrayList)19 Preconditions (com.google.common.base.Preconditions)18 Path (java.nio.file.Path)18 Map (java.util.Map)18 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)17 Field (com.google.api.generator.gapic.model.Field)17 GapicClass (com.google.api.generator.gapic.model.GapicClass)17