Skip to content

Fix implementation for slice filter#398

Merged
mattcoley merged 5 commits into
masterfrom
fix-slice-filter
Feb 12, 2020
Merged

Fix implementation for slice filter#398
mattcoley merged 5 commits into
masterfrom
fix-slice-filter

Conversation

@mattcoley

Copy link
Copy Markdown
Collaborator

Slice filter was not implemented as according to spec. When doing list|slice(3) we should not be padding non-divisible slices with null values. When using list|slice(3, 'value') we should pad non-divisible slices with value. https://jinja.palletsprojects.com/en/2.11.x/templates/#slice

@liamrharwood liamrharwood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, just a couple suggestions.

Also, can we update the JinjavaDoc annotation to reflect the second argument?

@@ -47,7 +51,30 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)
}

int slices = NumberUtils.toInt(args[0], 3);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we default to 3 here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would throw an error if args[0] wasn't provided, however we can't do that without breaking existing functionality. Somebody could be using a raw |slice out there.


if (args.length > 1 && currentList != null) {
while (currentList.size() < slices) {
currentList.add(args[1]);

@liamrharwood liamrharwood Feb 12, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assign args[1] to something like fillWith for clarity

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mattcoley mattcoley merged commit 6ec3b09 into master Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants