Search in sources :

Example 1 with SoyLegacyObjectMap

use of com.google.template.soy.data.SoyLegacyObjectMap in project closure-templates by google.

the class AugmentMapFunction method computeForJava.

// IntelliJ
@SuppressWarnings("ConstantConditions")
@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    Preconditions.checkArgument(arg0 instanceof SoyLegacyObjectMap, "First argument to augmentMap() function is not SoyLegacyObjectMap.");
    Preconditions.checkArgument(arg1 instanceof SoyLegacyObjectMap, "Second argument to augmentMap() function is not SoyLegacyObjectMap.");
    // TODO: Support map with nonstring key.
    Preconditions.checkArgument(arg0 instanceof SoyDict, "First argument to augmentMap() function is not SoyDict. Currently, augmentMap() doesn't" + " support maps that are not dicts (it is a todo).");
    Preconditions.checkArgument(arg1 instanceof SoyDict, "Second argument to augmentMap() function is not SoyDict. Currently, augmentMap() doesn't" + " support maps that are not dicts (it is a todo).");
    return BasicFunctionsRuntime.augmentMap((SoyDict) arg0, (SoyDict) arg1);
}
Also used : SoyLegacyObjectMap(com.google.template.soy.data.SoyLegacyObjectMap) SoyDict(com.google.template.soy.data.SoyDict) SoyValue(com.google.template.soy.data.SoyValue)

Example 2 with SoyLegacyObjectMap

use of com.google.template.soy.data.SoyLegacyObjectMap in project closure-templates by google.

the class EvalVisitor method visitNullSafeItemAccessNode.

private SoyValue visitNullSafeItemAccessNode(ItemAccessNode itemAccess) {
    SoyValue base = visitNullSafeNodeRecurse(itemAccess.getBaseExprChild());
    // attempting item access on non-SoyMap
    if (!(base instanceof SoyLegacyObjectMap || base instanceof SoyMap)) {
        if (base == NullSafetySentinel.INSTANCE) {
            // Bail out if base expression failed a null-safety check.
            return NullSafetySentinel.INSTANCE;
        }
        if (itemAccess.isNullSafe()) {
            if (isNullOrUndefinedBase(base)) {
                // Return the sentinel value that indicates that a null-safety check failed.
                return NullSafetySentinel.INSTANCE;
            } else {
                throw RenderException.create(String.format("While evaluating \"%s\", encountered non-map/list just before accessing \"%s\".", itemAccess.toSourceString(), itemAccess.getSourceStringSuffix()));
            }
        }
        // TODO: If feasible, find and fix existing instances, then throw RenderException here.
        return UndefinedData.INSTANCE;
    }
    // base is a valid SoyMap or SoyLegacyObjectMap: get value
    maybeMarkBadProtoAccess(itemAccess, base);
    SoyValue key = visit(itemAccess.getKeyExprChild());
    SoyType baseType = SoyTypes.tryRemoveNull(itemAccess.getBaseExprChild().getType());
    // We need to know whether to invoke the SoyMap or SoyLegacyObjectMap method.
    // An instanceof check on the runtime value of base is insufficient, since
    // DictImpl implements both interfaces. Instead, look at the declared type of the base
    // expression.
    boolean shouldUseNewMap = MapType.ANY_MAP.isAssignableFrom(baseType);
    SoyValue value = shouldUseNewMap ? ((SoyMap) base).get(key) : ((SoyLegacyObjectMap) base).getItem(key);
    if (value != null && !TofuTypeChecks.isInstance(itemAccess.getType(), value)) {
        throw RenderException.create(String.format("Expected value of type '%s', but actual type was '%s'.", itemAccess.getType(), value.getClass().getSimpleName()));
    }
    if (value != null) {
        return value;
    } else if (shouldUseNewMap) {
        // UndefinedData is a misfeature. The new map type should return null for failed lookups.
        return NullData.INSTANCE;
    } else {
        return UndefinedData.INSTANCE;
    }
}
Also used : SoyLegacyObjectMap(com.google.template.soy.data.SoyLegacyObjectMap) SoyType(com.google.template.soy.types.SoyType) SoyMap(com.google.template.soy.data.SoyMap) SoyValue(com.google.template.soy.data.SoyValue)

Example 3 with SoyLegacyObjectMap

use of com.google.template.soy.data.SoyLegacyObjectMap in project closure-templates by google.

the class LegacyObjectMapToMapFunctionTest method computeForJava.

@Test
public void computeForJava() {
    SoyLegacyObjectMap legacyObjectMap = SoyValueConverterUtility.newDict("x", "y", "z", SoyValueConverterUtility.newDict("xx", 2));
    SoyMapImpl map = SoyMapImpl.forProviderMap(ImmutableMap.of(StringData.forValue("x"), CONVERTER.convert("y"), StringData.forValue("z"), SoyValueConverterUtility.newDict("xx", 2)));
    SoyMapImpl convertedMap = (SoyMapImpl) LEGACY_OBJECT_MAP_TO_MAP.computeForJava(ImmutableList.of(legacyObjectMap));
    assertThat(map.get(StringData.forValue("x"))).isEqualTo(convertedMap.get(StringData.forValue("x")));
}
Also used : SoyMapImpl(com.google.template.soy.data.internal.SoyMapImpl) SoyLegacyObjectMap(com.google.template.soy.data.SoyLegacyObjectMap) Test(org.junit.Test)

Example 4 with SoyLegacyObjectMap

use of com.google.template.soy.data.SoyLegacyObjectMap in project closure-templates by google.

the class AugmentMapFunctionTest method testComputeForJava.

@Test
public void testComputeForJava() {
    AugmentMapFunction augmentMapFunction = new AugmentMapFunction();
    SoyLegacyObjectMap origMap = SoyValueConverterUtility.newDict("aaa", "blah", "bbb", "bleh", "ccc", SoyValueConverterUtility.newDict("xxx", 2));
    SoyLegacyObjectMap additionalMap = SoyValueConverterUtility.newDict("aaa", "bluh", "ccc", SoyValueConverterUtility.newDict("yyy", 5));
    SoyDict augmentedDict = (SoyDict) augmentMapFunction.computeForJava(ImmutableList.<SoyValue>of(origMap, additionalMap));
    assertThat(augmentedDict.getField("aaa").stringValue()).isEqualTo("bluh");
    assertThat(augmentedDict.getItem(StringData.forValue("bbb")).stringValue()).isEqualTo("bleh");
    assertThat(((SoyDict) augmentedDict.getField("ccc")).getField("yyy").integerValue()).isEqualTo(5);
    assertThat(((SoyDict) augmentedDict.getField("ccc")).getField("xxx")).isEqualTo(null);
}
Also used : SoyLegacyObjectMap(com.google.template.soy.data.SoyLegacyObjectMap) SoyDict(com.google.template.soy.data.SoyDict) SoyValue(com.google.template.soy.data.SoyValue) Test(org.junit.Test)

Aggregations

SoyLegacyObjectMap (com.google.template.soy.data.SoyLegacyObjectMap)4 SoyValue (com.google.template.soy.data.SoyValue)3 SoyDict (com.google.template.soy.data.SoyDict)2 Test (org.junit.Test)2 SoyMap (com.google.template.soy.data.SoyMap)1 SoyMapImpl (com.google.template.soy.data.internal.SoyMapImpl)1 SoyType (com.google.template.soy.types.SoyType)1