Search in sources :

Example 6 with MethodConfigProto

use of com.google.api.codegen.MethodConfigProto in project toolkit by googleapis.

the class GapicProductConfig method findMethodsToGenerateWithConfigYaml.

/**
 * Find the methods that should be generated on the surface when a GAPIC config was given.
 */
@Nullable
private static ImmutableMap<Method, MethodConfigProto> findMethodsToGenerateWithConfigYaml(Interface apiInterface, InterfaceConfigProto interfaceConfigProto, TargetLanguage targetLanguage, DiagCollector diagCollector) {
    LinkedHashMap<Method, MethodConfigProto> methodMap = new LinkedHashMap<>();
    // Exclude these proto methods that are marked as DISABLE in the GAPIC config.
    Set<Method> gapicDisabledMethods = new HashSet<>();
    // Get the set of methods defined by the GAPIC config. Only these methods will be generated.
    for (MethodConfigProto methodConfigProto : interfaceConfigProto.getMethodsList()) {
        Interface targetInterface = GapicInterfaceConfig.getTargetInterface(apiInterface, methodConfigProto.getRerouteToGrpcInterface());
        Method protoMethod = targetInterface.lookupMethod(methodConfigProto.getName());
        if (protoMethod == null) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "method not found: %s", methodConfigProto.getName()));
            continue;
        }
        if (methodConfigProto.getSurfaceTreatmentsList().stream().anyMatch(s -> s.getVisibility().equals(VisibilityProto.DISABLED) && s.getIncludeLanguagesList().stream().anyMatch(lang -> lang.equalsIgnoreCase(targetLanguage.name())))) {
            gapicDisabledMethods.add(protoMethod);
            continue;
        }
        methodMap.put(protoMethod, methodConfigProto);
    }
    // Add the proto methods that do not have GAPIC configurations.
    for (Method method : apiInterface.getMethods()) {
        if (methodMap.containsKey(method) || gapicDisabledMethods.contains(method))
            continue;
        methodMap.put(method, MethodConfigProto.getDefaultInstance());
    }
    if (diagCollector.getErrorCount() > 0) {
        return null;
    }
    return ImmutableMap.copyOf(methodMap);
}
Also used : MethodConfigProto(com.google.api.codegen.MethodConfigProto) Nullable(javax.annotation.Nullable)

Example 7 with MethodConfigProto

use of com.google.api.codegen.MethodConfigProto in project toolkit by googleapis.

the class GapicInterfaceConfig method createMethodConfigMap.

private static ImmutableMap<String, GapicMethodConfig> createMethodConfigMap(DiagCollector diagCollector, String language, InterfaceConfigProto interfaceConfigProto, Interface apiInterface, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, ImmutableSet<String> retryCodesConfigNames, ImmutableSet<String> retryParamsConfigNames) {
    ImmutableMap.Builder<String, GapicMethodConfig> methodConfigMapBuilder = ImmutableMap.builder();
    for (MethodConfigProto methodConfigProto : interfaceConfigProto.getMethodsList()) {
        Interface targetInterface = getTargetInterface(apiInterface, methodConfigProto.getRerouteToGrpcInterface());
        Method method = targetInterface.lookupMethod(methodConfigProto.getName());
        if (method == null) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "method not found: %s", methodConfigProto.getName()));
            continue;
        }
        GapicMethodConfig methodConfig = GapicMethodConfig.createMethodConfig(diagCollector, language, methodConfigProto, method, messageConfigs, resourceNameConfigs, retryCodesConfigNames, retryParamsConfigNames);
        if (methodConfig == null) {
            continue;
        }
        methodConfigMapBuilder.put(methodConfigProto.getName(), methodConfig);
    }
    if (diagCollector.getErrorCount() > 0) {
        return null;
    } else {
        return methodConfigMapBuilder.build();
    }
}
Also used : MethodConfigProto(com.google.api.codegen.MethodConfigProto) Method(com.google.api.tools.framework.model.Method) ImmutableMap(com.google.common.collect.ImmutableMap) Interface(com.google.api.tools.framework.model.Interface)

Example 8 with MethodConfigProto

use of com.google.api.codegen.MethodConfigProto in project toolkit by googleapis.

the class GapicProductConfig method createGapicInterfaceInputList.

private static ImmutableList<GapicInterfaceInput> createGapicInterfaceInputList(DiagCollector diagCollector, TargetLanguage language, Iterable<Interface> interfaceList, Map<String, InterfaceConfigProto> interfaceConfigProtos) {
    // Store info about each Interface in a GapicInterfaceInput object.
    ImmutableList.Builder<GapicInterfaceInput> interfaceInputs = ImmutableList.builder();
    for (Interface apiInterface : interfaceList) {
        String serviceFullName = apiInterface.getFullName();
        GapicInterfaceInput.Builder interfaceInput = GapicInterfaceInput.newBuilder().setInterface(apiInterface);
        InterfaceConfigProto interfaceConfigProto = interfaceConfigProtos.getOrDefault(serviceFullName, InterfaceConfigProto.getDefaultInstance());
        interfaceInput.setInterfaceConfigProto(interfaceConfigProto);
        Map<Method, MethodConfigProto> methodsToGenerate;
        methodsToGenerate = findMethodsToGenerateWithConfigYaml(apiInterface, interfaceConfigProto, language, diagCollector);
        if (methodsToGenerate == null) {
            return null;
        }
        interfaceInput.setMethodsToGenerate(methodsToGenerate);
        interfaceInputs.add(interfaceInput.build());
    }
    return interfaceInputs.build();
}
Also used : InterfaceConfigProto(com.google.api.codegen.InterfaceConfigProto) ImmutableList(com.google.common.collect.ImmutableList) MethodConfigProto(com.google.api.codegen.MethodConfigProto)

Example 9 with MethodConfigProto

use of com.google.api.codegen.MethodConfigProto in project toolkit by googleapis.

the class GapicInterfaceConfig method createInterfaceConfig.

/**
 * Creates an instance of GapicInterfaceConfig based on ConfigProto, linking up method
 * configurations with specified methods in methodConfigMap. On errors, null will be returned, and
 * diagnostics are reported to the model.
 */
@Nullable
static GapicInterfaceConfig createInterfaceConfig(DiagCollector diagCollector, TargetLanguage language, TransportProtocol transportProtocol, String defaultPackageName, GapicInterfaceInput interfaceInput, String interfaceNameOverride, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, ProtoParser protoParser, GrpcGapicRetryMapping grpcGapicRetryMapping) {
    Interface apiInterface = interfaceInput.getInterface();
    Map<Method, MethodConfigProto> methodsToGenerate = interfaceInput.getMethodsToGenerate();
    InterfaceConfigProto interfaceConfigProto = interfaceInput.getInterfaceConfigProto();
    RetryCodesConfig retryCodesConfig;
    ImmutableMap<String, RetryParamsDefinitionProto> retrySettingsDefinition;
    if (grpcGapicRetryMapping != null) {
        // use the gRPC ServiceConfig retry mapping as the source
        retryCodesConfig = RetryCodesConfig.create(grpcGapicRetryMapping, interfaceConfigProto.getName());
        retrySettingsDefinition = RetryDefinitionsTransformer.createRetryDefinitionsFromGRPCServiceConfig(grpcGapicRetryMapping, interfaceConfigProto.getName());
    } else {
        // use the GAPIC config as the source of retry
        retryCodesConfig = RetryCodesConfig.create(interfaceConfigProto, new ArrayList<>(interfaceInput.getMethodsToGenerate().keySet()), protoParser);
        retrySettingsDefinition = RetryDefinitionsTransformer.createRetrySettingsDefinition(interfaceConfigProto);
    }
    ImmutableMap<String, GapicMethodConfig> methodConfigsMap;
    List<GapicMethodConfig> methodConfigs;
    if (retryCodesConfig != null && retrySettingsDefinition != null) {
        methodConfigsMap = createMethodConfigMap(diagCollector, language, transportProtocol, defaultPackageName, methodsToGenerate, messageConfigs, resourceNameConfigs, retryCodesConfig, retrySettingsDefinition.keySet(), protoParser, grpcGapicRetryMapping, interfaceInput.getInterfaceConfigProto().getName());
        if (methodConfigsMap == null) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Error constructing methodConfigMap"));
            return null;
        }
        methodConfigs = ImmutableList.copyOf(methodConfigsMap.values());
    } else {
        methodConfigsMap = ImmutableMap.of();
        methodConfigs = ImmutableList.of();
    }
    SmokeTestConfig smokeTestConfig = createSmokeTestConfig(diagCollector, apiInterface, interfaceConfigProto);
    ImmutableList<String> requiredConstructorParams = ImmutableList.copyOf(interfaceConfigProto.getRequiredConstructorParamsList());
    for (String param : interfaceConfigProto.getRequiredConstructorParamsList()) {
        if (!CONSTRUCTOR_PARAMS.contains(param)) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Unsupported constructor param: %s", param));
        }
    }
    ImmutableSet.Builder<SingleResourceNameConfig> resourcesBuilder = ImmutableSet.builder();
    if (!protoParser.isProtoAnnotationsEnabled()) {
        for (CollectionConfigProto collectionConfigProto : interfaceConfigProto.getCollectionsList()) {
            String entityName = collectionConfigProto.getEntityName();
            ResourceNameConfig resourceName = resourceNameConfigs.get(entityName);
            if (!(resourceName instanceof SingleResourceNameConfig)) {
                diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Inconsistent configuration - single resource name %s specified for interface, " + " but was not found in GapicProductConfig configuration.", entityName));
                return null;
            }
            resourcesBuilder.add((SingleResourceNameConfig) resourceName);
        }
    } else {
        addDeprecatedResources(diagCollector, resourcesBuilder, interfaceConfigProto, language);
    }
    ImmutableSet<SingleResourceNameConfig> singleResourceNames = resourcesBuilder.build();
    String manualDoc = Strings.nullToEmpty(interfaceConfigProto.getLangDoc().get(language.toString().toLowerCase())).trim();
    if (diagCollector.hasErrors()) {
        return null;
    } else {
        return new AutoValue_GapicInterfaceConfig(interfaceNameOverride, new ProtoInterfaceModel(apiInterface), methodConfigs, smokeTestConfig, methodConfigsMap, retryCodesConfig, retrySettingsDefinition, requiredConstructorParams, singleResourceNames, manualDoc);
    }
}
Also used : InterfaceConfigProto(com.google.api.codegen.InterfaceConfigProto) ArrayList(java.util.ArrayList) Method(com.google.api.tools.framework.model.Method) RetryParamsDefinitionProto(com.google.api.codegen.RetryParamsDefinitionProto) ImmutableSet(com.google.common.collect.ImmutableSet) MethodConfigProto(com.google.api.codegen.MethodConfigProto) Interface(com.google.api.tools.framework.model.Interface) CollectionConfigProto(com.google.api.codegen.CollectionConfigProto) DeprecatedCollectionConfigProto(com.google.api.codegen.DeprecatedCollectionConfigProto) Nullable(javax.annotation.Nullable)

Example 10 with MethodConfigProto

use of com.google.api.codegen.MethodConfigProto 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)

Aggregations

MethodConfigProto (com.google.api.codegen.MethodConfigProto)11 Method (com.google.api.tools.framework.model.Method)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Test (org.junit.Test)4 InterfaceConfigProto (com.google.api.codegen.InterfaceConfigProto)3 Interface (com.google.api.tools.framework.model.Interface)3 CollectionConfigProto (com.google.api.codegen.CollectionConfigProto)2 Method (com.google.api.codegen.discovery.Method)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ResourceDescriptor (com.google.api.ResourceDescriptor)1 ResourceReference (com.google.api.ResourceReference)1 CollectionOneofProto (com.google.api.codegen.CollectionOneofProto)1 ConfigProto (com.google.api.codegen.ConfigProto)1 DeprecatedCollectionConfigProto (com.google.api.codegen.DeprecatedCollectionConfigProto)1