Search in sources :

Example 1 with SoyValue

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

the class SoyMapImpl method render.

@Override
public void render(LoggingAdvisingAppendable appendable) throws IOException {
    appendable.append('{');
    boolean isFirst = true;
    for (SoyValue key : keys()) {
        SoyValue value = get(key);
        if (isFirst) {
            isFirst = false;
        } else {
            appendable.append(", ");
        }
        key.render(appendable);
        appendable.append(": ");
        value.render(appendable);
    }
    appendable.append('}');
}
Also used : SoyValue(com.google.template.soy.data.SoyValue)

Example 2 with SoyValue

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

the class DictImpl method render.

@Override
public void render(LoggingAdvisingAppendable appendable) throws IOException {
    appendable.append('{');
    boolean isFirst = true;
    boolean useNewSoyMap = typeTracker.type() == RuntimeMapTypeTracker.Type.MAP;
    for (SoyValue key : (useNewSoyMap ? keys() : getItemKeys())) {
        SoyValue value = useNewSoyMap ? get(key) : getItem(key);
        if (isFirst) {
            isFirst = false;
        } else {
            appendable.append(", ");
        }
        key.render(appendable);
        appendable.append(": ");
        value.render(appendable);
    }
    appendable.append('}');
}
Also used : SoyValue(com.google.template.soy.data.SoyValue)

Example 3 with SoyValue

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

the class EvalVisitor method visitModOpNode.

@Override
protected SoyValue visitModOpNode(ModOpNode node) {
    SoyValue operand0 = visit(node.getChild(0));
    SoyValue operand1 = visit(node.getChild(1));
    return convertResult(operand0.longValue() % operand1.longValue());
}
Also used : SoyValue(com.google.template.soy.data.SoyValue)

Example 4 with SoyValue

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

the class EvalVisitor method visitNullSafeFieldAccessNode.

private SoyValue visitNullSafeFieldAccessNode(FieldAccessNode fieldAccess) {
    SoyValue base = visitNullSafeNodeRecurse(fieldAccess.getBaseExprChild());
    // attempting field access on non-SoyRecord
    if (!(base instanceof SoyRecord) && !(base instanceof SoyProtoValue)) {
        if (base == NullSafetySentinel.INSTANCE) {
            // Bail out if base expression failed a null-safety check.
            return NullSafetySentinel.INSTANCE;
        }
        if (fieldAccess.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-record just before accessing \"%s\".", fieldAccess.toSourceString(), fieldAccess.getSourceStringSuffix()));
            }
        }
        // TODO: If feasible, find and fix existing instances, then throw RenderException here.
        return UndefinedData.INSTANCE;
    }
    // the base type is possibly nullable, so remove null before testing for being a proto
    if (SoyTypes.tryRemoveNull(fieldAccess.getBaseExprChild().getType()).getKind() == Kind.PROTO) {
        return ((SoyProtoValue) base).getProtoField(fieldAccess.getFieldName());
    }
    maybeMarkBadProtoAccess(fieldAccess, base);
    // base is a valid SoyRecord: get value
    SoyValue value = ((SoyRecord) base).getField(fieldAccess.getFieldName());
    // TODO(user): Consider cleaning up the null / NullData inconsistencies.
    if (value != null && !TofuTypeChecks.isInstance(fieldAccess.getType(), value)) {
        throw RenderException.create(String.format("Expected value of type '%s', but actual type was '%s'.", fieldAccess.getType(), value.getClass().getSimpleName()));
    }
    return (value != null) ? value : UndefinedData.INSTANCE;
}
Also used : SoyRecord(com.google.template.soy.data.SoyRecord) SoyProtoValue(com.google.template.soy.data.SoyProtoValue) SoyValue(com.google.template.soy.data.SoyValue)

Example 5 with SoyValue

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

the class EvalVisitor method visitProtoInitNode.

@Override
protected SoyValue visitProtoInitNode(ProtoInitNode node) {
    // The downcast is safe because if it was anything else, compilation would have already failed.
    SoyProtoType soyProto = (SoyProtoType) node.getType();
    ImmutableList<String> paramNames = node.getParamNames();
    SoyProtoValueImpl.Builder builder = new SoyProtoValueImpl.Builder(soyProto.getDescriptor());
    for (int i = 0; i < node.numChildren(); i++) {
        SoyValue visit = visit(node.getChild(i));
        // null means don't assign
        if (visit instanceof NullData || visit instanceof UndefinedData) {
            continue;
        }
        builder.setField(paramNames.get(i), visit);
    }
    return builder.build();
}
Also used : NullData(com.google.template.soy.data.restricted.NullData) UndefinedData(com.google.template.soy.data.restricted.UndefinedData) SoyValue(com.google.template.soy.data.SoyValue) SoyProtoType(com.google.template.soy.types.SoyProtoType) SoyProtoValueImpl(com.google.template.soy.data.SoyProtoValueImpl)

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