use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.
the class JavaDiscoGapicResourceNameToViewTransformer method transform.
@Override
public List<ViewModel> transform(DiscoApiModel apiModel, GapicProductConfig productConfig) {
List<ViewModel> surfaceRequests = new ArrayList<>();
String packageName = productConfig.getPackageName();
SurfaceNamer surfaceNamer = new JavaSurfaceNamer(packageName, packageName, nameFormatter);
DiscoGapicInterfaceContext context = DiscoGapicInterfaceContext.createWithoutInterface(apiModel, productConfig, createTypeTable(productConfig.getPackageName(), surfaceNamer), surfaceNamer, JavaFeatureConfig.newBuilder().enableStringFormatFunctions(true).build());
// Keep track of which name patterns have been generated to avoid duplicate classes.
Set<String> namePatterns = new HashSet<>();
for (String interfaceName : productConfig.getInterfaceConfigMap().keySet()) {
SchemaTransformationContext requestContext = SchemaTransformationContext.create(interfaceName, context.getSchemaTypeTable(), context);
// Maps a canonical resource name pattern to any method that uses that pattern.
Map<String, Method> namePatternsToMethod = new HashMap<>();
for (MethodConfig methodConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getMethodConfigs()) {
Method method = ((DiscoveryMethodModel) methodConfig.getMethodModel()).getDiscoMethod();
namePatternsToMethod.put(DiscoGapicParser.getCanonicalPath(method), method);
}
for (SingleResourceNameConfig nameConfig : productConfig.getInterfaceConfigMap().get(interfaceName).getSingleResourceNameConfigs()) {
String namePattern = nameConfig.getNamePattern();
if (namePatterns.contains(namePattern)) {
continue;
}
Method method = namePatternsToMethod.get(namePattern);
StaticLangApiResourceNameView resourceNameView = generateResourceNameClass(requestContext, method, nameConfig);
surfaceRequests.add(generateResourceNameFile(requestContext, resourceNameView));
namePatterns.add(nameConfig.getNamePattern());
}
}
Collections.sort(surfaceRequests, new Comparator<ViewModel>() {
@Override
public int compare(ViewModel o1, ViewModel o2) {
return String.CASE_INSENSITIVE_ORDER.compare(o1.outputPath(), o2.outputPath());
}
});
return surfaceRequests;
}
use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.
the class ResourceNameMessageConfigs method createMessageResourceTypesConfig.
static ResourceNameMessageConfigs createMessageResourceTypesConfig(DiscoApiModel model, ConfigProto configProto, String defaultPackage, SurfaceNamer languageNamer) {
ImmutableMap.Builder<String, ResourceNameMessageConfig> builder = ImmutableMap.builder();
for (ResourceNameMessageConfigProto messageResourceTypesProto : configProto.getResourceNameGenerationList()) {
ResourceNameMessageConfig messageResourceTypeConfig = ResourceNameMessageConfig.createResourceNameMessageConfig(model.getDiagCollector(), messageResourceTypesProto, defaultPackage);
builder.put(messageResourceTypeConfig.messageName(), messageResourceTypeConfig);
}
ImmutableMap<String, ResourceNameMessageConfig> messageResourceTypeConfigMap = builder.build();
ListMultimap<String, FieldModel> fieldsByMessage = ArrayListMultimap.create();
DiscoGapicNamer discoGapicNamer = new DiscoGapicNamer();
for (Method method : model.getDocument().methods()) {
String fullName = discoGapicNamer.getRequestTypeName(method, languageNamer).getFullName();
ResourceNameMessageConfig messageConfig = messageResourceTypeConfigMap.get(fullName);
if (messageConfig == null) {
continue;
}
for (Schema property : method.parameters().values()) {
if (messageConfig.getEntityNameForField(property.getIdentifier()) != null) {
fieldsByMessage.put(fullName, DiscoveryField.create(property, model));
}
}
}
return new AutoValue_ResourceNameMessageConfigs(messageResourceTypeConfigMap, fieldsByMessage);
}
use of com.google.api.codegen.discovery.Method in project toolkit by googleapis.
the class DiscoConfigTransformer method generateConfig.
public ViewModel generateConfig(DiscoApiModel model, String outputPath) {
// Map of methods to unique resource names.
Map<Method, Name> methodToResourceName = new HashMap<>();
// Map of Methods to resource name patterns.
ImmutableMap.Builder<Method, String> methodToNamePattern = ImmutableMap.builder();
// Maps visited simple resource names to canonical name patterns. Used to check for collisions in simple resource names.
Map<Name, String> simpleResourceToFirstPatternMap = new HashMap<>();
for (Method method : model.getDocument().methods()) {
String namePattern = DiscoGapicParser.getCanonicalPath(method);
methodToNamePattern.put(method, namePattern);
Name simpleResourceName = DiscoGapicParser.getResourceIdentifier(method.flatPath());
String collisionPattern = simpleResourceToFirstPatternMap.get(simpleResourceName);
if (collisionPattern != null && !collisionPattern.equals(namePattern)) {
// Collision with another path template with the same simple resource name; qualify this resource name.
Name qualifiedResourceName = DiscoGapicParser.getQualifiedResourceIdentifier(namePattern);
methodToResourceName.put(method, qualifiedResourceName);
} else {
simpleResourceToFirstPatternMap.put(simpleResourceName, namePattern);
methodToResourceName.put(method, simpleResourceName);
}
}
// Map of base resource identifiers to all canonical name patterns that use that identifier.
ImmutableMap<Method, Name> methodToResourceNames = ImmutableMap.<Method, Name>builder().putAll(methodToResourceName).build();
return ConfigView.newBuilder().templateFileName(CONFIG_TEMPLATE_FILE).outputPath(outputPath).type(CONFIG_PROTO_TYPE).configSchemaVersion(DEFAULT_CONFIG_SCHEMA_VERSION).languageSettings(generateLanguageSettings(model.getDocument())).license(generateLicense()).interfaces(generateInterfaces(model, methodToNamePattern.build(), methodToResourceNames)).resourceNameGeneration(generateResourceNameGenerations(model.getDocument(), methodToResourceNames)).build();
}
Aggregations