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();
}
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));
}
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("");
}
Aggregations