use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class KeysFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression soyExpression = args.get(0);
SoyType argType = soyExpression.soyType();
// TODO(lukes): this logic should live in ResolveExpressionTypesVisitor
SoyType listElementType;
if (argType.getKind() == Kind.LEGACY_OBJECT_MAP) {
// pretty much just string
listElementType = ((LegacyObjectMapType) argType).getKeyType();
} else if (argType.getKind() == Kind.LIST) {
listElementType = IntType.getInstance();
} else {
listElementType = UnknownType.getInstance();
}
return SoyExpression.forList(ListType.of(listElementType), JbcSrcMethods.KEYS_FN.invoke(soyExpression.box().checkedCast(SoyLegacyObjectMap.class)));
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class LengthFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression soyExpression = args.get(0);
Expression lengthExpressionAsInt;
if (soyExpression.isBoxed()) {
lengthExpressionAsInt = soyExpression.checkedCast(SoyList.class).invoke(JbcSrcMethods.SOYLIST_LENGTH);
} else {
lengthExpressionAsInt = soyExpression.checkedCast(List.class).invoke(JbcSrcMethods.LIST_SIZE);
}
return SoyExpression.forInt(BytecodeUtils.numericConversion(lengthExpressionAsInt, Type.LONG_TYPE));
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class StrContainsFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression left = args.get(0);
SoyExpression right = args.get(1);
return SoyExpression.forBool(left.unboxAs(String.class).invoke(JbcSrcMethods.STRING_CONTAINS, right.coerceToString()));
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class FloatFunction method computeForJbcSrc.
@Override
public SoyExpression computeForJbcSrc(JbcSrcPluginContext context, List<SoyExpression> args) {
SoyExpression arg = args.get(0);
SoyExpression unboxed = arg.isBoxed() ? arg.unboxAs(long.class) : arg;
SoyExpression result = SoyExpression.forFloat(BytecodeUtils.numericConversion(unboxed, Type.DOUBLE_TYPE));
return arg.isCheap() ? result.asCheap() : result;
}
use of com.google.template.soy.jbcsrc.restricted.SoyExpression in project closure-templates by google.
the class ExpressionCompilerTest method testCollectionLiterals_map.
@Test
public void testCollectionLiterals_map() {
assertExpression("[:]").evaluatesTo(SoyValueConverter.EMPTY_DICT);
// Map values are always boxed. SoyMaps use == for equality, so check equivalence by comparing
// their string representations.
SoyExpression compile = compileExpression("['a': 1, 'b': 1.0, 'c': 'asdf', 'd': false]").coerceToString();
ExpressionTester.assertThatExpression(compile).evaluatesTo(DictImpl.forProviderMap(ImmutableMap.<String, SoyValue>of("a", IntegerData.forValue(1), "b", FloatData.forValue(1.0), "c", StringData.forValue("asdf"), "d", BooleanData.FALSE), RuntimeMapTypeTracker.Type.LEGACY_OBJECT_MAP_OR_RECORD).toString());
}
Aggregations