Search in sources :

Example 1 with SoyDataException

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

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

the class RenderVisitorAssistantForMsgs method visitMsgSelectNode.

@Override
protected void visitMsgSelectNode(MsgSelectNode node) {
    ExprRootNode selectExpr = node.getExpr();
    String selectValue;
    try {
        selectValue = master.evalForUseByAssistants(selectExpr, node).stringValue();
    } catch (SoyDataException e) {
        throw RenderException.createWithSource(String.format("Select expression \"%s\" doesn't evaluate to string.", selectExpr.toSourceString()), e, node);
    }
    // Check each case.
    for (CaseOrDefaultNode child : node.getChildren()) {
        if (child instanceof MsgSelectDefaultNode) {
            // This means it didn't match any other case.
            visitChildren(child);
        } else {
            if (((MsgSelectCaseNode) child).getCaseValue().equals(selectValue)) {
                visitChildren(child);
                return;
            }
        }
    }
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) SoyDataException(com.google.template.soy.data.SoyDataException) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 3 with SoyDataException

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

the class RenderVisitorAssistantForMsgs method visitMsgPluralNode.

@Override
protected void visitMsgPluralNode(MsgPluralNode node) {
    ExprRootNode pluralExpr = node.getExpr();
    double pluralValue;
    try {
        pluralValue = master.evalForUseByAssistants(pluralExpr, node).numberValue();
    } catch (SoyDataException e) {
        throw RenderException.createWithSource(String.format("Plural expression \"%s\" doesn't evaluate to number.", pluralExpr.toSourceString()), e, node);
    }
    // Check each case.
    for (CaseOrDefaultNode child : node.getChildren()) {
        if (child instanceof MsgPluralDefaultNode) {
            // This means it didn't match any other case.
            visitChildren(child);
            break;
        } else {
            if (((MsgPluralCaseNode) child).getCaseNumber() == pluralValue) {
                visitChildren(child);
                break;
            }
        }
    }
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) MsgPluralDefaultNode(com.google.template.soy.soytree.MsgPluralDefaultNode) SoyDataException(com.google.template.soy.data.SoyDataException) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 4 with SoyDataException

use of com.google.template.soy.data.SoyDataException 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 5 with SoyDataException

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

the class CollectionData method put.

/**
 * Puts data into this data tree at the specified key string.
 *
 * @param keyStr One or more map keys and/or list indices (separated by '.' if multiple parts).
 *     Indicates the path to the location within this data tree.
 * @param value The data to put at the specified location.
 */
public void put(String keyStr, SoyData value) {
    List<String> keys = split(keyStr, '.');
    int numKeys = keys.size();
    CollectionData collectionData = this;
    for (int i = 0; i <= numKeys - 2; ++i) {
        SoyData nextSoyData = collectionData.getSingle(keys.get(i));
        if (nextSoyData != null && !(nextSoyData instanceof CollectionData)) {
            throw new SoyDataException("Failed to evaluate key string \"" + keyStr + "\" for put().");
        }
        CollectionData nextCollectionData = (CollectionData) nextSoyData;
        if (nextCollectionData == null) {
            // Create the SoyData object that will be bound to keys.get(i). We need to check the first
            // part of keys[i+1] to know whether to create a SoyMapData or SoyListData (checking the
            // first char is sufficient).
            nextCollectionData = (Character.isDigit(keys.get(i + 1).charAt(0))) ? new SoyListData() : new SoyMapData();
            collectionData.putSingle(keys.get(i), nextCollectionData);
        }
        collectionData = nextCollectionData;
    }
    collectionData.putSingle(keys.get(numKeys - 1), ensureValidValue(value));
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) SoyListData(com.google.template.soy.data.SoyListData) SoyDataException(com.google.template.soy.data.SoyDataException) SoyData(com.google.template.soy.data.SoyData)

Aggregations

SoyDataException (com.google.template.soy.data.SoyDataException)5 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)3 SoyValue (com.google.template.soy.data.SoyValue)2 CaseOrDefaultNode (com.google.template.soy.soytree.CaseOrDefaultNode)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SoyData (com.google.template.soy.data.SoyData)1 SoyListData (com.google.template.soy.data.SoyListData)1 SoyMapData (com.google.template.soy.data.SoyMapData)1 IntegerData (com.google.template.soy.data.restricted.IntegerData)1 PrimitiveData (com.google.template.soy.data.restricted.PrimitiveData)1 MsgPluralDefaultNode (com.google.template.soy.soytree.MsgPluralDefaultNode)1 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)1 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)1 DelTemplateKey (com.google.template.soy.soytree.TemplateDelegateNode.DelTemplateKey)1 Map (java.util.Map)1