Search in sources :

Example 1 with Kind

use of com.google.template.soy.types.SoyType.Kind in project closure-templates by google.

the class RenderVisitor method checkStrictParamType.

/**
 * Check that the given {@code paramValue} matches the static type of {@code param}.
 */
private void checkStrictParamType(final TemplateNode node, final TemplateParam param, @Nullable SoyValueProvider paramValue) {
    Kind kind = param.type().getKind();
    if (kind == Kind.ANY || kind == Kind.UNKNOWN) {
        // Nothing to check.  ANY and UKNOWN match all types.
        return;
    }
    if (paramValue == null) {
        paramValue = NullData.INSTANCE;
    } else if (paramValue instanceof SoyAbstractCachingValueProvider) {
        SoyAbstractCachingValueProvider typedValue = (SoyAbstractCachingValueProvider) paramValue;
        if (!typedValue.isComputed()) {
            // in order to preserve laziness we tell the value provider to assert the type when
            // computation is triggered
            typedValue.addValueAssertion(new ValueAssertion() {

                @Override
                public void check(SoyValue value) {
                    checkValueType(param, value, node);
                }
            });
            return;
        }
    }
    checkValueType(param, paramValue.resolve(), node);
}
Also used : ValueAssertion(com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion) Kind(com.google.template.soy.types.SoyType.Kind) ContentKind(com.google.template.soy.data.SanitizedContent.ContentKind) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind) SoyAbstractCachingValueProvider(com.google.template.soy.data.SoyAbstractCachingValueProvider) SoyValue(com.google.template.soy.data.SoyValue)

Example 2 with Kind

use of com.google.template.soy.types.SoyType.Kind in project closure-templates by google.

the class TranslateExprNodeVisitor method genMapKeyCode.

/**
 * Soy strings can be represented by SanitizedContent objects at runtime, so care needs to be
 * taken when indexing into a map with a Soy "string". For pre-ES6 object maps, this isn't a
 * problem, since bracket access implicitly calls toString() on the key, and SanitizedContent
 * overrides toString appropriately. But ES6 Maps and jspb.Maps don't do this automatically, so we
 * need to set it up.
 */
private CodeChunk.WithValue genMapKeyCode(ExprNode keyNode) {
    CodeChunk.WithValue key = visit(keyNode);
    // We need to coerce if the value could possibly a sanitizedcontent object
    boolean needsRuntimeCoercionLogic = false;
    SoyType type = keyNode.getType();
    for (SoyType member : (type instanceof UnionType ? ((UnionType) type).getMembers() : ImmutableList.of(type))) {
        Kind kind = member.getKind();
        needsRuntimeCoercionLogic |= kind.isKnownStringOrSanitizedContent() || kind == Kind.UNKNOWN || kind == Kind.UNION || kind == Kind.ANY;
    }
    return needsRuntimeCoercionLogic ? SOY_MAP_MAYBE_COERCE_KEY_TO_STRING.call(key) : key;
}
Also used : UnionType(com.google.template.soy.types.UnionType) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) SoyType(com.google.template.soy.types.SoyType) Kind(com.google.template.soy.types.SoyType.Kind) SoyErrorKind(com.google.template.soy.error.SoyErrorKind)

Aggregations

Kind (com.google.template.soy.types.SoyType.Kind)2 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)1 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 SoyAbstractCachingValueProvider (com.google.template.soy.data.SoyAbstractCachingValueProvider)1 ValueAssertion (com.google.template.soy.data.SoyAbstractCachingValueProvider.ValueAssertion)1 SoyValue (com.google.template.soy.data.SoyValue)1 SoyErrorKind (com.google.template.soy.error.SoyErrorKind)1 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 SoyType (com.google.template.soy.types.SoyType)1 UnionType (com.google.template.soy.types.UnionType)1