Search in sources :

Example 1 with PrimitiveData

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

the class RewriteGlobalsPass method resolveGlobal.

private void resolveGlobal(GlobalNode global) {
    // First check to see if this global matches a proto enum.  We do this because the enums from
    // the type registry have better type information and for applications with legacy globals
    // configs there is often overlap, so the order in which we check is actually important.
    // proto enums are dotted identifiers
    String name = global.getName();
    int lastDot = name.lastIndexOf('.');
    if (lastDot > 0) {
        String enumTypeName = name.substring(0, lastDot);
        SoyType type = typeRegistry.getType(enumTypeName);
        if (type != null && type.getKind() == SoyType.Kind.PROTO_ENUM) {
            SoyProtoEnumType enumType = (SoyProtoEnumType) type;
            String enumMemberName = name.substring(lastDot + 1);
            Integer enumValue = enumType.getValue(enumMemberName);
            if (enumValue != null) {
                // TODO(lukes): consider introducing a new PrimitiveNode for enums
                global.resolve(enumType, new IntegerNode(enumValue, global.getSourceLocation()));
            } else {
                // If we found the type definition but not the value, then that's an error
                // regardless of whether we're allowing unbound globals or not.
                errorReporter.report(global.getSourceLocation(), ENUM_MEMBERSHIP_ERROR, enumMemberName, enumTypeName);
            }
            // TODO(lukes): issue a warning if a registered global also matches
            return;
        }
    }
    // if that doesn't work, see if it was registered in the globals file.
    PrimitiveData value = compileTimeGlobals.get(global.getName());
    if (value != null) {
        PrimitiveNode expr = InternalValueUtils.convertPrimitiveDataToExpr(value, global.getSourceLocation());
        global.resolve(expr.getType(), expr);
    }
}
Also used : PrimitiveNode(com.google.template.soy.exprtree.ExprNode.PrimitiveNode) PrimitiveData(com.google.template.soy.data.restricted.PrimitiveData) IntegerNode(com.google.template.soy.exprtree.IntegerNode) SoyType(com.google.template.soy.types.SoyType) SoyProtoEnumType(com.google.template.soy.types.SoyProtoEnumType)

Example 2 with PrimitiveData

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

the class SoyUtils method parseCompileTimeGlobals.

/**
 * Parses a globals file in the format created by {@link #generateCompileTimeGlobalsFile} into a
 * map from global name to primitive value.
 *
 * @param inputSource A source that returns a reader for the globals file.
 * @return The parsed globals map.
 * @throws IOException If an error occurs while reading the globals file.
 * @throws IllegalStateException If the globals file is not in the correct format.
 */
public static ImmutableMap<String, PrimitiveData> parseCompileTimeGlobals(CharSource inputSource) throws IOException {
    Builder<String, PrimitiveData> compileTimeGlobalsBuilder = ImmutableMap.builder();
    ErrorReporter errorReporter = ErrorReporter.exploding();
    try (BufferedReader reader = inputSource.openBufferedStream()) {
        int lineNum = 1;
        for (String line = reader.readLine(); line != null; line = reader.readLine(), ++lineNum) {
            if (line.startsWith("//") || line.trim().length() == 0) {
                continue;
            }
            SourceLocation sourceLocation = new SourceLocation("globals", lineNum, 1, lineNum, 1);
            Matcher matcher = COMPILE_TIME_GLOBAL_LINE.matcher(line);
            if (!matcher.matches()) {
                errorReporter.report(sourceLocation, INVALID_FORMAT, line);
                continue;
            }
            String name = matcher.group(1);
            String valueText = matcher.group(2).trim();
            ExprNode valueExpr = SoyFileParser.parseExprOrDie(valueText);
            // TODO: Consider allowing non-primitives (e.g. list/map literals).
            if (!(valueExpr instanceof PrimitiveNode)) {
                if (valueExpr instanceof GlobalNode || valueExpr instanceof VarRefNode) {
                    errorReporter.report(sourceLocation, INVALID_VALUE, valueExpr.toSourceString());
                } else {
                    errorReporter.report(sourceLocation, NON_PRIMITIVE_VALUE, valueExpr.toSourceString());
                }
                continue;
            }
            // Default case.
            compileTimeGlobalsBuilder.put(name, InternalValueUtils.convertPrimitiveExprToData((PrimitiveNode) valueExpr));
        }
    }
    return compileTimeGlobalsBuilder.build();
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ExprNode(com.google.template.soy.exprtree.ExprNode) PrimitiveNode(com.google.template.soy.exprtree.ExprNode.PrimitiveNode) PrimitiveData(com.google.template.soy.data.restricted.PrimitiveData) ErrorReporter(com.google.template.soy.error.ErrorReporter) VarRefNode(com.google.template.soy.exprtree.VarRefNode) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Example 3 with PrimitiveData

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

Aggregations

PrimitiveData (com.google.template.soy.data.restricted.PrimitiveData)3 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 SoyDataException (com.google.template.soy.data.SoyDataException)1 SoyValue (com.google.template.soy.data.SoyValue)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 GlobalNode (com.google.template.soy.exprtree.GlobalNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 SoyProtoEnumType (com.google.template.soy.types.SoyProtoEnumType)1 SoyType (com.google.template.soy.types.SoyType)1 BufferedReader (java.io.BufferedReader)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1