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