Search in sources :

Example 1 with SingleResourceNameConfig

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

the class InitCodeTransformer method createInitValueView.

private InitValueView createInitValueView(MethodContext context, FieldConfig fieldConfig, SurfaceNamer namer, ImportTypeTable typeTable, InitCodeNode item, boolean convertToString) {
    SingleResourceNameConfig singleResourceNameConfig;
    switch(fieldConfig.getResourceNameType()) {
        case ANY:
            singleResourceNameConfig = Iterables.get(context.getProductConfig().getSingleResourceNameConfigs(), 0);
            InterfaceModel interfaceModel = context.getInterfaceModel();
            if (interfaceModel instanceof ProtoInterfaceModel) {
                ProtoFile protoFile = ((ProtoInterfaceModel) interfaceModel).getInterface().getFile();
                singleResourceNameConfig = singleResourceNameConfig.toBuilder().setAssignedProtoFile(protoFile).build();
            }
            FieldConfig anyResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
            return createResourceNameInitValueView(context, anyResourceNameFieldConfig, item).convertToString(convertToString).build();
        case FIXED:
            throw new UnsupportedOperationException("entity name invalid");
        case ONEOF:
            return createResourceNameOneofInitValueView(context, fieldConfig, item, convertToString);
        case SINGLE:
            return createResourceNameInitValueView(context, fieldConfig, item).convertToString(convertToString).build();
        case NONE:
        // Fall-through
        default:
            throw new UnsupportedOperationException("unexpected entity name type '" + fieldConfig.getResourceNameType() + "'");
    }
}
Also used : InterfaceModel(com.google.api.codegen.config.InterfaceModel) ProtoInterfaceModel(com.google.api.codegen.config.ProtoInterfaceModel) ProtoInterfaceModel(com.google.api.codegen.config.ProtoInterfaceModel) FieldConfig(com.google.api.codegen.config.FieldConfig) ProtoFile(com.google.api.tools.framework.model.ProtoFile) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 2 with SingleResourceNameConfig

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

the class InitCodeTransformer method createResourceNameInitValueView.

private ResourceNameInitValueView.Builder createResourceNameInitValueView(MethodContext context, FieldConfig fieldConfig, InitCodeNode item) {
    String resourceName = context.getNamer().getAndSaveElementResourceTypeName(context.getTypeTable(), fieldConfig);
    SingleResourceNameConfig singleResourceNameConfig = (SingleResourceNameConfig) fieldConfig.getResourceNameConfig();
    List<String> varList = Lists.newArrayList(singleResourceNameConfig.getNameTemplate().vars());
    return ResourceNameInitValueView.newBuilder().resourceTypeName(resourceName).formatArgs(getFormatFunctionArgs(context, varList, item.getInitValueConfig()));
}
Also used : SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 3 with SingleResourceNameConfig

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

the class PathTemplateTransformer method getResourceNameConfigsUsedByInterface.

private static List<ResourceNameConfig> getResourceNameConfigsUsedByInterface(InterfaceContext context) {
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    Set<String> foundSet = new HashSet<>();
    List<ResourceNameConfig> resourceNameConfigs = new ArrayList<>();
    for (SingleResourceNameConfig config : interfaceConfig.getSingleResourceNameConfigs()) {
        resourceNameConfigs.add(config);
        foundSet.add(config.getEntityId());
    }
    for (MethodConfig methodConfig : interfaceConfig.getMethodConfigs()) {
        MethodContext methodContext = context.asRequestMethodContext(methodConfig.getMethodModel());
        for (String fieldNamePattern : methodConfig.getFieldNamePatterns().values()) {
            addSingleResourceNameConfigsUsedByInterface(methodContext, fieldNamePattern, foundSet, resourceNameConfigs);
            addResourceNameOneofConfigsUsedByInterface(context, fieldNamePattern, foundSet, resourceNameConfigs);
        }
    }
    return resourceNameConfigs;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) HashSet(java.util.HashSet) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) FixedResourceNameConfig(com.google.api.codegen.config.FixedResourceNameConfig)

Example 4 with SingleResourceNameConfig

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

the class PathTemplateTransformer method generateParseResourceFunctions.

public List<ParseResourceFunctionView> generateParseResourceFunctions(InterfaceContext context) {
    List<ParseResourceFunctionView> functions = new ArrayList<>();
    if (!context.getFeatureConfig().enableStringFormatFunctions()) {
        return functions;
    }
    SurfaceNamer namer = context.getNamer();
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    for (SingleResourceNameConfig resourceNameConfig : getSingleResourceNameConfigsUsedByInterface(context)) {
        for (String var : resourceNameConfig.getNameTemplate().vars()) {
            ParseResourceFunctionView.Builder function = ParseResourceFunctionView.newBuilder().entityName(resourceNameConfig.getEntityName().toLowerUnderscore()).name(namer.getParseFunctionName(var, resourceNameConfig)).pathTemplateName(namer.getPathTemplateName(interfaceConfig, resourceNameConfig)).pathTemplateGetterName(namer.getPathTemplateNameGetter(interfaceConfig, resourceNameConfig)).entityNameTypeName(namer.getResourceTypeName(resourceNameConfig)).entityNameParamName(namer.getEntityNameParamName(resourceNameConfig)).isResourceNameDeprecated(resourceNameConfig.getDeprecated()).outputResourceId(var);
            functions.add(function.build());
        }
    }
    return functions;
}
Also used : InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) ParseResourceFunctionView(com.google.api.codegen.viewmodel.ParseResourceFunctionView) ArrayList(java.util.ArrayList) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 5 with SingleResourceNameConfig

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

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