Search in sources :

Example 1 with InitValueConfig

use of com.google.api.codegen.metacode.InitValueConfig 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(context, 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 {
            initValue = createInitValueView(context, fieldConfig, namer, typeTable, item, false);
        }
    } else if (initValueConfig.hasFormattingConfig() && !item.getType().isRepeated()) {
        if (context.getFeatureConfig().enableStringFormatFunctions() || fieldConfig.getResourceNameConfig() == null) {
            initValue = createFormattedInitValueView(context, fieldConfig, item, initValueConfig);
        } else {
            initValue = createInitValueView(context, fieldConfig, namer, typeTable, item, true);
        }
    } 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 (generateStandardComments) {
        surfaceLine.doc(context.getNamer().getDocLines(comment));
    } else {
        surfaceLine.doc(ImmutableList.of());
    }
    surfaceLine.descriptions(context.getNamer().getWrappedDocLines(item.getDescription(), false));
}
Also used : SimpleInitValueView(com.google.api.codegen.viewmodel.SimpleInitValueView) 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) FormattedInitValueView(com.google.api.codegen.viewmodel.FormattedInitValueView) FieldConfig(com.google.api.codegen.config.FieldConfig) InitValueConfig(com.google.api.codegen.metacode.InitValueConfig) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig)

Example 2 with InitValueConfig

use of com.google.api.codegen.metacode.InitValueConfig in project toolkit by googleapis.

the class InitCodeTransformer method createCollectionMap.

/**
 * A utility method which creates the InitValueConfig map that contains the collection config
 * data.
 */
public static ImmutableMap<String, InitValueConfig> createCollectionMap(MethodContext context) {
    ImmutableMap.Builder<String, InitValueConfig> mapBuilder = ImmutableMap.builder();
    Map<String, String> fieldNamePatterns = context.getFieldResourceEntityMap();
    for (Map.Entry<String, String> fieldNamePattern : fieldNamePatterns.entrySet()) {
        SingleResourceNameConfig resourceNameConfig = context.getSingleResourceNameConfig(fieldNamePattern.getValue());
        String apiWrapperClassName = context.getNamer().getApiWrapperClassName(context.getInterfaceConfig());
        InitValueConfig initValueConfig = InitValueConfig.create(apiWrapperClassName, resourceNameConfig);
        mapBuilder.put(fieldNamePattern.getKey(), initValueConfig);
    }
    return mapBuilder.build();
}
Also used : InitValueConfig(com.google.api.codegen.metacode.InitValueConfig) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ImmutableMap(com.google.common.collect.ImmutableMap) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig)

Aggregations

SingleResourceNameConfig (com.google.api.codegen.config.SingleResourceNameConfig)2 InitValueConfig (com.google.api.codegen.metacode.InitValueConfig)2 FieldConfig (com.google.api.codegen.config.FieldConfig)1 ResourceNameConfig (com.google.api.codegen.config.ResourceNameConfig)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 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1