Search in sources :

Example 1 with InitValueView

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

the class InitCodeTransformer method setInitValueAndComments.

private void setInitValueAndComments(SimpleInitCodeLineView.Builder surfaceLine, MethodContext context, InitCodeNode item, boolean isFirstItem) {
    SurfaceNamer namer = context.getNamer();
    ImportTypeTable typeTable = context.getTypeTable();
    InitValueConfig initValueConfig = item.getInitValueConfig();
    FieldConfig fieldConfig = item.getFieldConfig();
    // Output variables
    InitValueView initValue;
    String comment = "";
    if (context.getFeatureConfig().useResourceNameFormatOptionInSample(fieldConfig)) {
        if (!context.isFlattenedMethodContext()) {
            ResourceNameConfig messageResNameConfig = fieldConfig.getMessageResourceNameConfig();
            if (messageResNameConfig == null || messageResNameConfig.getResourceNameType() != ResourceNameType.ANY) {
                // In a non-flattened context, we always use the resource name type set on the message
                // instead of set on the flattened method, unless the resource name type on message
                // is ANY.
                fieldConfig = fieldConfig.getMessageFieldConfig();
            }
        }
        if (item.getType().isRepeated()) {
            initValue = RepeatedResourceNameInitValueView.newBuilder().resourceTypeName(namer.getAndSaveElementResourceTypeName(context.getTypeTable(), fieldConfig)).build();
        } else {
            SingleResourceNameConfig singleResourceNameConfig;
            switch(fieldConfig.getResourceNameType()) {
                case ANY:
                    // TODO(michaelbausor): handle case where there are no other resource names at all...
                    singleResourceNameConfig = Iterables.get(context.getProductConfig().getSingleResourceNameConfigs(), 0);
                    FieldConfig anyResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
                    initValue = createResourceNameInitValueView(context, anyResourceNameFieldConfig, item).build();
                    break;
                case FIXED:
                    throw new UnsupportedOperationException("entity name invalid");
                case ONEOF:
                    ResourceNameOneofConfig oneofConfig = (ResourceNameOneofConfig) fieldConfig.getResourceNameConfig();
                    singleResourceNameConfig = Iterables.get(oneofConfig.getSingleResourceNameConfigs(), 0);
                    FieldConfig singleResourceNameFieldConfig = fieldConfig.withResourceNameConfig(singleResourceNameConfig);
                    ResourceNameInitValueView initView = createResourceNameInitValueView(context, singleResourceNameFieldConfig, item).build();
                    initValue = ResourceNameOneofInitValueView.newBuilder().resourceOneofTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)).specificResourceNameView(initView).build();
                    break;
                case SINGLE:
                    initValue = createResourceNameInitValueView(context, fieldConfig, item).build();
                    break;
                case NONE:
                // Fall-through
                default:
                    throw new UnsupportedOperationException("unexpected entity name type '" + fieldConfig.getResourceNameType() + "'");
            }
        }
    } else if (initValueConfig.hasFormattingConfig() && !item.getType().isRepeated()) {
        if (context.getFeatureConfig().enableStringFormatFunctions() || fieldConfig.getResourceNameConfig() == null) {
            FormattedInitValueView.Builder formattedInitValue = FormattedInitValueView.newBuilder();
            formattedInitValue.apiWrapperName(context.getNamer().getApiWrapperClassName(context.getInterfaceConfig()));
            formattedInitValue.fullyQualifiedApiWrapperName(context.getNamer().getFullyQualifiedApiWrapperClassName(context.getInterfaceConfig()));
            formattedInitValue.formatFunctionName(context.getNamer().getFormatFunctionName(context.getInterfaceConfig(), initValueConfig.getSingleResourceNameConfig()));
            PathTemplate template = initValueConfig.getSingleResourceNameConfig().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(initValueConfig.getSingleResourceNameConfig().getNameTemplate().vars());
            formattedInitValue.formatArgs(getFormatFunctionArgs(context, varList, initValueConfig));
            initValue = formattedInitValue.build();
        } else {
            initValue = createResourceNameInitValueView(context, fieldConfig, item).convertToString(true).build();
        }
    } else {
        SimpleInitValueView.Builder simpleInitValue = SimpleInitValueView.newBuilder();
        if (initValueConfig.hasSimpleInitialValue()) {
            String value = initValueConfig.getInitialValue().getValue();
            switch(initValueConfig.getInitialValue().getType()) {
                case Literal:
                    if (item.getType().isEnum()) {
                        value = context.getTypeTable().getEnumValue(item.getType(), value);
                    } else {
                        value = context.getTypeTable().renderPrimitiveValue(item.getType(), value);
                    }
                    break;
                case Random:
                    value = context.getNamer().injectRandomStringGeneratorCode(value);
                    break;
                case Variable:
                    value = context.getNamer().localVarReference(Name.anyLower(value));
                    break;
                default:
                    throw new IllegalArgumentException("Unhandled init value type");
            }
            simpleInitValue.initialValue(value);
        } else {
            simpleInitValue.initialValue(context.getTypeTable().getSnippetZeroValueAndSaveNicknameFor(item.getType()));
            simpleInitValue.isRepeated(item.getType().isRepeated());
            if (isRequired(item.getFieldConfig(), context)) {
                String name = getVariableName(context, item);
                comment = String.format(UNINITIALIZED_REQUIRED_FIELD_COMMENT, name);
            }
        }
        initValue = simpleInitValue.build();
    }
    surfaceLine.initValue(initValue);
    surfaceLine.needsLeadingNewline(!isFirstItem);
    if (generateUserFacingComments) {
        surfaceLine.doc(context.getNamer().getDocLines(comment));
    } else {
        surfaceLine.doc(ImmutableList.of());
    }
}
Also used : 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) InitValueConfig(com.google.api.codegen.metacode.InitValueConfig) PathTemplate(com.google.api.pathtemplate.PathTemplate) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) SimpleInitValueView(com.google.api.codegen.viewmodel.SimpleInitValueView) FormattedInitValueView(com.google.api.codegen.viewmodel.FormattedInitValueView) InitValueView(com.google.api.codegen.viewmodel.InitValueView) RepeatedResourceNameInitValueView(com.google.api.codegen.viewmodel.RepeatedResourceNameInitValueView) ResourceNameOneofInitValueView(com.google.api.codegen.viewmodel.ResourceNameOneofInitValueView) ResourceNameInitValueView(com.google.api.codegen.viewmodel.ResourceNameInitValueView) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

FieldConfig (com.google.api.codegen.config.FieldConfig)1 ResourceNameConfig (com.google.api.codegen.config.ResourceNameConfig)1 ResourceNameOneofConfig (com.google.api.codegen.config.ResourceNameOneofConfig)1 SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)1 InitValueConfig (com.google.api.codegen.metacode.InitValueConfig)1 FormattedInitValueView (com.google.api.codegen.viewmodel.FormattedInitValueView)1 InitValueView (com.google.api.codegen.viewmodel.InitValueView)1 RepeatedResourceNameInitValueView (com.google.api.codegen.viewmodel.RepeatedResourceNameInitValueView)1 ResourceNameInitValueView (com.google.api.codegen.viewmodel.ResourceNameInitValueView)1 ResourceNameOneofInitValueView (com.google.api.codegen.viewmodel.ResourceNameOneofInitValueView)1 SimpleInitValueView (com.google.api.codegen.viewmodel.SimpleInitValueView)1 PathTemplate (com.google.api.pathtemplate.PathTemplate)1 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1