From 0e4692af1053e9fc4e0fdd31b509c247d37c144b Mon Sep 17 00:00:00 2001 From: Aaron Segal Date: Tue, 7 Nov 2023 14:05:28 -0500 Subject: [PATCH] Basic functionality working with two tests --- .../jinjava/interpret/JinjavaInterpreter.java | 18 ++++++------------ .../jinjava/lib/filter/RenderFilter.java | 5 +---- .../jinjava/lib/filter/RenderFilterTest.java | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index d46568fc2..2e66bd233 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -75,8 +75,6 @@ public class JinjavaInterpreter implements PyishSerializable { public static final String IGNORED_OUTPUT_FROM_EXTENDS_NOTE = "ignored_output_from_extends"; - public static final long NO_LIMIT = -1; - private final Multimap blocks = ArrayListMultimap.create(); private final LinkedList extendParentRoots = new LinkedList<>(); private final Map revertibleObjects = new HashMap<>(); @@ -261,7 +259,7 @@ public String renderFlat(String template) { * @return rendered result */ public String render(String template) { - return render(template, NO_LIMIT); + return render(template, config.getMaxOutputSize()); } public String render(String template, long renderLimit) { @@ -276,7 +274,7 @@ public String render(String template, long renderLimit) { * @return rendered result */ public String render(Node root) { - return render(root, true, NO_LIMIT); + return render(root, true, config.getMaxOutputSize()); } /** @@ -288,7 +286,7 @@ public String render(Node root) { * @return */ public String render(Node root, boolean processExtendRoots) { - return render(root, processExtendRoots, NO_LIMIT); + return render(root, processExtendRoots, config.getMaxOutputSize()); } /** @@ -315,11 +313,7 @@ public String render(Node root, long renderLimit) { * @return rendered result */ public String render(Node root, boolean processExtendRoots, long renderLimit) { - long maxOutput = (renderLimit == NO_LIMIT) - ? config.getMaxOutputSize() - : (Math.min(renderLimit, config.getMaxOutputSize())); - OutputList output = new OutputList(maxOutput); - + OutputList output = new OutputList(renderLimit); for (Node node : root.getChildren()) { lineNumber = node.getLineNumber(); position = node.getStartPosition(); @@ -375,8 +369,8 @@ public String render(Node root, boolean processExtendRoots, long renderLimit) { return output.getValue(); } } - StringBuilder ignoredOutput = new StringBuilder(); + StringBuilder ignoredOutput = new StringBuilder(); // render all extend parents, keeping the last as the root output if (processExtendRoots) { Set extendPaths = new HashSet<>(); @@ -441,6 +435,7 @@ public String render(Node root, boolean processExtendRoots, long renderLimit) { } resolveBlockStubs(output); + if (ignoredOutput.length() > 0) { return ( EagerReconstructionUtils.labelWithNotes( @@ -456,7 +451,6 @@ public String render(Node root, boolean processExtendRoots, long renderLimit) { output.getValue() ); } - return output.getValue(); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RenderFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RenderFilter.java index 1a2809e5a..405148d40 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RenderFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RenderFilter.java @@ -27,10 +27,7 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { if (args.length > 0) { String firstArg = args[0]; - return interpreter.render( - Objects.toString(var), - NumberUtils.toLong(firstArg, JinjavaInterpreter.NO_LIMIT) - ); + return interpreter.render(Objects.toString(var), NumberUtils.toLong(firstArg, 0)); } return interpreter.render(Objects.toString(var)); } diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/RenderFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/RenderFilterTest.java index 04e238b7a..bd5e80011 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/RenderFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/RenderFilterTest.java @@ -20,4 +20,18 @@ public void itRendersObject() { assertThat(filter.filter(stringToRender, interpreter)).isEqualTo("world"); } + + @Test + public void itRendersObjectWithinLimit() { + String stringToRender = "{% if null %}Hello{% else %}world{% endif %}"; + + assertThat(filter.filter(stringToRender, interpreter, "5")).isEqualTo("world"); + } + + @Test + public void itDoesNotRenderObjectOverLimit() { + String stringToRender = "{% if null %}Hello{% else %}world{% endif %}"; + + assertThat(filter.filter(stringToRender, interpreter, "4")).isEqualTo(""); + } }