Skip to content

Commit

Permalink
Basic functionality working with two tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asegal-hs committed Nov 7, 2023
1 parent 0a82efb commit 0e4692a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, BlockInfo> blocks = ArrayListMultimap.create();
private final LinkedList<Node> extendParentRoots = new LinkedList<>();
private final Map<String, RevertibleObject> revertibleObjects = new HashMap<>();
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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<String> extendPaths = new HashSet<>();
Expand Down Expand Up @@ -441,6 +435,7 @@ public String render(Node root, boolean processExtendRoots, long renderLimit) {
}

resolveBlockStubs(output);

if (ignoredOutput.length() > 0) {
return (
EagerReconstructionUtils.labelWithNotes(
Expand All @@ -456,7 +451,6 @@ public String render(Node root, boolean processExtendRoots, long renderLimit) {
output.getValue()
);
}

return output.getValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/RenderFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
}
}

0 comments on commit 0e4692a

Please sign in to comment.