Search in sources :

Example 1 with IntegerData

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

the class RenderVisitor method visitCallDelegateNode.

@Override
protected void visitCallDelegateNode(CallDelegateNode node) {
    ExprRootNode variantExpr = node.getDelCalleeVariantExpr();
    String variant;
    if (variantExpr == null) {
        variant = "";
    } else {
        try {
            SoyValue variantData = eval(variantExpr, node);
            if (variantData instanceof IntegerData) {
                // An integer constant is being used as variant. Use the value string representation as
                // variant.
                variant = String.valueOf(variantData.longValue());
            } else {
                // Variant is either a StringData or a SanitizedContent. Use the value as a string. If
                // the value is not a string, and exception will be thrown.
                variant = variantData.stringValue();
            }
        } catch (SoyDataException e) {
            throw RenderException.createWithSource(String.format("Variant expression \"%s\" doesn't evaluate to a valid type " + "(Only string and integer are supported).", variantExpr.toSourceString()), e, node);
        }
    }
    DelTemplateKey delegateKey = DelTemplateKey.create(node.getDelCalleeName(), variant);
    TemplateDelegateNode callee;
    try {
        callee = templateRegistry.selectDelTemplate(delegateKey, activeDelPackageSelector);
    } catch (IllegalArgumentException e) {
        throw RenderException.createWithSource(e.getMessage(), e, node);
    }
    if (callee != null) {
        visitCallNodeHelper(node, callee);
    } else if (node.allowEmptyDefault()) {
        // no active delegate implementation, so the call output is empty string
        return;
    } else {
        throw RenderException.createWithSource("Found no active impl for delegate call to \"" + node.getDelCalleeName() + (variant.isEmpty() ? "" : ":" + variant) + "\" (and delcall does not set allowemptydefault=\"true\").", node);
    }
}
Also used : TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) IntegerData(com.google.template.soy.data.restricted.IntegerData) DelTemplateKey(com.google.template.soy.soytree.TemplateDelegateNode.DelTemplateKey) SoyValue(com.google.template.soy.data.SoyValue) SoyDataException(com.google.template.soy.data.SoyDataException) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 2 with IntegerData

use of com.google.template.soy.data.restricted.IntegerData 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)

Aggregations

SoyValue (com.google.template.soy.data.SoyValue)2 IntegerData (com.google.template.soy.data.restricted.IntegerData)2 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 SoyDataException (com.google.template.soy.data.SoyDataException)1 StringData (com.google.template.soy.data.restricted.StringData)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)1 DelTemplateKey (com.google.template.soy.soytree.TemplateDelegateNode.DelTemplateKey)1