Search in sources :

Example 6 with Method

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;
}
Also used : SchemaTransformationContext(com.google.api.codegen.discogapic.SchemaTransformationContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) StaticLangApiResourceNameView(com.google.api.codegen.viewmodel.StaticLangApiResourceNameView) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) MethodConfig(com.google.api.codegen.config.MethodConfig) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) DiscoGapicInterfaceContext(com.google.api.codegen.transformer.DiscoGapicInterfaceContext) HashSet(java.util.HashSet)

Example 7 with Method

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);
}
Also used : ResourceNameMessageConfigProto(com.google.api.codegen.ResourceNameMessageConfigProto) Schema(com.google.api.codegen.discovery.Schema) Method(com.google.api.codegen.discovery.Method) ImmutableMap(com.google.common.collect.ImmutableMap) DiscoGapicNamer(com.google.api.codegen.discogapic.transformer.DiscoGapicNamer)

Example 8 with Method

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();
}
Also used : HashMap(java.util.HashMap) Method(com.google.api.codegen.discovery.Method) ImmutableMap(com.google.common.collect.ImmutableMap) Name(com.google.api.codegen.util.Name)

Aggregations

Method (com.google.api.codegen.discovery.Method)8 ImmutableMap (com.google.common.collect.ImmutableMap)5 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)3 HashMap (java.util.HashMap)3 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)2 Schema (com.google.api.codegen.discovery.Schema)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 CollectionConfigProto (com.google.api.codegen.CollectionConfigProto)1 MethodConfigProto (com.google.api.codegen.MethodConfigProto)1 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)1 DiscoGapicInterfaceConfig (com.google.api.codegen.config.DiscoGapicInterfaceConfig)1 FieldConfig (com.google.api.codegen.config.FieldConfig)1 FieldModel (com.google.api.codegen.config.FieldModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 ResourceNameGenerationView (com.google.api.codegen.configgen.viewmodel.ResourceNameGenerationView)1 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)1 DiscoGapicNamer (com.google.api.codegen.discogapic.transformer.DiscoGapicNamer)1 DiscoGapicInterfaceContext (com.google.api.codegen.transformer.DiscoGapicInterfaceContext)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1