Search in sources :

Example 56 with SoyValue

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

the class StrSubFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    SoyValue arg2 = args.size() == 3 ? args.get(2) : null;
    Preconditions.checkArgument(arg0 instanceof StringData || arg0 instanceof SanitizedContent, "First argument to strSub() function is not StringData or SanitizedContent: %s", arg0);
    Preconditions.checkArgument(arg1 instanceof IntegerData, "Second argument to strSub() function is not IntegerData: %s", arg1);
    if (arg2 != null) {
        Preconditions.checkArgument(arg2 instanceof IntegerData, "Third argument to strSub() function is not IntegerData: %s", arg2);
    }
    String strArg0 = arg0.coerceToString();
    int intArg1 = arg1.integerValue();
    if (arg2 != null) {
        return StringData.forValue(strArg0.substring(intArg1, arg2.integerValue()));
    } else {
        return StringData.forValue(strArg0.substring(intArg1));
    }
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) IntegerData(com.google.template.soy.data.restricted.IntegerData) StringData(com.google.template.soy.data.restricted.StringData) SoyValue(com.google.template.soy.data.SoyValue)

Example 57 with SoyValue

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

the class BidiMarkAfterFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue value = args.get(0);
    boolean isHtml = args.size() == 2 && args.get(1).booleanValue();
    String markAfterKnownDir = BidiFunctionsRuntime.bidiMarkAfter(bidiGlobalDirProvider.get(), value, isHtml);
    return StringData.forValue(markAfterKnownDir);
}
Also used : SoyValue(com.google.template.soy.data.SoyValue)

Example 58 with SoyValue

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

the class InternalValueUtils method convertCompileTimeGlobalsMap.

/**
 * Converts a compile-time globals map in user-provided format into one in the internal format.
 *
 * <p>The returned map will have the same iteration order as the provided map.
 *
 * @param compileTimeGlobalsMap Map from compile-time global name to value. The values can be any
 *     of the Soy primitive types: null, boolean, integer, float (Java double), or string.
 * @return An equivalent map in the internal format.
 * @throws IllegalArgumentException If the map contains an invalid value.
 */
public static ImmutableMap<String, PrimitiveData> convertCompileTimeGlobalsMap(Map<String, ?> compileTimeGlobalsMap) {
    ImmutableMap.Builder<String, PrimitiveData> resultMapBuilder = ImmutableMap.builder();
    for (Map.Entry<String, ?> entry : compileTimeGlobalsMap.entrySet()) {
        Object valueObj = entry.getValue();
        PrimitiveData value;
        boolean isValidValue = true;
        try {
            SoyValue value0 = SoyValueConverter.INSTANCE.convert(valueObj).resolve();
            if (!(value0 instanceof PrimitiveData)) {
                isValidValue = false;
            }
            value = (PrimitiveData) value0;
        } catch (SoyDataException sde) {
            isValidValue = false;
            // make compiler happy
            value = null;
        }
        if (!isValidValue) {
            throw new IllegalArgumentException("Compile-time globals map contains invalid value: " + valueObj + " for key: " + entry.getKey());
        }
        resultMapBuilder.put(entry.getKey(), value);
    }
    return resultMapBuilder.build();
}
Also used : PrimitiveData(com.google.template.soy.data.restricted.PrimitiveData) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) SoyValue(com.google.template.soy.data.SoyValue) ImmutableMap(com.google.common.collect.ImmutableMap) SoyDataException(com.google.template.soy.data.SoyDataException)

Example 59 with SoyValue

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

the class DetachableSoyValueProvider method resolve.

@Override
public final SoyValue resolve() {
    SoyValue local = resolvedValue;
    checkState(local != TombstoneValue.INSTANCE, "called resolve() before status() returned ready.");
    return local;
}
Also used : SoyValue(com.google.template.soy.data.SoyValue)

Example 60 with SoyValue

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

the class MinFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    return BasicFunctionsRuntime.min(arg0, arg1);
}
Also used : SoyValue(com.google.template.soy.data.SoyValue)

Aggregations

SoyValue (com.google.template.soy.data.SoyValue)61 Test (org.junit.Test)31 StringData (com.google.template.soy.data.restricted.StringData)5 ExprNode (com.google.template.soy.exprtree.ExprNode)4 SoyLegacyObjectMap (com.google.template.soy.data.SoyLegacyObjectMap)3 SoyList (com.google.template.soy.data.SoyList)3 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)2 SanitizedContent (com.google.template.soy.data.SanitizedContent)2 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)2 SoyDataException (com.google.template.soy.data.SoyDataException)2 SoyDict (com.google.template.soy.data.SoyDict)2 SoyRecord (com.google.template.soy.data.SoyRecord)2 FloatData (com.google.template.soy.data.restricted.FloatData)2 IntegerData (com.google.template.soy.data.restricted.IntegerData)2 SoyString (com.google.template.soy.data.restricted.SoyString)2 UndefinedData (com.google.template.soy.data.restricted.UndefinedData)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)2