Search in sources :

Example 6 with SingleResourceNameConfig

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

the class PathTemplateTransformer method addSingleResourceNameConfigsUsedByInterface.

private static void addSingleResourceNameConfigsUsedByInterface(MethodContext methodContext, String fieldNamePattern, Set<String> foundSet, List<ResourceNameConfig> resourceNameConfigs) {
    SingleResourceNameConfig resourceNameConfig = methodContext.getSingleResourceNameConfig(fieldNamePattern);
    if (resourceNameConfig != null && !foundSet.contains(resourceNameConfig.getEntityId())) {
        resourceNameConfigs.add(resourceNameConfig);
        foundSet.add(resourceNameConfig.getEntityId());
    }
}
Also used : SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 7 with SingleResourceNameConfig

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

the class PathTemplateTransformer method generateFormatResourceFunctions.

public List<FormatResourceFunctionView> generateFormatResourceFunctions(InterfaceContext context) {
    List<FormatResourceFunctionView> functions = new ArrayList<>();
    if (!context.getFeatureConfig().enableStringFormatFunctions()) {
        return functions;
    }
    SurfaceNamer namer = context.getNamer();
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    for (SingleResourceNameConfig resourceNameConfig : getSingleResourceNameConfigsUsedByInterface(context)) {
        FormatResourceFunctionView.Builder function = FormatResourceFunctionView.newBuilder().resourceName(namer.getResourceTypeName(resourceNameConfig)).entityName(resourceNameConfig.getEntityName().toLowerUnderscore()).name(namer.getFormatFunctionName(interfaceConfig, resourceNameConfig)).pathTemplateName(namer.getPathTemplateName(interfaceConfig, resourceNameConfig)).pathTemplateGetterName(namer.getPathTemplateNameGetter(interfaceConfig, resourceNameConfig)).pattern(resourceNameConfig.getNamePattern()).isResourceNameDeprecated(resourceNameConfig.getDeprecated());
        List<ResourceIdParamView> resourceIdParams = new ArrayList<>();
        for (String variable : resourceNameConfig.getNameTemplate().vars()) {
            ResourceIdParamView param = ResourceIdParamView.newBuilder().name(namer.getParamName(variable)).docName(namer.getParamDocName(variable)).templateKey(variable).build();
            resourceIdParams.add(param);
        }
        function.resourceIdParams(resourceIdParams);
        functions.add(function.build());
    }
    return functions;
}
Also used : InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) ResourceIdParamView(com.google.api.codegen.viewmodel.ResourceIdParamView) FormatResourceFunctionView(com.google.api.codegen.viewmodel.FormatResourceFunctionView) ArrayList(java.util.ArrayList) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 8 with SingleResourceNameConfig

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

the class ApiCallableTransformer method generateHttpFields.

private HttpMethodView generateHttpFields(MethodContext context) {
    if (context.getProductConfig().getTransportProtocol().equals(TransportProtocol.HTTP)) {
        if (context.getMethodModel() instanceof DiscoveryMethodModel) {
            // This section is only for DiscoGapic and will be deleted once the generator stops
            // ingesting Discovery files.
            Method method = ((DiscoveryMethodModel) context.getMethodModel()).getDiscoMethod();
            HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
            httpMethodView.fullMethodName(method.id());
            httpMethodView.httpMethod(method.httpMethod());
            List<String> pathParams = new ArrayList<>(method.pathParams().keySet());
            List<String> queryParams = new ArrayList<>(method.queryParams().keySet());
            Collections.sort(pathParams);
            Collections.sort(queryParams);
            httpMethodView.pathParams(pathParams);
            httpMethodView.queryParams(queryParams);
            httpMethodView.pathTemplate(method.path());
            // TODO(andrealin): handle multiple resource names.
            DiscoGapicInterfaceConfig interfaceConfig = (DiscoGapicInterfaceConfig) context.getSurfaceInterfaceContext().getInterfaceConfig();
            SingleResourceNameConfig nameConfig = interfaceConfig.methodToResourceNameMap().get(context.getMethodConfig());
            httpMethodView.resourceNameTypeName(context.getNamer().publicClassName(DiscoGapicParser.getResourceNameName(nameConfig)));
            // Find the field with the resource name config.
            for (FieldConfig fieldConfig : context.getMethodConfig().getRequiredFieldConfigs()) {
                if (fieldConfig.getResourceNameConfig() != null && fieldConfig.getResourceNameConfig().equals(nameConfig)) {
                    httpMethodView.resourceNameFieldName(context.getNamer().privateFieldName(Name.anyCamel(fieldConfig.getField().getNameAsParameter())));
                }
            }
            return httpMethodView.build();
        } else if (context.getMethodModel() instanceof ProtoMethodModel) {
            com.google.api.tools.framework.model.Method method = ((ProtoMethodModel) context.getMethodModel()).getProtoMethod();
            HttpAttribute httpAttr = method.getAttribute(HttpAttribute.KEY);
            HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
            httpMethodView.httpMethod(httpAttr.getMethodKind().toString());
            httpMethodView.fullMethodName(httpAttr.getRestMethod().getFullName());
            SurfaceNamer namer = context.getNamer();
            httpMethodView.pathTemplate(httpAttr.getPath().stream().map(pathSegment -> normalizePathSegment(pathSegment.toString())).collect(Collectors.joining("/", "/", "")));
            httpMethodView.pathParamSelectors(populateMethodSelectors(namer, httpAttr.getPathSelectors()));
            httpMethodView.queryParamSelectors(populateMethodSelectors(namer, httpAttr.getParamSelectors()));
            httpMethodView.bodySelectors(populateMethodSelectors(namer, httpAttr.getBodySelectors()));
            return httpMethodView.build();
        }
    }
    return null;
}
Also used : ApiCallableImplType(com.google.api.codegen.viewmodel.ApiCallableImplType) FieldModel(com.google.api.codegen.config.FieldModel) RetryParamsDefinitionView(com.google.api.codegen.viewmodel.RetryParamsDefinitionView) Field(com.google.api.tools.framework.model.Field) GapicMethodContext(com.google.api.codegen.config.GapicMethodContext) ServiceMethodType(com.google.api.codegen.viewmodel.ServiceMethodType) RetryCodesDefinitionView(com.google.api.codegen.viewmodel.RetryCodesDefinitionView) DiscoGapicInterfaceConfig(com.google.api.codegen.config.DiscoGapicInterfaceConfig) HashMap(java.util.HashMap) ProtoField(com.google.api.codegen.config.ProtoField) ApiCallableView(com.google.api.codegen.viewmodel.ApiCallableView) LongRunningOperationDetailView(com.google.api.codegen.viewmodel.LongRunningOperationDetailView) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) ProtoMethodModel(com.google.api.codegen.config.ProtoMethodModel) DiscoGapicParser(com.google.api.codegen.discogapic.transformer.DiscoGapicParser) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) MethodModel(com.google.api.codegen.config.MethodModel) TransportProtocol(com.google.api.codegen.config.TransportProtocol) Map(java.util.Map) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ApiCallSettingsView(com.google.api.codegen.viewmodel.ApiCallSettingsView) MethodConfig(com.google.api.codegen.config.MethodConfig) HttpAttribute(com.google.api.tools.framework.aspects.http.model.HttpAttribute) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) HttpMethodView(com.google.api.codegen.viewmodel.HttpMethodView) HttpMethodSelectorView(com.google.api.codegen.viewmodel.HttpMethodSelectorView) Collectors(java.util.stream.Collectors) FieldSelector(com.google.api.tools.framework.model.FieldSelector) Name(com.google.api.codegen.util.Name) List(java.util.List) MethodDescriptorView(com.google.api.codegen.viewmodel.MethodDescriptorView) PageStreamingConfig(com.google.api.codegen.config.PageStreamingConfig) Method(com.google.api.codegen.discovery.Method) Collections(java.util.Collections) FieldConfig(com.google.api.codegen.config.FieldConfig) VisibilityConfig(com.google.api.codegen.config.VisibilityConfig) HttpAttribute(com.google.api.tools.framework.aspects.http.model.HttpAttribute) ProtoMethodModel(com.google.api.codegen.config.ProtoMethodModel) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) HttpMethodView(com.google.api.codegen.viewmodel.HttpMethodView) DiscoGapicInterfaceConfig(com.google.api.codegen.config.DiscoGapicInterfaceConfig) Method(com.google.api.codegen.discovery.Method) DiscoveryMethodModel(com.google.api.codegen.config.DiscoveryMethodModel) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 9 with SingleResourceNameConfig

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

the class StaticLangApiMethodTransformer method generatePathTemplateChecks.

private List<PathTemplateCheckView> generatePathTemplateChecks(MethodContext context, Iterable<FieldConfig> fieldConfigs) {
    List<PathTemplateCheckView> pathTemplateChecks = new ArrayList<>();
    if (!context.getFeatureConfig().enableStringFormatFunctions()) {
        return pathTemplateChecks;
    }
    for (FieldConfig fieldConfig : fieldConfigs) {
        if (!fieldConfig.useValidation()) {
            // Don't generate a path template check if fieldConfig is not configured to use validation.
            continue;
        }
        FieldModel field = fieldConfig.getField();
        ImmutableMap<String, String> fieldNamePatterns = context.getFieldResourceEntityMap();
        String entityName = fieldNamePatterns.get(field.getSimpleName());
        if (entityName != null) {
            SingleResourceNameConfig resourceNameConfig = context.getSingleResourceNameConfig(entityName);
            if (resourceNameConfig == null) {
                String methodName = context.getMethodModel().getSimpleName();
                throw new IllegalStateException("No collection config with id '" + entityName + "' required by configuration for method '" + methodName + "'");
            }
            PathTemplateCheckView.Builder check = PathTemplateCheckView.newBuilder();
            check.pathTemplateName(context.getNamer().getPathTemplateName(context.getInterfaceConfig(), resourceNameConfig));
            check.paramName(context.getNamer().getVariableName(field));
            check.allowEmptyString(shouldAllowEmpty(context, field));
            check.validationMessageContext(context.getNamer().getApiMethodName(context.getMethodModel(), context.getMethodConfig().getVisibility()));
            pathTemplateChecks.add(check.build());
        }
    }
    return pathTemplateChecks;
}
Also used : PathTemplateCheckView(com.google.api.codegen.viewmodel.PathTemplateCheckView) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList) FieldModel(com.google.api.codegen.config.FieldModel) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Example 10 with SingleResourceNameConfig

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

the class PathTemplateTransformer method generatePathTemplates.

public List<PathTemplateView> generatePathTemplates(InterfaceContext context) {
    List<PathTemplateView> pathTemplates = new ArrayList<>();
    if (!context.getFeatureConfig().enableStringFormatFunctions()) {
        return pathTemplates;
    }
    for (SingleResourceNameConfig resourceNameConfig : getSingleResourceNameConfigsUsedByInterface(context)) {
        PathTemplateView.Builder pathTemplate = PathTemplateView.newBuilder();
        pathTemplate.name(context.getNamer().getPathTemplateName(context.getInterfaceConfig(), resourceNameConfig));
        pathTemplate.pattern(resourceNameConfig.getNamePattern());
        pathTemplates.add(pathTemplate.build());
    }
    return pathTemplates;
}
Also used : PathTemplateView(com.google.api.codegen.viewmodel.PathTemplateView) ArrayList(java.util.ArrayList) 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