Search in sources :

Example 1 with FieldSettingView

use of com.google.api.codegen.viewmodel.FieldSettingView in project toolkit by googleapis.

the class InitCodeTransformer method getFieldSettings.

private List<FieldSettingView> getFieldSettings(MethodContext context, Iterable<InitCodeNode> childItems) {
    SurfaceNamer namer = context.getNamer();
    List<FieldSettingView> allSettings = new ArrayList<>();
    for (InitCodeNode item : childItems) {
        FieldSettingView.Builder fieldSetting = FieldSettingView.newBuilder();
        FieldConfig fieldConfig = item.getFieldConfig();
        if (context.getFeatureConfig().useResourceNameProtoAccessor(fieldConfig)) {
            fieldSetting.fieldSetFunction(namer.getResourceNameFieldSetFunctionName(fieldConfig.getMessageFieldConfig()));
        } else {
            fieldSetting.fieldSetFunction(namer.getFieldSetFunctionName(item.getType(), Name.anyLower(item.getVarName())));
        }
        fieldSetting.fieldAddFunction(namer.getFieldAddFunctionName(item.getType(), Name.anyLower(item.getVarName())));
        fieldSetting.fieldGetFunction(namer.getFieldGetFunctionName(item.getType(), Name.anyLower(item.getVarName())));
        fieldSetting.identifier(getVariableName(context, item));
        fieldSetting.initCodeLine(generateSurfaceInitCodeLine(context, item, allSettings.isEmpty()));
        fieldSetting.fieldName(context.getNamer().publicFieldName(Name.anyLower(item.getVarName())));
        fieldSetting.isMap(item.getType().isMap());
        fieldSetting.isArray(!item.getType().isMap() && item.getType().isRepeated());
        fieldSetting.elementTypeName(context.getTypeTable().getFullNameFor(item.getType()));
        if (item.getOneofConfig() != null) {
            fieldSetting.oneofConfig(OneofConfigView.newBuilder().groupName(namer.publicFieldName(item.getOneofConfig().groupName())).variantType(namer.getOneofVariantTypeName(item.getOneofConfig())).build());
        }
        fieldSetting.required(isRequired(fieldConfig, context));
        String formatMethodName = "";
        String transformParamFunctionName = "";
        // If resource name converters should only be used in the sample, we need to convert the
        // resource name to a string before passing it or setting it on the next thing
        boolean needsConversion = context.getFeatureConfig().useResourceNameConvertersInSampleOnly(fieldConfig);
        // flattened method implementation when setting fields on the proto object).
        if (context.getFeatureConfig().useResourceNameConverters(fieldConfig) && !context.isFlattenedMethodContext()) {
            needsConversion = true;
        }
        if (needsConversion) {
            if (fieldConfig.getField().isRepeated()) {
                // TODO (https://github.com/googleapis/toolkit/issues/1806) support repeated one-ofs
                transformParamFunctionName = namer.getResourceTypeFormatListMethodName(context.getTypeTable(), fieldConfig);
            } else {
                formatMethodName = namer.getResourceNameFormatMethodName();
            }
        }
        fieldSetting.transformParamFunctionName(transformParamFunctionName);
        fieldSetting.formatMethodName(formatMethodName);
        allSettings.add(fieldSetting.build());
    }
    return allSettings;
}
Also used : FieldSettingView(com.google.api.codegen.viewmodel.FieldSettingView) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList)

Aggregations

FieldConfig (com.google.api.codegen.config.FieldConfig)1 InitCodeNode (com.google.api.codegen.metacode.InitCodeNode)1 FieldSettingView (com.google.api.codegen.viewmodel.FieldSettingView)1 ArrayList (java.util.ArrayList)1