Search in sources :

Example 31 with FieldModel

use of com.google.api.codegen.config.FieldModel 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.getMethodConfig().getFieldNamePatterns();
        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 32 with FieldModel

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

the class StaticLangResourceObjectTransformer method generateRequestObjectParam.

public RequestObjectParamView generateRequestObjectParam(MethodContext context, FieldConfig fieldConfig) {
    SurfaceNamer namer = context.getNamer();
    FeatureConfig featureConfig = context.getFeatureConfig();
    ImportTypeTable typeTable = context.getTypeTable();
    FieldModel field = fieldConfig.getField();
    Iterable<FieldModel> requiredFields = context.getMethodConfig().getRequiredFields();
    boolean isRequired = false;
    for (FieldModel f : requiredFields) {
        if (f.getSimpleName().equals(field.getSimpleName())) {
            isRequired = true;
            break;
        }
    }
    String typeName = namer.getNotImplementedString("StaticLangApiMethodTransformer.generateRequestObjectParam - typeName");
    String elementTypeName = namer.getNotImplementedString("StaticLangApiMethodTransformer.generateRequestObjectParam - elementTypeName");
    if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
        if (namer.shouldImportRequestObjectParamType(field)) {
            typeName = namer.getAndSaveResourceTypeName(typeTable, fieldConfig);
        }
        if (namer.shouldImportRequestObjectParamElementType(field)) {
            // Use makeOptional to remove repeated property from type
            elementTypeName = namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig);
        }
    } else {
        if (namer.shouldImportRequestObjectParamType(field)) {
            typeName = typeTable.getAndSaveNicknameFor(field);
            if (!isRequired) {
                typeName = namer.makePrimitiveTypeNullable(typeName, field);
            }
        }
        if (namer.shouldImportRequestObjectParamElementType(field)) {
            elementTypeName = typeTable.getAndSaveNicknameForElementType(field);
        }
    }
    String setCallName = namer.getFieldSetFunctionName(featureConfig, fieldConfig);
    String addCallName = namer.getFieldAddFunctionName(field);
    String getCallName = namer.getFieldGetFunctionName(field);
    String transformParamFunctionName = null;
    String formatMethodName = null;
    if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
        if (fieldConfig.requiresParamTransformation() && !fieldConfig.requiresParamTransformationFromAny() && !featureConfig.useInheritanceForOneofs()) {
            transformParamFunctionName = namer.getResourceOneofCreateMethod(typeTable, fieldConfig);
        }
        if (context.getFeatureConfig().useResourceNameConverters(fieldConfig)) {
            if (field.isRepeated()) {
                // TODO support repeated one-ofs (in Java: Any* classes)
                transformParamFunctionName = namer.getResourceTypeFormatListMethodName(context.getTypeTable(), fieldConfig);
            } else {
                formatMethodName = namer.getResourceNameFormatMethodName();
            }
        }
    }
    RequestObjectParamView.Builder param = RequestObjectParamView.newBuilder();
    param.name(namer.getVariableName(field));
    param.keyName(namer.getFieldKey(field));
    param.nameAsMethodName(namer.getFieldGetFunctionName(featureConfig, fieldConfig));
    param.typeName(typeName);
    param.elementTypeName(elementTypeName);
    param.setCallName(setCallName);
    param.addCallName(addCallName);
    param.getCallName(getCallName);
    param.transformParamFunctionName(transformParamFunctionName);
    param.formatMethodName(formatMethodName);
    param.isMap(field.isMap());
    param.isArray(!field.isMap() && field.isRepeated());
    param.isPrimitive(namer.isPrimitive(field));
    param.isOptional(!isRequired);
    if (!isRequired) {
        param.optionalDefault(namer.getOptionalFieldDefaultValue(fieldConfig, context));
    }
    List<StaticLangMemberView> fieldViews = new ArrayList<>();
    for (FieldModel child : context.getMethodModel().getResourceNameInputFields()) {
        StaticLangMemberView.Builder staticMember = StaticLangMemberView.newBuilder();
        staticMember.fieldGetFunction(namer.getFieldGetFunctionName(child));
        staticMember.fieldSetFunction(namer.getFieldSetFunctionName(child));
        staticMember.name(child.getNameAsParameter());
        staticMember.typeName(child.getTypeFullName());
        fieldViews.add(staticMember.build());
    }
    Collections.sort(fieldViews);
    param.fieldCopyMethods(fieldViews);
    return param.build();
}
Also used : ArrayList(java.util.ArrayList) StaticLangMemberView(com.google.api.codegen.viewmodel.StaticLangMemberView) RequestObjectParamView(com.google.api.codegen.viewmodel.RequestObjectParamView) FieldModel(com.google.api.codegen.config.FieldModel)

Example 33 with FieldModel

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

the class SurfaceNamer method getResourceNameFieldSetFunctionName.

/**
 * The function name to set a field that is a resource name class.
 */
public String getResourceNameFieldSetFunctionName(FieldConfig fieldConfig) {
    FieldModel type = fieldConfig.getField();
    Name identifier = fieldConfig.getField().getNameAsParameterName();
    Name resourceName = getResourceTypeNameObject(fieldConfig.getResourceNameConfig());
    if (type.isMap()) {
        return getNotImplementedString("SurfaceNamer.getResourceNameFieldSetFunctionName:map-type");
    } else if (type.isRepeated()) {
        return publicMethodName(Name.from("add", "all").join(identifier).join("with").join(resourceName).join("list"));
    } else {
        return publicMethodName(Name.from("set").join(identifier).join("with").join(resourceName));
    }
}
Also used : FieldModel(com.google.api.codegen.config.FieldModel) Name(com.google.api.codegen.util.Name)

Example 34 with FieldModel

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

the class CSharpSurfaceNamer method getOptionalFieldDefaultValue.

@Override
public String getOptionalFieldDefaultValue(FieldConfig fieldConfig, MethodContext context) {
    // Need to provide defaults for primitives, enums, strings, and repeated (including maps)
    FieldModel type = fieldConfig.getField();
    if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
        if (type.isRepeated()) {
            TypeName elementTypeName = new TypeName(getResourceTypeNameObject(fieldConfig.getResourceNameConfig()).toUpperCamel());
            TypeNameConverter typeNameConverter = getTypeNameConverter();
            TypeName enumerableTypeName = typeNameConverter.getTypeName("System.Linq.Enumerable");
            TypeName emptyTypeName = new TypeName(enumerableTypeName.getFullName(), enumerableTypeName.getNickname(), "%s.Empty<%i>", elementTypeName);
            return TypedValue.create(emptyTypeName, "%s()").getValueAndSaveTypeNicknameIn((CSharpTypeTable) typeNameConverter);
        } else {
            return null;
        }
    } else {
        if (type.isPrimitive() || type.isEnum() || type.isRepeated()) {
            return context.getTypeTable().getImplZeroValueAndSaveNicknameFor(type);
        } else {
            return null;
        }
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName) TypeNameConverter(com.google.api.codegen.util.TypeNameConverter) FieldModel(com.google.api.codegen.config.FieldModel)

Example 35 with FieldModel

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

the class CSharpSurfaceNamer method getReturnDocLines.

@Override
public List<String> getReturnDocLines(TransformationContext context, MethodContext methodContext, Synchronicity synchronicity) {
    MethodConfig methodConfig = methodContext.getMethodConfig();
    if (methodConfig.isPageStreaming()) {
        FieldModel resourceType = methodConfig.getPageStreaming().getResourcesField();
        String resourceTypeName = context.getImportTypeTable().getAndSaveNicknameForElementType(resourceType);
        switch(synchronicity) {
            case Sync:
                return ImmutableList.of("A pageable sequence of <see cref=\"" + resourceTypeName + "\"/> resources.");
            case Async:
                return ImmutableList.of("A pageable asynchronous sequence of <see cref=\"" + resourceTypeName + "\"/> resources.");
        }
    } else if (methodConfig.isGrpcStreaming()) {
        switch(methodConfig.getGrpcStreamingType()) {
            case ServerStreaming:
                return ImmutableList.of("The server stream.");
            case BidiStreaming:
                return ImmutableList.of("The client-server stream.");
            default:
                throw new IllegalStateException("Invalid streaming: " + methodConfig.getGrpcStreamingType());
        }
    } else {
        boolean hasReturn = !methodConfig.getMethodModel().isOutputTypeEmpty();
        switch(synchronicity) {
            case Sync:
                return hasReturn ? ImmutableList.of("The RPC response.") : ImmutableList.of();
            case Async:
                return ImmutableList.of(hasReturn ? "A Task containing the RPC response." : "A Task that completes when the RPC has completed.");
        }
    }
    throw new IllegalStateException("Invalid Synchronicity: " + synchronicity);
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) FieldModel(com.google.api.codegen.config.FieldModel)

Aggregations

FieldModel (com.google.api.codegen.config.FieldModel)44 MethodConfig (com.google.api.codegen.config.MethodConfig)13 ArrayList (java.util.ArrayList)13 ImmutableList (com.google.common.collect.ImmutableList)11 FieldConfig (com.google.api.codegen.config.FieldConfig)9 MethodModel (com.google.api.codegen.config.MethodModel)8 SimpleParamDocView (com.google.api.codegen.viewmodel.SimpleParamDocView)7 ParamDocView (com.google.api.codegen.viewmodel.ParamDocView)6 BatchingConfig (com.google.api.codegen.config.BatchingConfig)4 PageStreamingConfig (com.google.api.codegen.config.PageStreamingConfig)4 DynamicLangDefaultableParamView (com.google.api.codegen.viewmodel.DynamicLangDefaultableParamView)4 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)3 Name (com.google.api.codegen.util.Name)3 DiscoveryMethodModel (com.google.api.codegen.config.DiscoveryMethodModel)2 GenericFieldSelector (com.google.api.codegen.config.GenericFieldSelector)2 Schema (com.google.api.codegen.discovery.Schema)2 SymbolTable (com.google.api.codegen.util.SymbolTable)2 TypeName (com.google.api.codegen.util.TypeName)2 RequestObjectParamView (com.google.api.codegen.viewmodel.RequestObjectParamView)2 StaticLangApiMessageView (com.google.api.codegen.viewmodel.StaticLangApiMessageView)2