From 5c495af73e178f07815723b88975b07db74ca2cb Mon Sep 17 00:00:00 2001 From: Manish Devgan Date: Thu, 16 Jan 2020 02:36:04 +0530 Subject: [PATCH 1/5] Fixes #159: Adds dict support for DefaultFilter --- .../jinjava/benchmarks/liquid/Filters.java | 34 +++++++++---------- .../hubspot/jinjava/lib/filter/AbsFilter.java | 2 +- .../hubspot/jinjava/lib/filter/AddFilter.java | 5 +-- .../jinjava/lib/filter/AdvancedFilter.java | 2 +- .../jinjava/lib/filter/AttrFilter.java | 6 ++-- .../jinjava/lib/filter/BatchFilter.java | 5 +-- .../jinjava/lib/filter/BoolFilter.java | 2 +- .../jinjava/lib/filter/CapitalizeFilter.java | 2 +- .../jinjava/lib/filter/CenterFilter.java | 6 ++-- .../hubspot/jinjava/lib/filter/CutFilter.java | 4 +-- .../lib/filter/DateTimeFormatFilter.java | 17 ++++++++-- .../jinjava/lib/filter/DatetimeFilter.java | 2 +- .../jinjava/lib/filter/DefaultFilter.java | 6 ++-- .../jinjava/lib/filter/DictSortFilter.java | 7 ++-- .../jinjava/lib/filter/DivideFilter.java | 5 +-- .../jinjava/lib/filter/DivisibleFilter.java | 6 ++-- .../jinjava/lib/filter/EscapeFilter.java | 2 +- .../lib/filter/EscapeJinjavaFilter.java | 2 +- .../jinjava/lib/filter/EscapeJsFilter.java | 2 +- .../jinjava/lib/filter/EscapeJsonFilter.java | 2 +- .../lib/filter/FileSizeFormatFilter.java | 4 +-- .../hubspot/jinjava/lib/filter/Filter.java | 2 +- .../jinjava/lib/filter/FirstFilter.java | 2 +- .../jinjava/lib/filter/FloatFilter.java | 5 +-- .../jinjava/lib/filter/ForceEscapeFilter.java | 2 +- .../jinjava/lib/filter/FromJsonFilter.java | 2 +- .../jinjava/lib/filter/GroupByFilter.java | 4 +-- .../jinjava/lib/filter/IndentFilter.java | 6 ++-- .../hubspot/jinjava/lib/filter/IntFilter.java | 5 +-- .../jinjava/lib/filter/IpAddrFilter.java | 5 +-- .../jinjava/lib/filter/JoinFilter.java | 6 ++-- .../jinjava/lib/filter/LastFilter.java | 2 +- .../jinjava/lib/filter/LengthFilter.java | 2 +- .../jinjava/lib/filter/ListFilter.java | 2 +- .../hubspot/jinjava/lib/filter/LogFilter.java | 5 +-- .../jinjava/lib/filter/LowerFilter.java | 2 +- .../hubspot/jinjava/lib/filter/Md5Filter.java | 2 +- .../jinjava/lib/filter/MultiplyFilter.java | 5 +-- .../jinjava/lib/filter/PrettyPrintFilter.java | 2 +- .../jinjava/lib/filter/RandomFilter.java | 2 +- .../lib/filter/RegexReplaceFilter.java | 8 +++-- .../jinjava/lib/filter/RejectFilter.java | 5 +-- .../jinjava/lib/filter/ReplaceFilter.java | 10 +++--- .../jinjava/lib/filter/ReverseFilter.java | 2 +- .../jinjava/lib/filter/RootFilter.java | 5 +-- .../jinjava/lib/filter/RoundFilter.java | 7 ++-- .../jinjava/lib/filter/SafeFilter.java | 2 +- .../jinjava/lib/filter/ShuffleFilter.java | 2 +- .../jinjava/lib/filter/SliceFilter.java | 6 ++-- .../jinjava/lib/filter/SortFilter.java | 9 ++--- .../jinjava/lib/filter/SplitFilter.java | 6 ++-- .../jinjava/lib/filter/StringFilter.java | 2 +- .../lib/filter/StringToTimeFilter.java | 6 ++-- .../jinjava/lib/filter/StripTagsFilter.java | 2 +- .../jinjava/lib/filter/TitleFilter.java | 2 +- .../jinjava/lib/filter/ToJsonFilter.java | 2 +- .../jinjava/lib/filter/TrimFilter.java | 2 +- .../jinjava/lib/filter/TruncateFilter.java | 2 +- .../lib/filter/TruncateHtmlFilter.java | 4 +-- .../jinjava/lib/filter/UniqueFilter.java | 5 +-- .../lib/filter/UnixTimestampFilter.java | 2 +- .../jinjava/lib/filter/UpperFilter.java | 2 +- .../jinjava/lib/filter/UrlEncodeFilter.java | 4 +-- .../jinjava/lib/filter/UrlizeFilter.java | 8 ++--- .../jinjava/lib/filter/WordCountFilter.java | 2 +- .../jinjava/lib/filter/WordWrapFilter.java | 6 ++-- .../jinjava/lib/filter/XmlAttrFilter.java | 4 +-- .../lib/filter/AdvancedFilterTest.java | 1 - .../jinjava/lib/tag/ValidationModeTest.java | 2 +- 69 files changed, 174 insertions(+), 132 deletions(-) diff --git a/benchmark/src/main/java/com/hubspot/jinjava/benchmarks/liquid/Filters.java b/benchmark/src/main/java/com/hubspot/jinjava/benchmarks/liquid/Filters.java index 492543196..790351838 100644 --- a/benchmark/src/main/java/com/hubspot/jinjava/benchmarks/liquid/Filters.java +++ b/benchmark/src/main/java/com/hubspot/jinjava/benchmarks/liquid/Filters.java @@ -33,7 +33,7 @@ public class Filters { */ public static class OverrideDateFilter extends DatetimeFilter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { return Functions.dateTimeFormat(ZonedDateTime.now(), arg); } } @@ -45,7 +45,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return null; } } @@ -57,7 +57,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if(var == null) { return ""; } @@ -73,7 +73,7 @@ public String getName() { return "money_with_currency"; } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { Object val = super.filter(var, interpreter, args); if(val.toString().length() == 0) { return ""; @@ -89,7 +89,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { double grams = toDouble(Objects.toString(var)); return String.format("%.2f", grams / 1000); } @@ -102,7 +102,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return super.filter(var, interpreter, args) + " kg"; } } @@ -114,7 +114,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return "/files/1/[shop_id]/[shop_id]/assets/" + var; } } @@ -126,7 +126,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return "/global/" + var; } } @@ -138,7 +138,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return "/shopify/" + var; } } @@ -150,7 +150,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return String.format("", var); } } @@ -162,7 +162,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String media = "all"; if(args.length > 0) { media = args[0]; @@ -178,7 +178,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String url = args[0]; String title = ""; if(args.length > 1) { @@ -195,7 +195,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String alt = ""; if(args.length > 0) { alt = args[0]; @@ -212,7 +212,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String label = Objects.toString(var); String tag = args[0]; @@ -227,7 +227,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String tag = Objects.toString(var); String cssClass = "active"; if(args.length > 0) { @@ -251,7 +251,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String label = Objects.toString(var); String tag = args[0]; @@ -270,7 +270,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String label = Objects.toString(var); String tag = args[0]; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java index 9643b4613..3c42fb562 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java @@ -36,7 +36,7 @@ public class AbsFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object == null) { return null; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java index 57379d0e5..c2e3e38e6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java @@ -16,6 +16,7 @@ package com.hubspot.jinjava.lib.filter; import java.math.BigDecimal; +import java.util.Objects; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -40,7 +41,7 @@ public class AddFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) { if (object == null) { return null; @@ -63,7 +64,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar BigDecimal addend; try { - addend = new BigDecimal(args[0]); + addend = new BigDecimal(Objects.toString(args[0])); } catch (NumberFormatException e) { throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0]); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/AdvancedFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/AdvancedFilter.java index d6079eb3f..973966140 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AdvancedFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AdvancedFilter.java @@ -39,7 +39,7 @@ public interface AdvancedFilter extends Importable, Filter { Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs); // Default implementation to maintain backward-compatibility with old Filters - default Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + default Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return filter(var, interpreter, (Object[]) args, new HashMap<>()); } } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java index 0673400ce..536b100a3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java @@ -1,5 +1,7 @@ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; @@ -25,13 +27,13 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attribute name to use)"); } - return interpreter.resolveProperty(var, args[0]); + return interpreter.resolveProperty(var, Objects.toString(args[0])); } } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java index 61c4dfc2f..e7c499c2c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import org.apache.commons.lang3.math.NumberUtils; @@ -53,12 +54,12 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null || args.length == 0) { return Collections.emptyList(); } - int lineCount = NumberUtils.toInt(args[0], 0); + int lineCount = NumberUtils.toInt(Objects.toString(args[0]), 0); if (lineCount == 0) { return Collections.emptyList(); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java index f7716b085..669d58e06 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java @@ -39,7 +39,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, - String... args) { + Object... args) { if (var == null) { return false; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java index aa2cde927..7b1918e80 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java @@ -23,7 +23,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null) { return null; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java index 8dbeeb5dd..4752eb59b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java @@ -1,5 +1,7 @@ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -30,7 +32,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null) { return null; @@ -38,7 +40,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) int size = 80; if (args.length > 0) { - size = NumberUtils.toInt(args[0], 80); + size = NumberUtils.toInt(Objects.toString(args[0]), 80); } return StringUtils.center(var.toString(), size); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java index 78ac7985e..7975f3eb9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java @@ -39,12 +39,12 @@ public class CutFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (string to remove from target)"); } - String cutee = arg[0]; + String cutee = Objects.toString(arg[0]); String origin = Objects.toString(object, ""); return StringUtils.replace(origin, cutee, ""); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java index ea531f803..715f9e169 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java @@ -1,5 +1,9 @@ package com.hubspot.jinjava.lib.filter; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; @@ -27,10 +31,19 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, - String... args) { + Object... args) { + List stringArgs = new ArrayList<>(); + for(Object arg: args) { + stringArgs.add(Objects.toString(arg)); + } + String[] filterArgs = new String[stringArgs.size()]; + for(int i=0; i < filterArgs.length; i++) { + filterArgs[i] = stringArgs.get(i); + } + if (args.length > 0) { - return Functions.dateTimeFormat(var, args); + return Functions.dateTimeFormat(var, filterArgs); } else { return Functions.dateTimeFormat(var); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DatetimeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DatetimeFilter.java index 8e0d61305..ef7f0f5a8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DatetimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DatetimeFilter.java @@ -22,7 +22,7 @@ public class DatetimeFilter extends DateTimeFormatFilter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { return super.filter(object, interpreter, arg); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java index a41877b84..2726fee32 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java @@ -15,6 +15,8 @@ **********************************************************************/ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import org.apache.commons.lang3.BooleanUtils; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; @@ -42,7 +44,7 @@ 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) { @@ -50,7 +52,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } if (args.length > 1) { - truthy = BooleanUtils.toBoolean(args[1]); + truthy = BooleanUtils.toBoolean(Objects.toString(args[1])); } if (truthy) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java index 9deea24e9..b14ecd578 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java @@ -4,6 +4,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Map.Entry; import org.apache.commons.lang3.BooleanUtils; @@ -36,19 +37,19 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null || !Map.class.isAssignableFrom(var.getClass())) { return var; } boolean caseSensitive = false; if (args.length > 0) { - caseSensitive = BooleanUtils.toBoolean(args[0]); + caseSensitive = BooleanUtils.toBoolean(Objects.toString(args[0])); } boolean sortByKey = true; if (args.length > 1) { - sortByKey = "value".equalsIgnoreCase(args[1]); + sortByKey = "value".equalsIgnoreCase(Objects.toString(args[1])); } @SuppressWarnings("unchecked") diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java index 04c8e46c3..9158fcca6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java @@ -17,6 +17,7 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Objects; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -41,11 +42,11 @@ public class DivideFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (divisor) argument"); } - String toMul = arg[0]; + String toMul = arg[0] == null ? null : Objects.toString(arg[0]); Number num; if (toMul != null) { try { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java index 6ef1c0400..918029559 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java @@ -15,6 +15,8 @@ **********************************************************************/ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; @@ -38,7 +40,7 @@ public class DivisibleFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object == null) { return false; } @@ -46,7 +48,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to divide by)"); } - long factor = Long.parseLong(arg[0]); + long factor = Long.parseLong(Objects.toString(arg[0])); long value = ((Number) object).longValue(); if (value % factor == 0) { return true; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java index 198514ee1..539ad74e9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java @@ -57,7 +57,7 @@ public static String escapeHtmlEntities(String input) { } @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { return escapeHtmlEntities(Objects.toString(object, "")); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java index d8bb71b02..bfa0ad3f9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java @@ -55,7 +55,7 @@ public static String escapeJinjavaEntities(String input) { } @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { return escapeJinjavaEntities(Objects.toString(object, "")); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java index 6e386aedf..04976ea14 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java @@ -35,7 +35,7 @@ public class EscapeJsFilter implements Filter { @Override - public Object filter(Object objectToFilter, JinjavaInterpreter jinjavaInterpreter, String... strings) { + public Object filter(Object objectToFilter, JinjavaInterpreter jinjavaInterpreter, Object... strings) { String input = Objects.toString(objectToFilter, ""); LengthLimitingStringBuilder builder = new LengthLimitingStringBuilder(jinjavaInterpreter.getConfig().getMaxOutputSize()); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java index f0fd72997..ab1f0503c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java @@ -26,7 +26,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return StringEscapeUtils.escapeJson(Objects.toString(var)); } } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java index ef414a18f..388214181 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java @@ -29,7 +29,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { float bytes = NumberUtils.toFloat(Objects.toString(var), 0.0f); if (bytes == 1) { @@ -38,7 +38,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) boolean binary = false; if (args.length > 0) { - binary = BooleanUtils.toBoolean(args[0]); + binary = BooleanUtils.toBoolean(Objects.toString(args[0])); } int base = binary ? 1024 : 1000; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java b/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java index 4be823fbd..1eadb78d8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java @@ -38,7 +38,7 @@ public interface Filter extends Importable { * any arguments passed to this filter invocation * @return the filtered form of the given variable */ - Object filter(Object var, JinjavaInterpreter interpreter, String... args); + Object filter(Object var, JinjavaInterpreter interpreter, Object... args); /* * The JinJava parser calls filters giving to them two lists of parameters: diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java index 247faf1ee..1d0c14376 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java @@ -23,7 +23,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { ForLoop loop = ObjectIterator.getLoop(var); return loop.next(); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java index f6bc10209..9d2dfdb70 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java @@ -3,6 +3,7 @@ import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; +import java.util.Objects; import org.apache.commons.lang3.math.NumberUtils; @@ -31,10 +32,10 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { float defaultVal = 0; if (args.length > 0) { - defaultVal = NumberUtils.toFloat(args[0], 0.0f); + defaultVal = NumberUtils.toFloat(Objects.toString(args[0]), 0.0f); } if (var == null) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java index ca1bbe351..f18eaa1e4 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java @@ -25,7 +25,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return StringEscapeUtils.escapeHtml4(Objects.toString(var, "")); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java index 25c4f5f6d..2bcffa07f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java @@ -24,7 +24,7 @@ public class FromJsonFilter implements Filter { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null) { return null; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java index 58f85fd74..8697f0d64 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java @@ -43,13 +43,13 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attr name to group by)"); } - String attr = args[0]; + String attr = Objects.toString(args[0]); ForLoop loop = ObjectIterator.getLoop(var); Multimap groupBuckets = LinkedListMultimap.create(); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java index 7892ff18e..035a4a318 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java @@ -37,15 +37,15 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { int width = 4; if (args.length > 0) { - width = NumberUtils.toInt(args[0], 4); + width = NumberUtils.toInt(Objects.toString(args[0]), 4); } boolean indentFirst = false; if (args.length > 1) { - indentFirst = BooleanUtils.toBoolean(args[1]); + indentFirst = BooleanUtils.toBoolean(Objects.toString(args[1])); } List indentedLines = new ArrayList<>(); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java index a60b19802..dcd812e69 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java @@ -3,6 +3,7 @@ import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; +import java.util.Objects; import org.apache.commons.lang3.math.NumberUtils; @@ -35,11 +36,11 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, - String... args) { + Object... args) { Long defaultVal = 0L; if (args.length > 0) { - defaultVal = NumberUtils.toLong(args[0], 0); + defaultVal = NumberUtils.toLong(Objects.toString(args[0]), 0); } if (var == null) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java index 9c0ef1f86..e2edeae18 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -1,6 +1,7 @@ package com.hubspot.jinjava.lib.filter; import java.util.List; +import java.util.Objects; import java.util.regex.Pattern; import org.apache.commons.net.util.SubnetUtils; @@ -46,14 +47,14 @@ public class IpAddrFilter implements Filter { BROADCAST_STRING); @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) { if (object == null) { return false; } if (args.length > 0) { - String function = args[0].trim(); + String function = Objects.toString(args[0]).trim(); return getFunctionValue(interpreter, function.toLowerCase(), object); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java index d2a8c93f5..3ec6d783e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java @@ -41,18 +41,18 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { LengthLimitingStringBuilder stringBuilder = new LengthLimitingStringBuilder(interpreter.getConfig().getMaxStringLength()); String separator = ""; if (args.length > 0) { - separator = args[0]; + separator = Objects.toString(args[0]); } String attr = null; if (args.length > 1) { - attr = args[1]; + attr = Objects.toString(args[1]); } ForLoop loop = ObjectIterator.getLoop(var); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java index 01a3ec215..261d37720 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java @@ -23,7 +23,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { ForLoop loop = ObjectIterator.getLoop(var); Object last = null; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java index f020ef9ac..8d3f78165 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java @@ -36,7 +36,7 @@ public class LengthFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (null == object) { return 0; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java index 3983a6249..9f22480da 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java @@ -29,7 +29,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { List result; if (var instanceof String) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/LogFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/LogFilter.java index a87f64473..74c6fc456 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LogFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LogFilter.java @@ -4,6 +4,7 @@ import java.math.BigInteger; import java.math.MathContext; import java.math.RoundingMode; +import java.util.Objects; import com.google.common.primitives.Doubles; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; @@ -29,12 +30,12 @@ public class LogFilter implements Filter { private static final MathContext PRECISION = new MathContext(50); @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) { // default to e Double root = null; if (args.length > 0 && args[0] != null) { - Double tryRoot = Doubles.tryParse(args[0]); + Double tryRoot = Doubles.tryParse(Objects.toString(args[0])); if (tryRoot == null) { throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0]); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java index b75d7f0a6..635884135 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java @@ -29,7 +29,7 @@ public class LowerFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object instanceof String) { String value = (String) object; return value.toLowerCase(); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java b/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java index 820b7e97b..fd2a6319f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java @@ -69,7 +69,7 @@ private String md5(String str, Charset encoding) { } @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object instanceof String) { return md5((String) object, interpreter.getConfig().getCharset()); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java index 72777a7ac..5c0f60284 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java @@ -17,6 +17,7 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Objects; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -41,12 +42,12 @@ public class MultiplyFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to multiply by)"); } - String toMul = arg[0]; + String toMul = Objects.toString(arg[0]); Number num; try { num = new BigDecimal(toMul); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java index c14e65670..693acfe3f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java @@ -37,7 +37,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null) { return "null"; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java index 724b19a82..d3c83f73a 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java @@ -39,7 +39,7 @@ public class RandomFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object == null) { return null; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java index 7cda384f8..6b03a22de 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java @@ -1,5 +1,7 @@ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import com.google.re2j.Matcher; import com.google.re2j.Pattern; import com.google.re2j.PatternSyntaxException; @@ -35,7 +37,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, - String... args) { + Object... args) { if (args.length < 2) { throw new TemplateSyntaxException(interpreter, getName(), "requires 2 arguments (regex string, replacement string)"); @@ -47,8 +49,8 @@ public Object filter(Object var, JinjavaInterpreter interpreter, if (var instanceof String) { String s = (String) var; - String toReplace = args[0]; - String replaceWith = args[1]; + String toReplace = Objects.toString(args[0]); + String replaceWith = Objects.toString(args[1]); try { Pattern p = Pattern.compile(toReplace); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java index da29d9c9e..4fbe8bbff 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -33,7 +34,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { List result = new ArrayList<>(); if (args.length < 1) { @@ -44,7 +45,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 0); } - ExpTest expTest = interpreter.getContext().getExpTest(args[0]); + ExpTest expTest = interpreter.getContext().getExpTest(Objects.toString(args[0])); if (expTest == null) { throw new InvalidArgumentException(interpreter, this, InvalidReason.EXPRESSION_TEST, 0, args[0]); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java index bd161448f..690c8e7ad 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java @@ -1,5 +1,7 @@ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -36,7 +38,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, - String... args) { + Object... args) { if (var == null) { return null; @@ -48,12 +50,12 @@ public Object filter(Object var, JinjavaInterpreter interpreter, } String s = (String) var; - String toReplace = args[0]; - String replaceWith = args[1]; + String toReplace = Objects.toString(args[0]); + String replaceWith = Objects.toString(args[1]); Integer count = null; if (args.length > 2) { - count = NumberUtils.createInteger(args[2]); + count = NumberUtils.createInteger(Objects.toString(args[2])); } if (count == null) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java index cc01c40d5..be296ddae 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java @@ -36,7 +36,7 @@ public class ReverseFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object == null) { return null; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RootFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RootFilter.java index c03255496..866f2ec97 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RootFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RootFilter.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; +import java.util.Objects; import com.google.common.primitives.Doubles; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; @@ -28,11 +29,11 @@ public class RootFilter implements Filter { private static final MathContext PRECISION = new MathContext(50); @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) { double root = 2; if (args.length > 0 && args[0] != null) { - Double tryRoot = Doubles.tryParse(args[0]); + Double tryRoot = Doubles.tryParse(Objects.toString(args[0])); if (tryRoot == null) { throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0]); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java index 31f9f1508..6dc7bfc29 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.Objects; import org.apache.commons.lang3.math.NumberUtils; @@ -32,7 +33,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null) { return null; @@ -47,12 +48,12 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) int precision = 0; if (args.length > 0) { - precision = NumberUtils.toInt(args[0]); + precision = NumberUtils.toInt(Objects.toString(args[0])); } String method = "common"; if (args.length > 1) { - method = args[1]; + method = Objects.toString(args[1]); } RoundingMode roundingMode; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java index 112be2a96..115eed7be 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java @@ -26,7 +26,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, - String... args) { + Object... args) { return var; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java index 93b2650e5..0951edca0 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java @@ -29,7 +29,7 @@ public String getName() { @SuppressWarnings("unchecked") @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var instanceof Collection) { List list = new ArrayList<>((Collection) var); Collections.shuffle(list, interpreter.getRandom()); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java index 3b36e72cf..636394c0e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java @@ -1,5 +1,7 @@ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import org.apache.commons.lang3.math.NumberUtils; import com.google.common.collect.Iterators; @@ -39,14 +41,14 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { ForLoop loop = ObjectIterator.getLoop(var); if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number of slices)"); } - int slices = NumberUtils.toInt(args[0], 3); + int slices = NumberUtils.toInt(Objects.toString(args[0]), 3); return Iterators.paddedPartition(loop, slices); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java index 45caa845c..66ec3a2c9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.apache.commons.lang3.BooleanUtils; @@ -51,19 +52,19 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null) { return null; } - boolean reverse = args.length > 0 && BooleanUtils.toBoolean(args[0]); - boolean caseSensitive = args.length > 1 && BooleanUtils.toBoolean(args[1]); + boolean reverse = args.length > 0 && BooleanUtils.toBoolean(Objects.toString(args[0])); + boolean caseSensitive = args.length > 1 && BooleanUtils.toBoolean(Objects.toString(args[1])); if (args.length > 2 && args[2] == null) { throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 2); } - List attr = args.length > 2 ? DOT_SPLITTER.splitToList(args[2]) : Collections.emptyList(); + List attr = args.length > 2 ? DOT_SPLITTER.splitToList(Objects.toString(args[2])) : Collections.emptyList(); return Lists.newArrayList(ObjectIterator.getLoop(var)).stream() .sorted(Comparator.comparing((o) -> mapObject(interpreter, o, attr), new ObjectComparator(reverse, caseSensitive))) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java index bfea57b83..9a87d3dab 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java @@ -46,17 +46,17 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { Splitter splitter; if (args.length > 0) { - splitter = Splitter.on(args[0]); + splitter = Splitter.on(Objects.toString(args[0])); } else { splitter = Splitter.on(CharMatcher.whitespace()); } if (args.length > 1) { - int limit = NumberUtils.toInt(args[1], 0); + int limit = NumberUtils.toInt(Objects.toString(args[1]), 0); if (limit > 0) { splitter = splitter.limit(limit); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java index 3abe18c45..e7f951f56 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java @@ -23,7 +23,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return Objects.toString(var); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java index 7a63188f3..88e051802 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java @@ -1,5 +1,7 @@ package com.hubspot.jinjava.lib.filter; +import java.util.Objects; + import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; @@ -21,7 +23,7 @@ public class StringToTimeFilter implements Filter { @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (datetime format string)"); @@ -35,7 +37,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) throw new InvalidInputException(interpreter, this, InvalidReason.STRING); } - return Functions.stringToTime((String) var, args[0]); + return Functions.stringToTime((String) var, Objects.toString(args[0])); } @Override diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java index 09c2e09e7..10c82ceda 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java @@ -25,7 +25,7 @@ public class StripTagsFilter implements Filter { private static final Pattern WHITESPACE = Pattern.compile("\\s{2,}"); @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (!(object instanceof String)) { return object; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java index 93063ece8..838e36fb4 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java @@ -28,7 +28,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var instanceof String) { String value = (String) var; return WordUtils.capitalize(value); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java index 34c38c306..8da2da730 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java @@ -23,7 +23,7 @@ public class ToJsonFilter implements Filter { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { try { return OBJECT_MAPPER.writeValueAsString(var); } catch (JsonProcessingException e) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java index b7a74d6e2..92e729e70 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java @@ -27,7 +27,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return StringUtils.trim(Objects.toString(var)); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java index 85548bd42..f9fe21f55 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java @@ -43,7 +43,7 @@ public class TruncateFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { return Functions.truncate(object, (Object[]) arg); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java index 0a336d4ee..661cff025 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java @@ -40,7 +40,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var instanceof String) { int length = DEFAULT_TRUNCATE_LENGTH; String ends = DEFAULT_END; @@ -59,7 +59,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) boolean killwords = false; if (args.length > 2) { - killwords = BooleanUtils.toBoolean(args[2]); + killwords = BooleanUtils.toBoolean(Objects.toString(args[2])); } Document dom = Jsoup.parseBodyFragment((String) var); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java index 0b5741309..772ffb5e6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java @@ -2,6 +2,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -34,12 +35,12 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { Map result = new LinkedHashMap<>(); String attr = null; if (args.length > 0) { - attr = args[0]; + attr = Objects.toString(args[0]); } ForLoop loop = ObjectIterator.getLoop(var); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java index 12dbf34fa..fdfff54c9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java @@ -20,7 +20,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { return Functions.unixtimestamp(var); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java index c53fd1b9e..1e482cd87 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java @@ -29,7 +29,7 @@ public class UpperFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) { if (object instanceof String) { String value = (String) object; return value.toUpperCase(); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java index 30eea05de..a93eb5bc7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java @@ -28,7 +28,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null && args.length == 0) { return ""; } @@ -55,7 +55,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) return urlEncode(var.toString()); } - return urlEncode(args[0]); + return urlEncode(Objects.toString(args[0])); } private String urlEncode(String s) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java index 7a50b1a5c..97337c3c5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java @@ -37,25 +37,25 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { Matcher m = URL_RE.matcher(Objects.toString(var, "")); StringBuffer result = new StringBuffer(); int trimUrlLimit = Integer.MAX_VALUE; if (args.length > 0) { - trimUrlLimit = NumberUtils.toInt(args[0], Integer.MAX_VALUE); + trimUrlLimit = NumberUtils.toInt(Objects.toString(args[0]), Integer.MAX_VALUE); } String fmt = " 1) { - nofollow = BooleanUtils.toBoolean(args[1]); + nofollow = BooleanUtils.toBoolean(Objects.toString(args[1])); } String target = ""; if (args.length > 2) { - target = args[2]; + target = Objects.toString(args[2]); } if (nofollow) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java index 18f2dd85b..1128a804b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java @@ -26,7 +26,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { Matcher matcher = WORD_RE.matcher(Objects.toString(var, "")); int count = 0; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java index 882355563..9e4bed199 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java @@ -33,17 +33,17 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { String str = Objects.toString(var, ""); int wrapLength = 79; if (args.length > 0) { - wrapLength = NumberUtils.toInt(args[0], 79); + wrapLength = NumberUtils.toInt(Objects.toString(args[0]), 79); } boolean wrapLongWords = true; if (args.length > 1) { - wrapLongWords = BooleanUtils.toBoolean(args[1]); + wrapLongWords = BooleanUtils.toBoolean(Objects.toString(args[1])); } return WordUtils.wrap(str, wrapLength, "\n", wrapLongWords); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java index 960fb150c..f4fc3a1f1 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java @@ -33,7 +33,7 @@ public String getName() { } @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { if (var == null || !Map.class.isAssignableFrom(var.getClass())) { return var; } @@ -51,7 +51,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) } String space = " "; - if (args.length > 0 && !BooleanUtils.toBoolean(args[0])) { + if (args.length > 0 && !BooleanUtils.toBoolean(Objects.toString(args[0]))) { space = ""; } diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/AdvancedFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/AdvancedFilterTest.java index 74d9fd78d..4b1ef37cd 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/AdvancedFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/AdvancedFilterTest.java @@ -53,7 +53,6 @@ public void itTestsNullKwargs() { }}; jinjava.getGlobalContext().registerFilter(new MyMirrorFilter(expectedArgs, expectedKwargs)); - assertThat(jinjava.render("{{ 'test'|divide(named1) }}", new HashMap<>())).isEqualTo("test"); } diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/ValidationModeTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/ValidationModeTest.java index 92b43ca69..e106bbc51 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/ValidationModeTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/ValidationModeTest.java @@ -40,7 +40,7 @@ class ValidationFilter implements Filter { private int executionCount = 0; @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { executionCount++; return var; } From 8487a16e6e88aad6405f46066e0d7dc31edfd759 Mon Sep 17 00:00:00 2001 From: Manish Devgan Date: Thu, 16 Jan 2020 03:09:30 +0530 Subject: [PATCH 2/5] removes undesired code from Filter.java --- .../jinjava/lib/filter/DefaultFilter.java | 3 +- .../hubspot/jinjava/lib/filter/Filter.java | 13 +----- .../jinjava/lib/filter/DefaultFilterTest.java | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java index 2726fee32..987e8e7a8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java @@ -24,6 +24,7 @@ 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( @@ -63,7 +64,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, Object... ar return object; } - return args[0]; + return (args[0] instanceof PyWrapper) ? args[0] : Objects.toString(args[0]); } @Override diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java b/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java index 1eadb78d8..4be4696e8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java @@ -52,17 +52,6 @@ default Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, // We append the named arguments at the end of the positional ones Object[] allArgs = ArrayUtils.addAll(args, kwargs.values().toArray()); - List stringArgs = new ArrayList<>(); - - for (Object arg : allArgs) { - stringArgs.add(arg == null ? null : Objects.toString(arg)); - } - - String[] filterArgs = new String[stringArgs.size()]; - for (int i = 0; i < stringArgs.size(); i++) { - filterArgs[i] = stringArgs.get(i); - } - - return filter(var, interpreter, filterArgs); + return filter(var, interpreter, allArgs); } } diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java new file mode 100644 index 000000000..568401d2f --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java @@ -0,0 +1,41 @@ +package com.hubspot.jinjava.lib.filter; + +import java.util.HashMap; + +import com.hubspot.jinjava.Jinjava; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Created by manishdevgan on 25/06/19. + */ + +public class DefaultFilterTest { + Jinjava jinjava; + + @Before + public void setup() { + jinjava = new Jinjava(); + jinjava.getGlobalContext().registerClasses(DefaultFilter.class); + } + + @Test + public void itSetsDefaultStringValues() { + assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ d }}", new HashMap<>())).isEqualTo("some random value"); + } + + @Test + public void itSetsDefaultObjectValue() { + assertThat(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Value = {{ d.key }}", new HashMap<>())).isEqualTo("Value = value"); + } + + @Test + public void itChecksForType() { + assertThat(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Type = {{ type(d.key) }}", new HashMap<>())) + .isEqualTo("Type = str"); + assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ type(d) }}", new HashMap<>())) + .isEqualTo("str"); + } +} \ No newline at end of file From d097b1785cedd47bbe08ba527ca9070f81bab263 Mon Sep 17 00:00:00 2001 From: Manish Devgan Date: Thu, 16 Jan 2020 03:43:58 +0530 Subject: [PATCH 3/5] removes whitespaces from DateTimeFormatFilter.java --- .../lib/filter/DateTimeFormatFilter.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java index 715f9e169..d31e0c0d5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java @@ -33,15 +33,15 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { - List stringArgs = new ArrayList<>(); - for(Object arg: args) { - stringArgs.add(Objects.toString(arg)); - } - String[] filterArgs = new String[stringArgs.size()]; - for(int i=0; i < filterArgs.length; i++) { - filterArgs[i] = stringArgs.get(i); - } - + List stringArgs = new ArrayList<>(); + for(Object arg: args) { + stringArgs.add(Objects.toString(arg)); + } + String[] filterArgs = new String[stringArgs.size()]; + for(int i=0; i < filterArgs.length; i++) { + filterArgs[i] = stringArgs.get(i); + } + if (args.length > 0) { return Functions.dateTimeFormat(var, filterArgs); } else { From 7abb0ccd3ca407d960236dcbc33e7df19a6dad06 Mon Sep 17 00:00:00 2001 From: Manish Devgan Date: Thu, 16 Jan 2020 04:00:25 +0530 Subject: [PATCH 4/5] Fixes Style Check Errors --- .../com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java | 4 ++-- src/main/java/com/hubspot/jinjava/lib/filter/Filter.java | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java index d31e0c0d5..68040d553 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java @@ -34,11 +34,11 @@ public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { List stringArgs = new ArrayList<>(); - for(Object arg: args) { + for (Object arg: args) { stringArgs.add(Objects.toString(arg)); } String[] filterArgs = new String[stringArgs.size()]; - for(int i=0; i < filterArgs.length; i++) { + for (int i = 0; i < filterArgs.length; i++) { filterArgs[i] = stringArgs.get(i); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java b/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java index 4be4696e8..331bad272 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/Filter.java @@ -15,10 +15,7 @@ **********************************************************************/ package com.hubspot.jinjava.lib.filter; -import java.util.ArrayList; -import java.util.List; import java.util.Map; -import java.util.Objects; import org.apache.commons.lang3.ArrayUtils; From ea624292f3949db242896cb722b4a784bef08e4f Mon Sep 17 00:00:00 2001 From: Manish Devgan Date: Fri, 17 Jan 2020 01:20:54 +0530 Subject: [PATCH 5/5] Removes stringArgs from DateTimeFormatFilter --- .../jinjava/lib/filter/DateTimeFormatFilter.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java index 68040d553..7dd3d8607 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java @@ -1,7 +1,5 @@ package com.hubspot.jinjava.lib.filter; -import java.util.ArrayList; -import java.util.List; import java.util.Objects; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; @@ -33,13 +31,9 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) { - List stringArgs = new ArrayList<>(); - for (Object arg: args) { - stringArgs.add(Objects.toString(arg)); - } - String[] filterArgs = new String[stringArgs.size()]; + String[] filterArgs = new String[args.length]; for (int i = 0; i < filterArgs.length; i++) { - filterArgs[i] = stringArgs.get(i); + filterArgs[i] = Objects.toString(args[i]); } if (args.length > 0) {