-
Notifications
You must be signed in to change notification settings - Fork 169
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
Filter upgrades to support kw params #531
Changes from all commits
eab8cb4
279c035
a571c8a
e5b850b
f1df1b3
7ae931d
8de2f09
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.lib.fn.Functions; | ||
import com.hubspot.jinjava.objects.date.StrftimeFormatter; | ||
import java.util.Map; | ||
|
||
@JinjavaDoc( | ||
value = "Formats a date object", | ||
|
@@ -17,19 +18,19 @@ | |
), | ||
params = { | ||
@JinjavaParam( | ||
value = "format", | ||
value = DateTimeFormatFilter.FORMAT_PARAM, | ||
defaultValue = StrftimeFormatter.DEFAULT_DATE_FORMAT, | ||
desc = "The format of the date determined by the directives added to this parameter" | ||
), | ||
@JinjavaParam( | ||
value = "timezone", | ||
defaultValue = "utc", | ||
value = DateTimeFormatFilter.TIMEZONE_PARAM, | ||
defaultValue = "UTC", | ||
desc = "Time zone of output date" | ||
), | ||
@JinjavaParam( | ||
value = "locale", | ||
value = DateTimeFormatFilter.LOCALE_PARAM, | ||
type = "string", | ||
defaultValue = "us", | ||
defaultValue = "en-US", | ||
desc = "The language code to use when formatting the datetime" | ||
) | ||
}, | ||
|
@@ -40,19 +41,29 @@ | |
) | ||
} | ||
) | ||
public class DateTimeFormatFilter implements Filter { | ||
public class DateTimeFormatFilter extends AbstractFilter implements Filter { | ||
public static final String FORMAT_PARAM = "format"; | ||
public static final String TIMEZONE_PARAM = "timezone"; | ||
public static final String LOCALE_PARAM = "locale"; | ||
|
||
@Override | ||
public String getName() { | ||
return "datetimeformat"; | ||
} | ||
|
||
@Override | ||
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { | ||
if (args.length > 0) { | ||
return Functions.dateTimeFormat(var, args); | ||
} else { | ||
public Object filter( | ||
Object var, | ||
JinjavaInterpreter interpreter, | ||
Map<String, Object> parsedArgs | ||
) { | ||
String format = (String) parsedArgs.get(FORMAT_PARAM); | ||
String timezone = (String) parsedArgs.get(TIMEZONE_PARAM); | ||
String locale = (String) parsedArgs.get(LOCALE_PARAM); | ||
if (format == null && timezone == null && locale == null) { | ||
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. Must all of these be null? 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. I could probably fix up the Functions code, but currently it errors out on zone being null. Let me know if you are happy for me to change it |
||
return Functions.dateTimeFormat(var); | ||
} else { | ||
return Functions.dateTimeFormat(var, format, timezone, locale); | ||
} | ||
} | ||
} |
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.
I really appreciate the static constants!