use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class DiscoGapicGeneratorApi method loadConfigFromFiles.
private static ConfigProto loadConfigFromFiles(List<String> configFileNames) {
DiagCollector diagCollector = new SimpleDiagCollector();
ConfigYamlReader yamlReader = new ConfigYamlReader();
MessageGenerator messageGenerator = new MessageGenerator(ConfigProto.newBuilder());
for (File file : pathsToFiles(configFileNames)) {
ConfigHelper helper = new ConfigHelper(diagCollector, file.getName());
ConfigNode configNode = yamlReader.generateConfigNode(file, helper);
if (configNode == null) {
continue;
}
messageGenerator.visit(configNode.getChild());
}
ConfigProto configProto = (ConfigProto) messageGenerator.getValue();
if (configProto == null || configProto.equals(ConfigProto.getDefaultInstance())) {
return null;
}
return configProto;
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class DiscoGapicMethodConfig method createDiscoGapicMethodConfig.
/**
* Creates an instance of DiscoGapicMethodConfig based on MethodConfigProto, linking it up with
* the provided method. On errors, null will be returned, and diagnostics are reported to the diag
* collector.
*/
@Nullable
static DiscoGapicMethodConfig createDiscoGapicMethodConfig(DiscoApiModel apiModel, String language, MethodConfigProto methodConfigProto, Method method, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, ImmutableSet<String> retryCodesConfigNames, ImmutableSet<String> retryParamsConfigNames) {
boolean error = false;
DiscoveryMethodModel methodModel = new DiscoveryMethodModel(method, apiModel);
DiagCollector diagCollector = apiModel.getDiagCollector();
PageStreamingConfig pageStreaming = null;
if (!PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getPageStreaming())) {
pageStreaming = PageStreamingConfig.createPageStreaming(apiModel, method);
if (pageStreaming == null) {
error = true;
}
}
ImmutableList<FlatteningConfig> flattening = null;
if (!FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
flattening = createFlattening(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
if (flattening == null) {
error = true;
}
}
BatchingConfig batching = null;
if (!BatchingConfigProto.getDefaultInstance().equals(methodConfigProto.getBatching())) {
batching = BatchingConfig.createBatching(diagCollector, methodConfigProto.getBatching(), methodModel);
if (batching == null) {
error = true;
}
}
String retryCodesName = methodConfigProto.getRetryCodesName();
if (!retryCodesName.isEmpty() && !retryCodesConfigNames.contains(retryCodesName)) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Retry codes config used but not defined: '%s' (in method %s)", retryCodesName, methodModel.getFullName()));
error = true;
}
String retryParamsName = methodConfigProto.getRetryParamsName();
if (!retryParamsConfigNames.isEmpty() && !retryParamsConfigNames.contains(retryParamsName)) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Retry parameters config used but not defined: %s (in method %s)", retryParamsName, methodModel.getFullName()));
error = true;
}
Duration timeout = Duration.ofMillis(methodConfigProto.getTimeoutMillis());
if (timeout.toMillis() <= 0) {
diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Default timeout not found or has invalid value (in method %s)", methodModel.getFullName()));
error = true;
}
boolean hasRequestObjectMethod = methodConfigProto.getRequestObjectMethod();
ImmutableMap<String, String> fieldNamePatterns = ImmutableMap.copyOf(methodConfigProto.getFieldNamePatterns());
ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
defaultResourceNameTreatment = ResourceNameTreatment.NONE;
}
Iterable<FieldConfig> requiredFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getRequiredFields(diagCollector, methodModel, methodConfigProto.getRequiredFieldsList()));
Iterable<FieldConfig> optionalFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getOptionalFields(methodModel, methodConfigProto.getRequiredFieldsList()));
List<String> sampleCodeInitFields = new ArrayList<>();
sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());
VisibilityConfig visibility = VisibilityConfig.PUBLIC;
ReleaseLevel releaseLevel = ReleaseLevel.ALPHA;
for (SurfaceTreatmentProto treatment : methodConfigProto.getSurfaceTreatmentsList()) {
if (!treatment.getIncludeLanguagesList().contains(language)) {
continue;
}
if (treatment.getVisibility() != VisibilityProto.UNSET_VISIBILITY) {
visibility = VisibilityConfig.fromProto(treatment.getVisibility());
}
if (treatment.getReleaseLevel() != ReleaseLevel.UNSET_RELEASE_LEVEL) {
releaseLevel = treatment.getReleaseLevel();
}
}
LongRunningConfig longRunningConfig = null;
if (error) {
return null;
} else {
return new AutoValue_DiscoGapicMethodConfig(methodModel, pageStreaming, flattening, retryCodesName, retryParamsName, timeout, requiredFieldConfigs, optionalFieldConfigs, defaultResourceNameTreatment, batching, hasRequestObjectMethod, fieldNamePatterns, sampleCodeInitFields, visibility, releaseLevel, longRunningConfig);
}
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class ResourceNameMessageConfigsTest method testCreateResourceNamesWithConfigOnly.
@Test
public void testCreateResourceNamesWithConfigOnly() {
DiagCollector diagCollector = new BoundedDiagCollector();
ConfigProto configProto = ConfigProto.newBuilder().addResourceNameGeneration(ResourceNameMessageConfigProto.newBuilder().setMessageName("Book").putFieldEntityMap("name", "book")).addResourceNameGeneration(ResourceNameMessageConfigProto.newBuilder().setMessageName("BookFromAnywhere").putFieldEntityMap("name", "book_oneof")).addResourceNameGeneration(ResourceNameMessageConfigProto.newBuilder().setMessageName("Shelf").putFieldEntityMap("name", "shelf")).addCollectionOneofs(CollectionOneofProto.newBuilder().setOneofName("book_oneof").addAllCollectionNames(Arrays.asList("book", "archived_book")).build()).build();
String defaultPackage = "library";
ResourceNameMessageConfigs messageConfigs = ResourceNameMessageConfigs.createFromGapicConfigOnly(ImmutableList.of(), configProto, defaultPackage);
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
assertThat(messageConfigs).isNotNull();
assertThat(messageConfigs.getResourceTypeConfigMap().size()).isEqualTo(3);
ResourceNameMessageConfig bookResource = messageConfigs.getResourceTypeConfigMap().get("library.Book");
assertThat(bookResource.getEntityNamesForField("name").get(0)).isEqualTo("book");
ResourceNameMessageConfig getShelfRequestObject = messageConfigs.getResourceTypeConfigMap().get("library.BookFromAnywhere");
assertThat(getShelfRequestObject.getEntityNamesForField("name").get(0)).isEqualTo("book_oneof");
ResourceNameMessageConfig shelfResource = messageConfigs.getResourceTypeConfigMap().get("library.Shelf");
assertThat(shelfResource.getEntityNamesForField("name").get(0)).isEqualTo("shelf");
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class ResourceNameMessageConfigsTest method testCreateFlattenings.
@Test
public void testCreateFlattenings() {
ProtoMethodModel methodModel = new ProtoMethodModel(createShelvesMethod);
Field bookField = Mockito.mock(Field.class);
Mockito.when(bookField.getType()).thenReturn(TypeRef.of(bookType));
Mockito.when(bookField.getParent()).thenReturn(createShelvesRequest);
Mockito.when(bookField.getSimpleName()).thenReturn("book");
Mockito.when(bookType.getFields()).thenReturn(ImmutableList.of());
Field nameField = Mockito.mock(Field.class);
Mockito.when(nameField.getParent()).thenReturn(createShelvesRequest);
Mockito.when(createShelvesRequest.getFullName()).thenReturn("library.CreateShelvesRequest");
Mockito.when(nameField.getType()).thenReturn(TypeRef.fromPrimitiveName("string"));
Mockito.when(nameField.getSimpleName()).thenReturn("name");
Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Shelf").build()).when(protoParser).getResourceReference(nameField);
Mockito.doReturn(true).when(protoParser).hasResourceReference(nameField);
Mockito.when(createShelvesRequest.lookupField("book")).thenReturn(bookField);
Mockito.when(createShelvesRequest.lookupField("name")).thenReturn(nameField);
Mockito.when(createShelvesRequest.getFields()).thenReturn(ImmutableList.of(bookField, nameField));
Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Shelf").build()).when(protoParser).getResourceReference(shelfName);
Mockito.doReturn(true).when(protoParser).hasResourceReference(shelfName);
Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Book").build()).when(protoParser).getResourceReference(bookField);
Mockito.doReturn(true).when(protoParser).hasResourceReference(bookField);
Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Shelf").build()).when(protoParser).getResourceReference(nameField);
Mockito.doReturn(true).when(protoParser).hasResourceReference(nameField);
// ProtoFile contributes flattenings {["name", "book"], ["name"]}.
Mockito.doReturn(Arrays.asList(Arrays.asList("name", "book"), Arrays.asList("name"))).when(protoParser).getMethodSignatures(createShelvesMethod);
Mockito.when(protoFile.getMessages()).thenReturn(ImmutableList.of(bookMessage, shelfMessage, createShelvesRequest));
// Gapic config contributes flattenings {["book"]}.
MethodConfigProto methodConfigProto = MethodConfigProto.newBuilder().setName(CREATE_SHELF_METHOD_NAME).setFlattening(FlatteningConfigProto.newBuilder().addGroups(FlatteningGroupProto.newBuilder().addAllParameters(Arrays.asList("book")))).setResourceNameTreatment(ResourceNameTreatment.STATIC_TYPES).build();
DiagCollector diagCollector = new BoundedDiagCollector();
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
ImmutableMap<String, ResourceNameConfig> resourceNameConfigs = GapicProductConfig.createResourceNameConfigsFromAnnotationsAndGapicConfig(null, diagCollector, ConfigProto.getDefaultInstance(), protoFile, TargetLanguage.CSHARP, resourceDescriptorConfigMap, resourceDescriptorConfigMap.keySet(), Collections.emptySet(), Collections.emptyMap(), patternResourceDescriptorMap, Collections.emptyMap(), "library");
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
ResourceNameMessageConfigs messageConfigs = ResourceNameMessageConfigs.createFromAnnotations(diagCollector, sourceProtoFiles, resourceNameConfigs, protoParser, resourceDescriptorConfigMap, Collections.emptyMap());
List<FlatteningConfig> flatteningConfigs = new ArrayList<>(FlatteningConfig.createFlatteningConfigs(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel, protoParser));
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
List<Diag> warningDiags = diagCollector.getDiags().stream().filter(d -> d.getKind().equals(Kind.WARNING)).collect(Collectors.toList());
assertThat(flatteningConfigs).isNotNull();
assertThat(flatteningConfigs.size()).isEqualTo(6);
// Check the flattening from the Gapic config.
List<FlatteningConfig> flatteningConfigFromGapicConfigs = flatteningConfigs.stream().filter(f -> f.getFlattenedFieldConfigs().size() == 1 && f.getFlattenedFieldConfigs().containsKey("book")).collect(ImmutableList.toImmutableList());
assertThat(flatteningConfigFromGapicConfigs.size()).isEqualTo(2);
for (FlatteningConfig configFromGapicConfig : flatteningConfigFromGapicConfigs) {
Map<String, FieldConfig> paramsFromGapicConfigFlattening = configFromGapicConfig.getFlattenedFieldConfigs();
assertThat(paramsFromGapicConfigFlattening.get("book").getField().getSimpleName()).isEqualTo("book");
assertThat(((ProtoField) paramsFromGapicConfigFlattening.get("book").getField()).getType().getProtoType().getMessageType()).isEqualTo(bookType);
}
flatteningConfigs.removeAll(flatteningConfigFromGapicConfigs);
// Check the flattenings from the protofile annotations.
flatteningConfigs.sort(Comparator.comparingInt(c -> Iterables.size(c.getFlattenedFields())));
FlatteningConfig shelfFlattening = flatteningConfigs.get(0);
assertThat(Iterables.size(shelfFlattening.getFlattenedFields())).isEqualTo(1);
FieldConfig nameConfig = shelfFlattening.getFlattenedFieldConfigs().get("name");
assertThat(nameConfig.getResourceNameTreatment()).isEqualTo(ResourceNameTreatment.STATIC_TYPES);
assertThat(((SingleResourceNameConfig) nameConfig.getResourceNameConfig()).getNamePattern()).isEqualTo(PROTO_SHELF_PATH);
FlatteningConfig shelfAndBookFlattening = flatteningConfigs.get(2);
assertThat(Iterables.size(shelfAndBookFlattening.getFlattenedFields())).isEqualTo(2);
FieldConfig nameConfig2 = shelfAndBookFlattening.getFlattenedFieldConfigs().get("name");
assertThat(nameConfig2.getResourceNameTreatment()).isEqualTo(ResourceNameTreatment.STATIC_TYPES);
// Use PROTO_SHELF_PATH over GAPIC_SHELF_PATH.
assertThat(((SingleResourceNameConfig) nameConfig2.getResourceNameConfig()).getNamePattern()).isEqualTo(PROTO_SHELF_PATH);
FieldConfig bookConfig = shelfAndBookFlattening.getFlattenedFieldConfigs().get("book");
assertThat(bookConfig.getResourceNameTreatment()).isEqualTo(ResourceNameTreatment.STATIC_TYPES);
// Use the resource name path from proto file.
assertThat(((SingleResourceNameConfig) bookConfig.getResourceNameConfig()).getNamePattern()).isEqualTo(PROTO_BOOK_PATH);
assertThat(((ProtoTypeRef) bookConfig.getField().getType()).getProtoType().getMessageType()).isEqualTo(bookType);
// Restore protoFile.getMessages()
Mockito.when(protoFile.getMessages()).thenReturn(ImmutableList.of(bookMessage, shelfMessage));
}
use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.
the class ResourceNameMessageConfigsTest method testCreateResourceNameConfigs.
@Test
public void testCreateResourceNameConfigs() {
DiagCollector diagCollector = new BoundedDiagCollector();
Map<String, ResourceNameConfig> resourceNameConfigs = GapicProductConfig.createResourceNameConfigsFromAnnotationsAndGapicConfig(null, diagCollector, configProtoV2, protoFile, TargetLanguage.CSHARP, resourceDescriptorConfigMap, resourceDescriptorConfigMap.keySet(), ImmutableSet.of(), Collections.emptyMap(), patternResourceDescriptorMap, Collections.emptyMap(), "library");
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
assertThat(resourceNameConfigs.size()).isEqualTo(3);
assertThat(((SingleResourceNameConfig) resourceNameConfigs.get("Book")).getNamePattern()).isEqualTo(PROTO_BOOK_PATH);
assertThat(((SingleResourceNameConfig) resourceNameConfigs.get("ArchivedBook")).getNamePattern()).isEqualTo(PROTO_ARCHIVED_BOOK_PATH);
assertThat(((SingleResourceNameConfig) resourceNameConfigs.get("Shelf")).getNamePattern()).isEqualTo(PROTO_SHELF_PATH);
// "Book" is the name from the unnamed Resource in the Book message type.
SingleResourceNameConfig bookResourcenameConfigFromProtoFile = (SingleResourceNameConfig) resourceNameConfigs.get("Book");
assertThat(bookResourcenameConfigFromProtoFile.getNamePattern()).isEqualTo(PROTO_BOOK_PATH);
assertThat(diagCollector.getErrorCount()).isEqualTo(0);
}
Aggregations