Search in sources :

Example 11 with SingleResourceNameConfig

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

the class PathTemplateTransformer method generateResourceNames.

List<ResourceNameView> generateResourceNames(InterfaceContext context, Iterable<ResourceNameConfig> configs) {
    List<ResourceNameView> resourceNames = new ArrayList<>();
    int index = 1;
    for (ResourceNameConfig config : configs) {
        switch(config.getResourceNameType()) {
            case SINGLE:
                resourceNames.add(generateResourceNameSingle(context, index, (SingleResourceNameConfig) config));
                break;
            case ONEOF:
                resourceNames.add(generateResourceNameOneof(context, index, (ResourceNameOneofConfig) config));
                break;
            case FIXED:
                resourceNames.add(generateResourceNameFixed(context, index, (FixedResourceNameConfig) config));
                break;
            case ANY:
                // ignore AnyResourceNameConfigs
                break;
            default:
                throw new IllegalStateException("Unexpected resource-name type.");
        }
        index += 1;
    }
    return resourceNames;
}
Also used : ResourceNameView(com.google.api.codegen.viewmodel.ResourceNameView) ResourceNameOneofConfig(com.google.api.codegen.config.ResourceNameOneofConfig) FixedResourceNameConfig(com.google.api.codegen.config.FixedResourceNameConfig) ArrayList(java.util.ArrayList) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) FixedResourceNameConfig(com.google.api.codegen.config.FixedResourceNameConfig)

Example 12 with SingleResourceNameConfig

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

the class PathTemplateTransformer method generatePathTemplateGetterFunctions.

public List<PathTemplateGetterFunctionView> generatePathTemplateGetterFunctions(GapicInterfaceContext context) {
    List<PathTemplateGetterFunctionView> functions = new ArrayList<>();
    SurfaceNamer namer = context.getNamer();
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    for (SingleResourceNameConfig resourceNameConfig : getSingleResourceNameConfigsUsedByInterface(context)) {
        PathTemplateGetterFunctionView.Builder function = PathTemplateGetterFunctionView.newBuilder().name(namer.getPathTemplateNameGetter(interfaceConfig, resourceNameConfig)).resourceName(namer.getPathTemplateResourcePhraseName(resourceNameConfig)).entityName(namer.getEntityName(resourceNameConfig)).pathTemplateName(namer.getPathTemplateName(interfaceConfig, resourceNameConfig)).pattern(resourceNameConfig.getNamePattern());
        functions.add(function.build());
    }
    return functions;
}
Also used : PathTemplateGetterFunctionView(com.google.api.codegen.viewmodel.PathTemplateGetterFunctionView) InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) ArrayList(java.util.ArrayList) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 13 with SingleResourceNameConfig

use of com.google.api.codegen.config.SingleResourceNameConfig 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.create(productConfig));
    // 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.flatPath()), 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.config.DiscoGapicInterfaceContext) HashSet(java.util.HashSet)

Example 14 with SingleResourceNameConfig

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

the class InitCodeTransformer method createFormattedInitValueView.

private InitValueView createFormattedInitValueView(MethodContext context, FieldConfig fieldConfig, InitCodeNode item, InitValueConfig initValueConfig) {
    FormattedInitValueView.Builder formattedInitValue = FormattedInitValueView.newBuilder();
    formattedInitValue.apiVariableName(context.getNamer().getApiWrapperVariableName(context.getInterfaceConfig()));
    formattedInitValue.apiWrapperName(context.getNamer().getApiWrapperClassName(context.getInterfaceConfig()));
    formattedInitValue.fullyQualifiedApiWrapperName(context.getNamer().getFullyQualifiedApiWrapperClassName(context.getInterfaceConfig()));
    // Use the single resource name config that matches binding values
    // specified in sample config
    SingleResourceNameConfig singleResourceNameConfig;
    if (fieldConfig.getResourceNameType() == ResourceNameType.ONEOF) {
        ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
        singleResourceNameConfig = getMatchingSingleResourceNameConfig(context, item, oneofConfig);
    } else {
        singleResourceNameConfig = initValueConfig.getSingleResourceNameConfig();
    }
    formattedInitValue.formatFunctionName(context.getNamer().getFormatFunctionName(context.getInterfaceConfig(), singleResourceNameConfig));
    PathTemplate template = singleResourceNameConfig.getNameTemplate();
    String[] encodeArgs = new String[template.vars().size()];
    Arrays.fill(encodeArgs, FORMAT_SPEC_PLACEHOLDER);
    // Format spec usually contains reserved character, escaped by path template.
    // So we first encode using FORMAT_SPEC_PLACEHOLDER, then do straight string replace.
    formattedInitValue.formatSpec(template.withoutVars().encode(encodeArgs).replace(FORMAT_SPEC_PLACEHOLDER, context.getNamer().formatSpec()));
    List<String> varList = Lists.newArrayList(singleResourceNameConfig.getNameTemplate().vars());
    formattedInitValue.formatArgs(getFormatFunctionArgs(context, varList, initValueConfig));
    return formattedInitValue.build();
}
Also used : ResourceNameOneofConfig(com.google.api.codegen.config.ResourceNameOneofConfig) FormattedInitValueView(com.google.api.codegen.viewmodel.FormattedInitValueView) PathTemplate(com.google.api.pathtemplate.PathTemplate) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 15 with SingleResourceNameConfig

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

the class InitCodeTransformer method createResourceNameOneofInitValueView.

private ResourceNameOneofInitValueView createResourceNameOneofInitValueView(MethodContext context, FieldConfig fieldConfig, InitCodeNode item, boolean convertToString) {
    ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
    SurfaceNamer namer = context.getNamer();
    ImportTypeTable typeTable = context.getTypeTable();
    if (context.getFeatureConfig().useStaticCreateMethodForOneofs()) {
        ResourceNamePatternConfig pattern = getMatchingResourceNamePattern(item, oneofConfig);
        String createMethod = convertToString ? pattern.getFormatMethodName() : pattern.getCreateMethodName();
        ImmutableList<String> formatArgs = getFormatFunctionArgs(context, ImmutableList.copyOf(pattern.getBindingVariables()), item.getInitValueConfig());
        return ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).createMethodName(createMethod).formatArgs(formatArgs).build();
    } else {
        SingleResourceNameConfig singleResourceNameConfig = getMatchingSingleResourceNameConfig(context, item, oneofConfig);
        FieldConfig singleResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
        ResourceNameInitValueView initView = createResourceNameInitValueView(context, singleResourceNameFieldConfig, item).convertToString(convertToString).build();
        return ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).specificResourceNameView(initView).build();
    }
}
Also used : ResourceNamePatternConfig(com.google.api.codegen.config.ResourceNamePatternConfig) RepeatedResourceNameInitValueView(com.google.api.codegen.viewmodel.RepeatedResourceNameInitValueView) ResourceNameInitValueView(com.google.api.codegen.viewmodel.ResourceNameInitValueView) ResourceNameOneofConfig(com.google.api.codegen.config.ResourceNameOneofConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Aggregations

SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)16 ArrayList (java.util.ArrayList)9 FieldConfig (com.google.api.codegen.config.FieldConfig)4 InterfaceConfig (com.google.api.codegen.config.InterfaceConfig)4 ResourceNameOneofConfig (com.google.api.codegen.config.ResourceNameOneofConfig)4 FixedResourceNameConfig (com.google.api.codegen.config.FixedResourceNameConfig)3 MethodConfig (com.google.api.codegen.config.MethodConfig)3 ResourceNameConfig (com.google.api.codegen.config.ResourceNameConfig)3 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 FieldModel (com.google.api.codegen.config.FieldModel)2 MethodContext (com.google.api.codegen.config.MethodContext)2 Method (com.google.api.codegen.discovery.Method)2 HashMap (java.util.HashMap)2 DiscoGapicInterfaceConfig (com.google.api.codegen.config.DiscoGapicInterfaceConfig)1 DiscoGapicInterfaceContext (com.google.api.codegen.config.DiscoGapicInterfaceContext)1 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)1 InterfaceContext (com.google.api.codegen.config.InterfaceContext)1 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 MethodModel (com.google.api.codegen.config.MethodModel)1 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)1