-
Notifications
You must be signed in to change notification settings - Fork 170
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
Fixes #159: Adds dict support for DefaultFilter #386
Changes from 4 commits
5c495af
8487a16
d097b17
7abb0cc
ea62429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,16 @@ | |
**********************************************************************/ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
import java.util.Objects; | ||
|
||
import org.apache.commons.lang3.BooleanUtils; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.interpret.TemplateSyntaxException; | ||
import com.hubspot.jinjava.objects.PyWrapper; | ||
import com.hubspot.jinjava.util.ObjectTruthValue; | ||
|
||
@JinjavaDoc( | ||
|
@@ -42,15 +45,15 @@ | |
public class DefaultFilter implements Filter { | ||
|
||
@Override | ||
public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { | ||
public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) { | ||
boolean truthy = false; | ||
|
||
if (args.length < 1) { | ||
throw new TemplateSyntaxException(interpreter, getName(), "requires either 1 (default value to use) or 2 (default value to use, default with variables that evaluate to false) arguments"); | ||
} | ||
|
||
if (args.length > 1) { | ||
truthy = BooleanUtils.toBoolean(args[1]); | ||
truthy = BooleanUtils.toBoolean(Objects.toString(args[1])); | ||
} | ||
|
||
if (truthy) { | ||
|
@@ -61,7 +64,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar | |
return object; | ||
} | ||
|
||
return args[0]; | ||
return (args[0] instanceof PyWrapper) ? args[0] : Objects.toString(args[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the only real significant change, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. basically checking if the argument is an instance of PyWrapper then we do not need to convert it into String. |
||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do this all in one loop. In fact, you don't need
stringArgs
at all. I know this was just moved from another file, but might as well clean it up while we're in here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done 👍