Search in sources :

Example 11 with MapLiteral

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral in project sling by apache.

the class SyntheticMapRemoval method overrideMap.

private MapLiteral overrideMap(String variableName, MapLiteral mapLiteral) {
    Map<String, ExpressionNode> newLiteral = new HashMap<>();
    for (Map.Entry<String, ExpressionNode> entry : mapLiteral.getMap().entrySet()) {
        String property = entry.getKey();
        ExpressionNode valueNode = entry.getValue();
        String valueVariable = valueVariableName(variableName, property);
        newLiteral.put(property, new Identifier(valueVariable));
        outputStream.write(new VariableBinding.Start(valueVariable, valueNode));
    }
    return new MapLiteral(newLiteral);
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) HashMap(java.util.HashMap) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) HashMap(java.util.HashMap) Map(java.util.Map) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 12 with MapLiteral

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral in project sling by apache.

the class ExpressionReducer method evaluate.

@Override
public EvalResult evaluate(MapLiteral mapLiteral) {
    HashMap<String, EvalResult> results = new HashMap<>();
    boolean isConstant = true;
    for (Map.Entry<String, ExpressionNode> entry : mapLiteral.getMap().entrySet()) {
        EvalResult result = eval(entry.getValue());
        results.put(entry.getKey(), result);
        isConstant = isConstant && result.isConstant();
    }
    if (isConstant) {
        HashMap<String, Object> map = new HashMap<>();
        for (Map.Entry<String, EvalResult> entry : results.entrySet()) {
            map.put(entry.getKey(), entry.getValue().getValue());
        }
        return EvalResult.constant(map);
    } else {
        HashMap<String, ExpressionNode> literal = new HashMap<>();
        for (Map.Entry<String, EvalResult> entry : results.entrySet()) {
            literal.put(entry.getKey(), entry.getValue().getNode());
        }
        return EvalResult.nonConstant(new MapLiteral(literal));
    }
}
Also used : HashMap(java.util.HashMap) MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)12 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)8 HashMap (java.util.HashMap)7 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)7 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)6 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)5 Map (java.util.Map)4 Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)4 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)3 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)2 Loop (org.apache.sling.scripting.sightly.compiler.commands.Loop)2 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)2 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)2 UnaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation)2 ArrayList (java.util.ArrayList)1 SightlyCompilerException (org.apache.sling.scripting.sightly.compiler.SightlyCompilerException)1 Procedure (org.apache.sling.scripting.sightly.compiler.commands.Procedure)1 Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)1 ArrayLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)1 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)1