Search in sources :

Example 1 with ResourceNameOneofConfig

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

the class PathTemplateTransformer method getSingleResourceNameConfigsUsedByInterface.

private static List<SingleResourceNameConfig> getSingleResourceNameConfigsUsedByInterface(InterfaceContext context) {
    TreeMap<String, SingleResourceNameConfig> resources = new TreeMap<>();
    for (ResourceNameConfig resourceNameConfig : getResourceNameConfigsUsedByInterface(context)) {
        if (resourceNameConfig.getResourceNameType() == ResourceNameType.SINGLE) {
            resources.put(resourceNameConfig.getEntityId(), (SingleResourceNameConfig) resourceNameConfig);
        }
        if (resourceNameConfig.getResourceNameType() == ResourceNameType.ONEOF) {
            // Add SingleResourceNameConfigs derived from gapic config v1 and v2
            ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) resourceNameConfig;
            for (SingleResourceNameConfig resource : oneofConfig.getSingleResourceNameConfigs()) {
                resources.put(resource.getEntityId(), resource);
            }
            // those derived from patterns will win
            if (context.getFeatureConfig().enableStringFormatFunctionsForOneofs()) {
                for (SingleResourceNameConfig resource : oneofConfig.getPatternsAsSingleResourceNameConfigs()) {
                    resources.put(resource.getEntityId(), resource);
                }
            }
            // Add the SingleResourceNameConfig derived from the first pattern last as if it is a
            // single-pattern resource name for backward-compatibility.
            // We need this for all dynamic languages.
            Optional<SingleResourceNameConfig> firstPattern = oneofConfig.getFirstPatternAsSingleResourceNameConfig();
            if (firstPattern.isPresent()) {
                resources.put(firstPattern.get().getEntityId(), firstPattern.get());
            }
        }
    }
    return ImmutableList.copyOf(resources.values());
}
Also used : ResourceNameOneofConfig(com.google.api.codegen.config.ResourceNameOneofConfig) TreeMap(java.util.TreeMap) 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 2 with ResourceNameOneofConfig

use of com.google.api.codegen.config.ResourceNameOneofConfig 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 3 with ResourceNameOneofConfig

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

the class PathTemplateTransformer method addResourceNameOneofConfigsUsedByInterface.

private static void addResourceNameOneofConfigsUsedByInterface(InterfaceContext context, String fieldNamePattern, Set<String> foundSet, List<ResourceNameConfig> resourceNameConfigs) {
    ResourceNameConfig resourceNameConfig = context.getProductConfig().getResourceNameConfigs().get(fieldNamePattern);
    if (resourceNameConfig != null && resourceNameConfig.getResourceNameType() == ResourceNameType.ONEOF) {
        ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) resourceNameConfig;
        if (!foundSet.contains(resourceNameConfig.getEntityId())) {
            resourceNameConfigs.add(resourceNameConfig);
            foundSet.add(resourceNameConfig.getEntityId());
        }
    }
}
Also used : ResourceNameOneofConfig(com.google.api.codegen.config.ResourceNameOneofConfig) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) FixedResourceNameConfig(com.google.api.codegen.config.FixedResourceNameConfig)

Example 4 with ResourceNameOneofConfig

use of com.google.api.codegen.config.ResourceNameOneofConfig 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 5 with ResourceNameOneofConfig

use of com.google.api.codegen.config.ResourceNameOneofConfig 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

ResourceNameOneofConfig (com.google.api.codegen.config.ResourceNameOneofConfig)5 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)5 FixedResourceNameConfig (com.google.api.codegen.config.FixedResourceNameConfig)3 ResourceNameConfig (com.google.api.codegen.config.ResourceNameConfig)3 FieldConfig (com.google.api.codegen.config.FieldConfig)1 ResourceNamePatternConfig (com.google.api.codegen.config.ResourceNamePatternConfig)1 FormattedInitValueView (com.google.api.codegen.viewmodel.FormattedInitValueView)1 RepeatedResourceNameInitValueView (com.google.api.codegen.viewmodel.RepeatedResourceNameInitValueView)1 ResourceNameInitValueView (com.google.api.codegen.viewmodel.ResourceNameInitValueView)1 ResourceNameView (com.google.api.codegen.viewmodel.ResourceNameView)1 PathTemplate (com.google.api.pathtemplate.PathTemplate)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1