Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make nested interpretation logic part of the ExpressionStrategy interface #731

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.hubspot.jinjava.tree.output.RenderedOutputNode;
import com.hubspot.jinjava.tree.parse.ExpressionToken;
import com.hubspot.jinjava.util.Logging;
import org.apache.commons.lang3.StringUtils;

public class DefaultExpressionStrategy implements ExpressionStrategy {
private static final long serialVersionUID = 436239440273704843L;
Expand All @@ -21,19 +20,11 @@ public RenderedOutputNode interpretOutput(
);
String result = interpreter.getAsString(var);

if (interpreter.getConfig().isNestedInterpretationEnabled()) {
if (
!StringUtils.equals(result, master.getImage()) &&
(
StringUtils.contains(result, master.getSymbols().getExpressionStart()) ||
StringUtils.contains(result, master.getSymbols().getExpressionStartWithTag())
)
) {
try {
result = interpreter.renderFlat(result);
} catch (Exception e) {
Logging.ENGINE_LOG.warn("Error rendering variable node result", e);
}
if (shouldDoNestedInterpretation(result, master, interpreter)) {
try {
result = interpreter.renderFlat(result);
} catch (Exception e) {
Logging.ENGINE_LOG.warn("Error rendering variable node result", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,17 @@ private String eagerResolveExpression(
}
if (eagerExecutionResult.getResult().isFullyResolved()) {
String result = eagerExecutionResult.getResult().toString(true);
if (
!StringUtils.equals(result, master.getImage()) &&
(
StringUtils.contains(result, master.getSymbols().getExpressionStart()) ||
StringUtils.contains(result, master.getSymbols().getExpressionStartWithTag())
)
) {
if (interpreter.getConfig().isNestedInterpretationEnabled()) {
long errorSizeStart = getUnclosedCommentErrorsCount(interpreter);
interpreter.parse(result);
if (getUnclosedCommentErrorsCount(interpreter) == errorSizeStart) {
try {
result = interpreter.renderFlat(result);
} catch (Exception e) {
Logging.ENGINE_LOG.warn("Error rendering variable node result", e);
}
}
} else {
// Possible macro/set tag in front of this one. Includes result
result = wrapInRawOrExpressionIfNeeded(result, interpreter);
long errorSizeStart = getUnclosedCommentErrorsCount(interpreter);

if (shouldDoNestedInterpretation(result, master, interpreter)) {
try {
result = interpreter.renderFlat(result);
} catch (Exception e) {
Logging.ENGINE_LOG.warn("Error rendering variable node result", e);
}
} else if (getUnclosedCommentErrorsCount(interpreter) == errorSizeStart) {
// Possible macro/set tag in front of this one. Includes result
result = wrapInRawOrExpressionIfNeeded(result, master, interpreter);
}

if (interpreter.getContext().isAutoEscape()) {
Expand Down Expand Up @@ -113,6 +103,22 @@ private String eagerResolveExpression(
);
}

@Override
public boolean shouldDoNestedInterpretation(
String result,
ExpressionToken master,
JinjavaInterpreter interpreter
) {
if (
ExpressionStrategy.super.shouldDoNestedInterpretation(result, master, interpreter)
) {
long errorSizeStart = getUnclosedCommentErrorsCount(interpreter);
interpreter.parse(result);
return getUnclosedCommentErrorsCount(interpreter) == errorSizeStart;
}
return false;
}

private long getUnclosedCommentErrorsCount(JinjavaInterpreter interpreter) {
return interpreter
.getErrors()
Expand All @@ -122,21 +128,23 @@ private long getUnclosedCommentErrorsCount(JinjavaInterpreter interpreter) {
}

private static String wrapInRawOrExpressionIfNeeded(
String output,
String result,
ExpressionToken master,
JinjavaInterpreter interpreter
) {
JinjavaConfig config = interpreter.getConfig();
if (
config.getExecutionMode().isPreserveRawTags() &&
!interpreter.getContext().isUnwrapRawOverride() &&
!StringUtils.equals(result, master.getImage()) &&
(
output.contains(config.getTokenScannerSymbols().getExpressionStart()) ||
output.contains(config.getTokenScannerSymbols().getExpressionStartWithTag())
result.contains(master.getSymbols().getExpressionStart()) ||
result.contains(master.getSymbols().getExpressionStartWithTag())
)
) {
return EagerTagDecorator.wrapInTag(output, RawTag.TAG_NAME, interpreter);
return EagerTagDecorator.wrapInTag(result, RawTag.TAG_NAME, interpreter);
}
return output;
return result;
}

private static String wrapInExpression(String output, JinjavaInterpreter interpreter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
import com.hubspot.jinjava.tree.output.RenderedOutputNode;
import com.hubspot.jinjava.tree.parse.ExpressionToken;
import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;

public interface ExpressionStrategy extends Serializable {
RenderedOutputNode interpretOutput(
ExpressionToken master,
JinjavaInterpreter interpreter
);

default boolean shouldDoNestedInterpretation(
String result,
ExpressionToken master,
JinjavaInterpreter interpreter
) {
return (
interpreter.getConfig().isNestedInterpretationEnabled() &&
!StringUtils.equals(result, master.getImage()) &&
(
StringUtils.contains(result, master.getSymbols().getExpressionStart()) ||
StringUtils.contains(result, master.getSymbols().getExpressionStartWithTag())
)
);
}
}