Search in sources :

Example 1 with StaticLangApiResourceNameView

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

the class JavaDiscoGapicResourceNameToViewTransformer method generateResourceNameClass.

private StaticLangApiResourceNameView generateResourceNameClass(SchemaTransformationContext context, Method method, SingleResourceNameConfig nameConfig) {
    StaticLangApiResourceNameView.Builder resourceNameView = StaticLangApiResourceNameView.newBuilder();
    SymbolTable symbolTable = SymbolTable.fromSeed(reservedKeywords);
    String resourceName = nameFormatter.localVarName(DiscoGapicParser.getResourceNameName(nameConfig));
    resourceNameView.name(resourceName);
    resourceNameView.typeName(nameFormatter.publicClassName(Name.anyCamel(resourceName)));
    resourceNameView.pathTemplate(nameConfig.getNamePattern());
    List<StaticLangMemberView> properties = new LinkedList<>();
    for (Map.Entry<String, Schema> entry : method.parameters().entrySet()) {
        if (entry.getValue().isPathParam()) {
            Schema param = entry.getValue();
            properties.add(schemaToParamView(context, param, symbolTable));
        }
    }
    Collections.sort(properties);
    resourceNameView.pathParams(properties);
    return resourceNameView.build();
}
Also used : Schema(com.google.api.codegen.discovery.Schema) SymbolTable(com.google.api.codegen.util.SymbolTable) StaticLangMemberView(com.google.api.codegen.viewmodel.StaticLangMemberView) StaticLangApiResourceNameView(com.google.api.codegen.viewmodel.StaticLangApiResourceNameView) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 2 with StaticLangApiResourceNameView

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

Aggregations

StaticLangApiResourceNameView (com.google.api.codegen.viewmodel.StaticLangApiResourceNameView)2 HashMap (java.util.HashMap)2 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)1 SchemaTransformationContext (com.google.api.codegen.discogapic.SchemaTransformationContext)1 Method (com.google.api.codegen.discovery.Method)1 Schema (com.google.api.codegen.discovery.Schema)1 DiscoGapicInterfaceContext (com.google.api.codegen.transformer.DiscoGapicInterfaceContext)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)1 SymbolTable (com.google.api.codegen.util.SymbolTable)1 StaticLangMemberView (com.google.api.codegen.viewmodel.StaticLangMemberView)1 ViewModel (com.google.api.codegen.viewmodel.ViewModel)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1