Search in sources :

Example 1 with ResourceDescriptor

use of com.google.api.ResourceDescriptor in project toolkit by googleapis.

the class ProtoParser method getResourceDescriptorConfigMap.

/**
 * Return a Map of Unified Resource Types to a ResourceDescriptorConfig object.
 */
public Map<String, ResourceDescriptorConfig> getResourceDescriptorConfigMap(List<ProtoFile> protoFiles, DiagCollector diagCollector) {
    ImmutableMap.Builder<String, ResourceDescriptorConfig> mapBuilder = ImmutableMap.builder();
    // Maps base names to ResourceDescriptors. Used to check redeclarations.
    Map<String, ResourceDescriptor> localDefs = new LinkedHashMap<>();
    // Skip unnecessary file parsing.
    if (!enableProtoAnnotations)
        return mapBuilder.build();
    for (ProtoFile protoFile : protoFiles) {
        List<ResourceDescriptor> resourceDescriptors = new ArrayList<>();
        // Get Resource[Set] definitions from file-level annotations.
        for (ResourceDescriptor definition : getFileLevelResourceDescriptors(protoFile)) {
            collectResourceDescriptor(diagCollector, localDefs, mapBuilder, definition, protoFile, false);
        }
        // Get Resource[Set] definitions from fields in message types.
        for (MessageType message : protoFile.getMessages()) {
            ResourceDescriptor definition = getResourceDescriptor(message);
            if (definition != null) {
                collectResourceDescriptor(diagCollector, localDefs, mapBuilder, definition, protoFile, true);
            }
        }
    }
    return mapBuilder.build();
}
Also used : ProtoFile(com.google.api.tools.framework.model.ProtoFile) ArrayList(java.util.ArrayList) ResourceDescriptorConfig(com.google.api.codegen.config.ResourceDescriptorConfig) ImmutableMap(com.google.common.collect.ImmutableMap) MessageType(com.google.api.tools.framework.model.MessageType) ResourceDescriptor(com.google.api.ResourceDescriptor) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ResourceDescriptor

use of com.google.api.ResourceDescriptor in project toolkit by googleapis.

the class ResourceNameMessageConfigs method createFromAnnotations.

@VisibleForTesting
static ResourceNameMessageConfigs createFromAnnotations(DiagCollector diagCollector, List<ProtoFile> protoFiles, Map<String, ResourceNameConfig> resourceNameConfigs, ProtoParser parser, Map<String, ResourceDescriptorConfig> descriptorConfigMap, Map<String, List<ResourceDescriptorConfig>> childParentResourceMap) {
    ImmutableMap.Builder<String, ResourceNameMessageConfig> builder = ImmutableMap.builder();
    for (ProtoFile protoFile : protoFiles) {
        for (MessageType message : protoFile.getMessages()) {
            ImmutableListMultimap.Builder<String, String> fieldEntityMapBuilder = ImmutableListMultimap.builder();
            // Handle resource definitions.
            ResourceDescriptor resourceDescriptor = parser.getResourceDescriptor(message);
            if (resourceDescriptor != null) {
                loadFieldEntityPairFromResourceAnnotation(fieldEntityMapBuilder, resourceDescriptor, message);
            }
            // Handle resource references.
            loadFieldEntityPairFromResourceReferenceAnnotation(fieldEntityMapBuilder, parser, message, resourceNameConfigs, descriptorConfigMap, childParentResourceMap);
            ImmutableListMultimap<String, String> fieldEntityMap = fieldEntityMapBuilder.build();
            if (fieldEntityMap.size() > 0) {
                ResourceNameMessageConfig messageConfig = new AutoValue_ResourceNameMessageConfig(message.getFullName(), fieldEntityMap);
                builder.put(messageConfig.messageName(), messageConfig);
            }
        }
    }
    ImmutableMap<String, ResourceNameMessageConfig> map = builder.build();
    return new AutoValue_ResourceNameMessageConfigs(map, createFieldsByMessage(protoFiles, map));
}
Also used : ProtoFile(com.google.api.tools.framework.model.ProtoFile) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) MessageType(com.google.api.tools.framework.model.MessageType) ResourceDescriptor(com.google.api.ResourceDescriptor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ResourceDescriptor

use of com.google.api.ResourceDescriptor in project toolkit by googleapis.

the class ResourceDescriptorConfigTest method testFromResourceDescriptor.

@Test
public void testFromResourceDescriptor() {
    ResourceDescriptor.Builder descriptorBuilder = ResourceDescriptor.newBuilder().setType("abc/Def");
    ResourceDescriptor descriptor = descriptorBuilder.addPattern("foos/{foo}").build();
    ResourceDescriptorConfig config = ResourceDescriptorConfig.from(descriptor, protoFile, true);
    assertThat(config.getRequiresOneofConfig()).isFalse();
    assertThat(config.getSinglePattern()).isEqualTo("foos/{foo}");
    descriptor = descriptorBuilder.clearPattern().addPattern("foos/{foo}").addPattern("foos/{foo}/bars/{bar}").build();
    config = ResourceDescriptorConfig.from(descriptor, protoFile, true);
    assertThat(config.getRequiresOneofConfig()).isTrue();
    assertThat(config.getSinglePattern()).isEqualTo("");
    descriptor = descriptorBuilder.clearPattern().addPattern("foos/{foo}").setHistory(ResourceDescriptor.History.ORIGINALLY_SINGLE_PATTERN).build();
    config = ResourceDescriptorConfig.from(descriptor, protoFile, true);
    assertThat(config.getRequiresOneofConfig()).isFalse();
    assertThat(config.getSinglePattern()).isEqualTo("foos/{foo}");
    descriptor = descriptorBuilder.clearPattern().addPattern("foos/{foo}").addPattern("foos/{foo}/bars/{bar}").setHistory(ResourceDescriptor.History.ORIGINALLY_SINGLE_PATTERN).build();
    config = ResourceDescriptorConfig.from(descriptor, protoFile, true);
    assertThat(config.getRequiresOneofConfig()).isTrue();
    assertThat(config.getSinglePattern()).isEqualTo("foos/{foo}");
    descriptor = descriptorBuilder.clearPattern().addPattern("foos/{foo}").setHistory(ResourceDescriptor.History.FUTURE_MULTI_PATTERN).build();
    config = ResourceDescriptorConfig.from(descriptor, protoFile, true);
    assertThat(config.getRequiresOneofConfig()).isTrue();
    assertThat(config.getSinglePattern()).isEqualTo("");
    descriptor = descriptorBuilder.clearPattern().addPattern("foos/{foo}").addPattern("foos/{foo}/bars/{bar}").setHistory(ResourceDescriptor.History.FUTURE_MULTI_PATTERN).build();
    config = ResourceDescriptorConfig.from(descriptor, protoFile, true);
    assertThat(config.getRequiresOneofConfig()).isTrue();
    assertThat(config.getSinglePattern()).isEqualTo("");
}
Also used : ResourceDescriptor(com.google.api.ResourceDescriptor) Test(org.junit.Test)

Aggregations

ResourceDescriptor (com.google.api.ResourceDescriptor)3 MessageType (com.google.api.tools.framework.model.MessageType)2 ProtoFile (com.google.api.tools.framework.model.ProtoFile)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ResourceDescriptorConfig (com.google.api.codegen.config.ResourceDescriptorConfig)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1