Search in sources :

Example 6 with DiagCollector

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;
}
Also used : MessageGenerator(com.google.api.codegen.configgen.MessageGenerator) SimpleDiagCollector(com.google.api.tools.framework.model.SimpleDiagCollector) DiagCollector(com.google.api.tools.framework.model.DiagCollector) ConfigHelper(com.google.api.codegen.configgen.ConfigHelper) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) File(java.io.File) SimpleDiagCollector(com.google.api.tools.framework.model.SimpleDiagCollector) ConfigYamlReader(com.google.api.codegen.configgen.ConfigYamlReader)

Example 7 with DiagCollector

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);
    }
}
Also used : SurfaceTreatmentProto(com.google.api.codegen.SurfaceTreatmentProto) ArrayList(java.util.ArrayList) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) Duration(org.threeten.bp.Duration) ReleaseLevel(com.google.api.codegen.ReleaseLevel) DiagCollector(com.google.api.tools.framework.model.DiagCollector) Nullable(javax.annotation.Nullable)

Example 8 with DiagCollector

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");
}
Also used : BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) MethodConfigProto(com.google.api.codegen.MethodConfigProto) CollectionConfigProto(com.google.api.codegen.CollectionConfigProto) ResourceNameMessageConfigProto(com.google.api.codegen.ResourceNameMessageConfigProto) ConfigProto(com.google.api.codegen.ConfigProto) InterfaceConfigProto(com.google.api.codegen.InterfaceConfigProto) FlatteningConfigProto(com.google.api.codegen.FlatteningConfigProto) Test(org.junit.Test)

Example 9 with DiagCollector

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));
}
Also used : TypeRef(com.google.api.tools.framework.model.TypeRef) CollectionOneofProto(com.google.api.codegen.CollectionOneofProto) Diag(com.google.api.tools.framework.model.Diag) Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) MethodConfigProto(com.google.api.codegen.MethodConfigProto) MessageType(com.google.api.tools.framework.model.MessageType) CollectionConfigProto(com.google.api.codegen.CollectionConfigProto) ProtoFile(com.google.api.tools.framework.model.ProtoFile) Field(com.google.api.tools.framework.model.Field) Method(com.google.api.tools.framework.model.Method) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Spy(org.mockito.Spy) ResourceNameMessageConfigProto(com.google.api.codegen.ResourceNameMessageConfigProto) TargetLanguage(com.google.api.codegen.common.TargetLanguage) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) ConfigProto(com.google.api.codegen.ConfigProto) Kind(com.google.api.tools.framework.model.Diag.Kind) Before(org.junit.Before) ResourceDescriptor(com.google.api.ResourceDescriptor) ImmutableSet(com.google.common.collect.ImmutableSet) ResourceReference(com.google.api.ResourceReference) ImmutableMap(com.google.common.collect.ImmutableMap) InterfaceConfigProto(com.google.api.codegen.InterfaceConfigProto) FlatteningGroupProto(com.google.api.codegen.FlatteningGroupProto) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Name(com.google.api.codegen.util.Name) Mockito(org.mockito.Mockito) List(java.util.List) DiagCollector(com.google.api.tools.framework.model.DiagCollector) Interface(com.google.api.tools.framework.model.Interface) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) ProtoParser(com.google.api.codegen.util.ProtoParser) Comparator(java.util.Comparator) FlatteningConfigProto(com.google.api.codegen.FlatteningConfigProto) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) Collections(java.util.Collections) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) ArrayList(java.util.ArrayList) Diag(com.google.api.tools.framework.model.Diag) Field(com.google.api.tools.framework.model.Field) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) MethodConfigProto(com.google.api.codegen.MethodConfigProto) Test(org.junit.Test)

Example 10 with DiagCollector

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);
}
Also used : BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) Test(org.junit.Test)

Aggregations

DiagCollector (com.google.api.tools.framework.model.DiagCollector)17 BoundedDiagCollector (com.google.api.tools.framework.model.BoundedDiagCollector)11 Test (org.junit.Test)10 ConfigProto (com.google.api.codegen.ConfigProto)3 ResourceNameTreatment (com.google.api.codegen.ResourceNameTreatment)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 CollectionConfigProto (com.google.api.codegen.CollectionConfigProto)2 FlatteningConfigProto (com.google.api.codegen.FlatteningConfigProto)2 InterfaceConfigProto (com.google.api.codegen.InterfaceConfigProto)2 MethodConfigProto (com.google.api.codegen.MethodConfigProto)2 ReleaseLevel (com.google.api.codegen.ReleaseLevel)2 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)2 SurfaceTreatmentProto (com.google.api.codegen.SurfaceTreatmentProto)2 SimpleDiagCollector (com.google.api.tools.framework.model.SimpleDiagCollector)2 ImmutableList (com.google.common.collect.ImmutableList)2 File (java.io.File)2 Nullable (javax.annotation.Nullable)2 ResourceDescriptor (com.google.api.ResourceDescriptor)1 ResourceReference (com.google.api.ResourceReference)1