Skip to content

Commit 25a9c3c

Browse files
committed
Make all eager ast nodes provide methods for getting partially resolved versions of themselves
1 parent ef10b65 commit 25a9c3c

20 files changed

Lines changed: 380 additions & 209 deletions

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstBinary.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,41 @@ public Object eval(Bindings bindings, ELContext context) {
4141
hasEvalResult = true;
4242
return evalResult;
4343
} catch (DeferredParsingException e) {
44-
String sb =
45-
EvalResultHolder.reconstructNode(bindings, context, left, e, false) +
46-
String.format(" %s ", operator.toString()) +
47-
EvalResultHolder.reconstructNode(
48-
bindings,
49-
(operator instanceof OrOperator || operator == AstBinary.AND)
50-
? new NoInvokeELContext(context) // short circuit on modification attempts because this may not be evaluated
51-
: context,
52-
right,
53-
e,
54-
false
55-
);
56-
throw new DeferredParsingException(this, sb);
44+
throw new DeferredParsingException(
45+
this,
46+
getPartiallyResolved(bindings, context, e, false)
47+
);
5748
}
5849
}
5950

51+
@Override
52+
public String getPartiallyResolved(
53+
Bindings bindings,
54+
ELContext context,
55+
DeferredParsingException deferredParsingException,
56+
boolean preserveIdentifier
57+
) {
58+
return (
59+
EvalResultHolder.reconstructNode(
60+
bindings,
61+
context,
62+
left,
63+
deferredParsingException,
64+
false
65+
) +
66+
String.format(" %s ", operator.toString()) +
67+
EvalResultHolder.reconstructNode(
68+
bindings,
69+
(operator instanceof OrOperator || operator == AstBinary.AND)
70+
? new NoInvokeELContext(context) // short circuit on modification attempts because this may not be evaluated
71+
: context,
72+
right,
73+
deferredParsingException,
74+
false
75+
)
76+
);
77+
}
78+
6079
@Override
6180
public Object getEvalResult() {
6281
return evalResult;

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstBracket.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import javax.el.ELContext;
99
import javax.el.ELException;
1010

11-
public class EagerAstBracket
12-
extends AstBracket
13-
implements EvalResultHolder, PartiallyResolvable {
11+
public class EagerAstBracket extends AstBracket implements EvalResultHolder {
1412
protected Object evalResult;
1513
protected boolean hasEvalResult;
1614

@@ -40,8 +38,10 @@ public Object eval(Bindings bindings, ELContext context) {
4038
DeferredParsingException e = EvalResultHolder.convertToDeferredParsingException(
4139
originalException
4240
);
43-
String sb = getPartiallyResolved(bindings, context, e, false);
44-
throw new DeferredParsingException(this, sb);
41+
throw new DeferredParsingException(
42+
this,
43+
getPartiallyResolved(bindings, context, e, false)
44+
);
4545
}
4646
}
4747

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstChoice.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ public Object eval(Bindings bindings, ELContext context) throws ELException {
4444
// the question was evaluated so jump to either yes or no
4545
throw new DeferredParsingException(this, e.getDeferredEvalResult());
4646
}
47-
String sb =
48-
e.getDeferredEvalResult() +
49-
" ? " +
50-
EvalResultHolder.reconstructNode(bindings, context, yes, e, false) +
51-
" : " +
52-
EvalResultHolder.reconstructNode(bindings, context, no, e, false);
53-
throw new DeferredParsingException(this, sb);
47+
throw new DeferredParsingException(
48+
this,
49+
getPartiallyResolved(bindings, context, e, false)
50+
);
5451
}
5552
}
5653

@@ -73,29 +70,36 @@ public boolean hasEvalResult() {
7370
return hasEvalResult;
7471
}
7572

73+
@Override
7674
public String getPartiallyResolved(
7775
Bindings bindings,
7876
ELContext context,
7977
DeferredParsingException deferredParsingException,
8078
boolean preserveIdentifier
8179
) {
8280
return (
83-
deferredParsingException.getDeferredEvalResult() +
81+
EvalResultHolder.reconstructNode(
82+
bindings,
83+
context,
84+
question,
85+
deferredParsingException,
86+
false
87+
) +
8488
" ? " +
8589
EvalResultHolder.reconstructNode(
8690
bindings,
8791
context,
8892
yes,
8993
deferredParsingException,
90-
false
94+
preserveIdentifier
9195
) +
9296
" : " +
9397
EvalResultHolder.reconstructNode(
9498
bindings,
9599
context,
96100
no,
97101
deferredParsingException,
98-
false
102+
preserveIdentifier
99103
)
100104
);
101105
}

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstDict.java

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.hubspot.jinjava.el.ext.ExtendedParser;
66
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
77
import de.odysseus.el.tree.Bindings;
8+
import de.odysseus.el.tree.impl.ast.AstIdentifier;
89
import de.odysseus.el.tree.impl.ast.AstNode;
910
import java.util.Map;
1011
import java.util.StringJoiner;
@@ -25,46 +26,61 @@ public Object eval(Bindings bindings, ELContext context) {
2526
hasEvalResult = true;
2627
return evalResult;
2728
} catch (DeferredParsingException e) {
28-
JinjavaInterpreter interpreter = (JinjavaInterpreter) context
29-
.getELResolver()
30-
.getValue(context, null, ExtendedParser.INTERPRETER);
31-
StringJoiner joiner = new StringJoiner(", ");
32-
dict.forEach(
33-
(key, value) -> {
34-
StringJoiner kvJoiner = new StringJoiner(": ");
35-
if (key instanceof EvalResultHolder) {
36-
kvJoiner.add(
37-
EvalResultHolder.reconstructNode(
38-
bindings,
39-
context,
40-
(EvalResultHolder) key,
41-
e,
42-
!interpreter.getConfig().getLegacyOverrides().isEvaluateMapKeys()
43-
)
44-
);
45-
} else {
46-
kvJoiner.add(key.toString());
47-
}
48-
if (value instanceof EvalResultHolder) {
49-
kvJoiner.add(
50-
EvalResultHolder.reconstructNode(
51-
bindings,
52-
context,
53-
(EvalResultHolder) value,
54-
e,
55-
false
56-
)
57-
);
58-
} else {
59-
kvJoiner.add(value.toString());
60-
}
61-
joiner.add(kvJoiner.toString());
62-
}
29+
throw new DeferredParsingException(
30+
this,
31+
getPartiallyResolved(bindings, context, e, false)
6332
);
64-
throw new DeferredParsingException(this, String.format("{%s}", joiner.toString()));
6533
}
6634
}
6735

36+
@Override
37+
public String getPartiallyResolved(
38+
Bindings bindings,
39+
ELContext context,
40+
DeferredParsingException deferredParsingException,
41+
boolean preserveIdentifier
42+
) {
43+
JinjavaInterpreter interpreter = (JinjavaInterpreter) context
44+
.getELResolver()
45+
.getValue(context, null, ExtendedParser.INTERPRETER);
46+
StringJoiner joiner = new StringJoiner(", ");
47+
dict.forEach(
48+
(key, value) -> {
49+
StringJoiner kvJoiner = new StringJoiner(": ");
50+
if (key instanceof AstIdentifier) {
51+
kvJoiner.add(((AstIdentifier) key).getName());
52+
} else if (key instanceof EvalResultHolder) {
53+
kvJoiner.add(
54+
EvalResultHolder.reconstructNode(
55+
bindings,
56+
context,
57+
(EvalResultHolder) key,
58+
deferredParsingException,
59+
!interpreter.getConfig().getLegacyOverrides().isEvaluateMapKeys()
60+
)
61+
);
62+
} else {
63+
kvJoiner.add(key.toString());
64+
}
65+
if (value instanceof EvalResultHolder) {
66+
kvJoiner.add(
67+
EvalResultHolder.reconstructNode(
68+
bindings,
69+
context,
70+
(EvalResultHolder) value,
71+
deferredParsingException,
72+
preserveIdentifier
73+
)
74+
);
75+
} else {
76+
kvJoiner.add(value.toString());
77+
}
78+
joiner.add(kvJoiner.toString());
79+
}
80+
);
81+
return String.format("{%s}", joiner);
82+
}
83+
6884
@Override
6985
public Object getEvalResult() {
7086
return evalResult;

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstDot.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import javax.el.ELContext;
99
import javax.el.ELException;
1010

11-
public class EagerAstDot extends AstDot implements EvalResultHolder, PartiallyResolvable {
11+
public class EagerAstDot extends AstDot implements EvalResultHolder {
1212
protected Object evalResult;
1313
protected boolean hasEvalResult;
1414
protected final EvalResultHolder base;
@@ -49,7 +49,6 @@ public Object eval(Bindings bindings, ELContext context) throws ELException {
4949
DeferredParsingException e = EvalResultHolder.convertToDeferredParsingException(
5050
originalException
5151
);
52-
5352
throw new DeferredParsingException(
5453
this,
5554
getPartiallyResolved(bindings, context, e, false)
@@ -63,7 +62,6 @@ public String getPartiallyResolved(
6362
DeferredParsingException e,
6463
boolean preserveIdentifier
6564
) {
66-
clearEvalResult();
6765
return String.format(
6866
"%s.%s",
6967
EvalResultHolder.reconstructNode(bindings, context, base, e, preserveIdentifier),

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstIdentifier.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import javax.el.ELContext;
88
import javax.el.ELException;
99

10-
public class EagerAstIdentifier
11-
extends AstIdentifier
12-
implements EvalResultHolder, PartiallyResolvable {
10+
public class EagerAstIdentifier extends AstIdentifier implements EvalResultHolder {
1311
protected Object evalResult;
1412
protected boolean hasEvalResult;
1513

@@ -51,7 +49,6 @@ public String getPartiallyResolved(
5149
DeferredParsingException deferredParsingException,
5250
boolean preserveIdentifier
5351
) {
54-
clearEvalResult();
5552
return getName();
5653
}
5754
}

src/main/java/com/hubspot/jinjava/el/ext/eager/EagerAstList.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,13 @@ public EagerAstList(AstParameters elements) {
1919
public Object eval(Bindings bindings, ELContext context) {
2020
try {
2121
evalResult = super.eval(bindings, context);
22+
hasEvalResult = true;
2223
return evalResult;
2324
} catch (DeferredParsingException e) {
24-
StringJoiner joiner = new StringJoiner(", ");
25-
for (int i = 0; i < elements.getCardinality(); i++) {
26-
joiner.add(
27-
EvalResultHolder.reconstructNode(
28-
bindings,
29-
context,
30-
(EvalResultHolder) elements.getChild(i),
31-
e,
32-
false
33-
)
34-
);
35-
}
36-
throw new DeferredParsingException(this, "[" + joiner.toString() + "]");
25+
throw new DeferredParsingException(
26+
this,
27+
getPartiallyResolved(bindings, context, e, false)
28+
);
3729
}
3830
}
3931

@@ -55,4 +47,26 @@ public void clearEvalResult() {
5547
public boolean hasEvalResult() {
5648
return hasEvalResult;
5749
}
50+
51+
@Override
52+
public String getPartiallyResolved(
53+
Bindings bindings,
54+
ELContext context,
55+
DeferredParsingException deferredParsingException,
56+
boolean preserveIdentifier
57+
) {
58+
StringJoiner joiner = new StringJoiner(", ");
59+
for (int i = 0; i < elements.getCardinality(); i++) {
60+
joiner.add(
61+
EvalResultHolder.reconstructNode(
62+
bindings,
63+
context,
64+
(EvalResultHolder) elements.getChild(i),
65+
deferredParsingException,
66+
preserveIdentifier
67+
)
68+
);
69+
}
70+
return "[" + joiner.toString() + "]";
71+
}
5872
}

0 commit comments

Comments
 (0)