From 69284f5f873687cdcc447dfa5e81459fc67a6692 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Mon, 25 Feb 2019 16:51:06 -0500 Subject: [PATCH 1/6] initial overhaul. --- .../java/com/hubspot/jinjava/Jinjava.java | 6 ++- .../interpret/InvalidArgumentException.java | 32 +++++++++++++++ .../jinjava/interpret/TemplateError.java | 12 ++++++ .../interpret/TemplateSyntaxException.java | 8 ++++ .../hubspot/jinjava/lib/filter/AbsFilter.java | 39 +++++++++---------- .../hubspot/jinjava/lib/filter/AddFilter.java | 8 +++- 6 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java diff --git a/src/main/java/com/hubspot/jinjava/Jinjava.java b/src/main/java/com/hubspot/jinjava/Jinjava.java index e2b1d2d2b..10c35978e 100644 --- a/src/main/java/com/hubspot/jinjava/Jinjava.java +++ b/src/main/java/com/hubspot/jinjava/Jinjava.java @@ -29,6 +29,7 @@ import com.hubspot.jinjava.interpret.Context; import com.hubspot.jinjava.interpret.FatalTemplateErrorsException; import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.RenderResult; import com.hubspot.jinjava.interpret.TemplateError; @@ -203,7 +204,10 @@ public RenderResult renderForResult(String template, Map bindings, Ji return new RenderResult(TemplateError.fromException((TemplateSyntaxException) e), interpreter.getContext(), interpreter.getErrorsCopy()); } return new RenderResult(TemplateError.fromSyntaxError(e), interpreter.getContext(), interpreter.getErrorsCopy()); - } catch (Exception e) { + } catch (InvalidArgumentException e) { + return new RenderResult(TemplateError.fromInvalidArgumentException(e), interpreter.getContext(), interpreter.getErrorsCopy()); + } + catch (Exception e) { return new RenderResult(TemplateError.fromException(e), interpreter.getContext(), interpreter.getErrorsCopy()); } finally { globalContext.reset(); diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java new file mode 100644 index 000000000..fe8db3552 --- /dev/null +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java @@ -0,0 +1,32 @@ +package com.hubspot.jinjava.interpret; + +public class InvalidArgumentException extends RuntimeException { + + private final int lineNumber; + private final int startPosition; + private final String message; + private final String fieldName; + + public InvalidArgumentException(JinjavaInterpreter interpreter, String code, String message, String fieldName) { + this.fieldName = fieldName; + this.message = String.format("Invalid argument in %s: %s", code, message); + this.lineNumber = interpreter.getLineNumber(); + this.startPosition = interpreter.getPosition(); + } + + public String getFieldName() { + return fieldName; + } + + public String getMessage() { + return message; + } + + public int getLineNumber() { + return lineNumber; + } + + public int getStartPosition() { + return startPosition; + } +} diff --git a/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java b/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java index 13d6c0aee..2c3e606b0 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java +++ b/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java @@ -26,6 +26,7 @@ public enum ErrorReason { EXCEPTION, MISSING, DISABLED, + INVALID_ARGUMENT, OTHER } @@ -67,6 +68,17 @@ public static TemplateError fromException(TemplateSyntaxException ex) { return new TemplateError(ErrorType.FATAL, ErrorReason.SYNTAX_ERROR, ErrorItem.OTHER, ExceptionUtils.getMessage(ex), fieldName, ex.getLineNumber(), ex.getStartPosition(), ex); } + public static TemplateError fromInvalidArgumentException(InvalidArgumentException ex) { + return new TemplateError(ErrorType.FATAL, + ErrorReason.INVALID_ARGUMENT, + ErrorItem.PROPERTY, + ex.getMessage(), + ex.getFieldName(), + ex.getLineNumber(), + ex.getStartPosition(), + ex); + } + public static TemplateError fromException(Exception ex) { int lineNumber = -1; int startPosition = -1; diff --git a/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java b/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java index 0cacb05f3..77d7a3df1 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java @@ -5,24 +5,32 @@ public class TemplateSyntaxException extends InterpretException { private final String code; + @Deprecated public TemplateSyntaxException(String code, String message, int lineNumber, int startPosition) { super("Syntax error in '" + code + "': " + message, lineNumber, startPosition); this.code = code; } + @Deprecated public TemplateSyntaxException(String code, String message, int lineNumber) { this(code, message, lineNumber, -1); } + @Deprecated public TemplateSyntaxException(String code, String message, int lineNumber, int startPosition, Throwable t) { super(message, t, lineNumber, startPosition); this.code = code; } + @Deprecated public TemplateSyntaxException(String code, String message, int lineNumber, Throwable t) { this(code, message, lineNumber, -1, t); } + public TemplateSyntaxException(JinjavaInterpreter interpreter, String code, String message) { + this(code, message, interpreter.getLineNumber(), interpreter.getPosition()); + } + public String getCode() { return code; } 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 85ebcb358..241ec0b38 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java @@ -21,7 +21,7 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( @@ -38,38 +38,37 @@ public class AbsFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + + if (object == null) { + return null; + } + if (object instanceof Integer) { return Math.abs((Integer) object); - } - if (object instanceof Float) { + } else if (object instanceof Float) { return Math.abs((Float) object); - } - if (object instanceof Long) { + } else if (object instanceof Long) { return Math.abs((Long) object); - } - if (object instanceof Short) { + } else if (object instanceof Short) { return Math.abs((Short) object); - } - if (object instanceof Double) { + } else if (object instanceof Double) { return Math.abs((Double) object); - } - if (object instanceof BigDecimal) { + } else if (object instanceof BigDecimal) { return ((BigDecimal) object).abs(); - } - if (object instanceof BigInteger) { + } else if (object instanceof BigInteger) { return ((BigInteger) object).abs(); - } - if (object instanceof Byte) { + } else if (object instanceof Byte) { return Math.abs((Byte) object); - } - if (object instanceof String) { + } else { try { - return new BigDecimal((String) object).abs(); + return new BigDecimal(object.toString()).abs(); } catch (Exception e) { - throw new InterpretException(object + " can't be handled by abs filter", e); + throw new InvalidArgumentException(interpreter, + getName(), + String.format("Input %s must be a number", getName(), object.toString()), + "number"); } } - return object; } @Override 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 37dbee21c..b38c7539a 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java @@ -23,6 +23,7 @@ import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.interpret.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "adds a number to the existing value", @@ -39,8 +40,13 @@ public class AddFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + + if (object == null) { + return null; + } + if (arg.length != 1) { - throw new InterpretException("filter add expects 1 arg >>> " + arg.length); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument"); } try { From d0562703d7452a9850ce8a83b6f16f56192a0143 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Tue, 26 Feb 2019 17:28:24 -0500 Subject: [PATCH 2/6] more overhaul. --- .../jinjava/doc/annotations/JinjavaDoc.java | 2 + .../interpret/InvalidArgumentException.java | 41 ++++++++++++++---- .../interpret/InvalidInputException.java | 31 +++++++++++++ .../jinjava/interpret/InvalidReason.java | 23 ++++++++++ .../interpret/TemplateSyntaxException.java | 2 +- .../lib/exptest/IsDivisibleByExpTest.java | 18 ++++++-- .../jinjava/lib/exptest/IsEqualToExpTest.java | 4 +- .../jinjava/lib/exptest/IsEvenExpTest.java | 1 + .../jinjava/lib/exptest/IsSameAsExpTest.java | 5 ++- .../exptest/IsStringContainingExpTest.java | 6 ++- .../hubspot/jinjava/lib/filter/AbsFilter.java | 12 ++---- .../hubspot/jinjava/lib/filter/AddFilter.java | 30 +++++++++---- .../jinjava/lib/filter/AttrFilter.java | 9 ++-- .../jinjava/lib/filter/BaseDateFilter.java | 43 ++++++++++++------- .../jinjava/lib/filter/BatchFilter.java | 2 +- .../lib/filter/BetweenTimesFilter.java | 18 ++++---- .../jinjava/lib/filter/BoolFilter.java | 7 ++- .../jinjava/lib/filter/CapitalizeFilter.java | 9 ++-- .../jinjava/lib/filter/CenterFilter.java | 11 ++--- .../hubspot/jinjava/lib/filter/CutFilter.java | 7 +-- .../lib/filter/DateTimeFormatFilter.java | 2 +- .../jinjava/lib/filter/DefaultFilter.java | 16 +++---- .../jinjava/lib/filter/DictSortFilter.java | 2 +- .../jinjava/lib/filter/DifferenceFilter.java | 2 +- .../jinjava/lib/filter/DivideFilter.java | 19 +++++--- .../jinjava/lib/filter/DivisibleFilter.java | 6 +-- .../jinjava/lib/filter/EscapeFilter.java | 4 +- .../lib/filter/EscapeJinjavaFilter.java | 4 +- .../jinjava/lib/filter/EscapeJsFilter.java | 4 +- .../jinjava/lib/filter/EscapeJsonFilter.java | 4 +- .../lib/filter/FileSizeFormatFilter.java | 2 +- .../jinjava/lib/filter/FirstFilter.java | 4 +- .../jinjava/lib/filter/FloatFilter.java | 2 +- .../jinjava/lib/filter/ForceEscapeFilter.java | 2 +- .../jinjava/lib/filter/FormatFilter.java | 2 +- .../jinjava/lib/filter/FromJsonFilter.java | 17 +++++--- .../jinjava/lib/filter/GroupByFilter.java | 9 ++-- .../jinjava/lib/filter/IndentFilter.java | 2 +- .../hubspot/jinjava/lib/filter/IntFilter.java | 3 +- .../jinjava/lib/filter/IntersectFilter.java | 2 +- .../jinjava/lib/filter/IpAddrFilter.java | 2 +- .../jinjava/lib/filter/JoinFilter.java | 2 +- .../jinjava/lib/filter/LastFilter.java | 4 +- .../jinjava/lib/filter/LengthFilter.java | 2 +- .../jinjava/lib/filter/ListFilter.java | 2 +- .../jinjava/lib/filter/LowerFilter.java | 2 +- .../hubspot/jinjava/lib/filter/MapFilter.java | 6 +-- .../hubspot/jinjava/lib/filter/Md5Filter.java | 2 +- .../jinjava/lib/filter/MinusTimeFilter.java | 10 ++--- .../jinjava/lib/filter/MultiplyFilter.java | 21 ++++++--- .../jinjava/lib/filter/PlusTimeFilter.java | 10 ++--- .../jinjava/lib/filter/PrettyPrintFilter.java | 2 +- .../jinjava/lib/filter/RandomFilter.java | 2 +- .../lib/filter/RegexReplaceFilter.java | 22 ++++++---- .../jinjava/lib/filter/RejectAttrFilter.java | 2 +- .../jinjava/lib/filter/RejectFilter.java | 16 ++++--- .../jinjava/lib/filter/ReplaceFilter.java | 10 +++-- .../jinjava/lib/filter/ReverseFilter.java | 6 +-- .../jinjava/lib/filter/RoundFilter.java | 12 +++++- .../jinjava/lib/filter/SafeFilter.java | 2 + .../jinjava/lib/filter/SelectAttrFilter.java | 25 +++++------ .../jinjava/lib/filter/SelectFilter.java | 9 ++-- .../jinjava/lib/filter/ShuffleFilter.java | 2 + .../jinjava/lib/filter/SliceFilter.java | 7 ++- .../jinjava/lib/filter/SortFilter.java | 4 +- .../jinjava/lib/filter/SplitFilter.java | 2 +- .../jinjava/lib/filter/StringFilter.java | 2 + .../lib/filter/StringToTimeFilter.java | 18 +++++--- .../jinjava/lib/filter/StripTagsFilter.java | 3 ++ .../hubspot/jinjava/lib/filter/SumFilter.java | 2 +- .../lib/filter/SymmetricDifferenceFilter.java | 2 +- .../jinjava/lib/filter/TitleFilter.java | 2 + .../jinjava/lib/filter/ToJsonFilter.java | 9 ++-- .../jinjava/lib/filter/TrimFilter.java | 2 + .../jinjava/lib/filter/TruncateFilter.java | 2 +- .../lib/filter/TruncateHtmlFilter.java | 15 ++++--- .../jinjava/lib/filter/UnionFilter.java | 2 +- .../jinjava/lib/filter/UniqueFilter.java | 2 +- .../lib/filter/UnixTimestampFilter.java | 4 +- .../jinjava/lib/filter/UpperFilter.java | 2 + .../jinjava/lib/filter/UrlEncodeFilter.java | 2 + .../jinjava/lib/filter/UrlizeFilter.java | 2 +- .../jinjava/lib/filter/WordCountFilter.java | 2 + .../jinjava/lib/filter/WordWrapFilter.java | 2 +- .../jinjava/lib/filter/XmlAttrFilter.java | 2 +- .../lib/filter/FromJsonFilterTest.java | 6 +-- .../lib/filter/RegexReplaceFilterTest.java | 3 +- 87 files changed, 419 insertions(+), 242 deletions(-) create mode 100644 src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java create mode 100644 src/main/java/com/hubspot/jinjava/interpret/InvalidReason.java diff --git a/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java b/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java index e9b56a78f..e0efa143e 100644 --- a/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java +++ b/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java @@ -11,6 +11,8 @@ String value() default ""; + JinjavaParam input(); + JinjavaParam[] params() default {}; JinjavaSnippet[] snippets() default {}; diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java index fe8db3552..0a83bcd81 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java @@ -1,23 +1,22 @@ package com.hubspot.jinjava.interpret; +import com.hubspot.jinjava.lib.Importable; + public class InvalidArgumentException extends RuntimeException { private final int lineNumber; private final int startPosition; private final String message; - private final String fieldName; - public InvalidArgumentException(JinjavaInterpreter interpreter, String code, String message, String fieldName) { - this.fieldName = fieldName; - this.message = String.format("Invalid argument in %s: %s", code, message); + public InvalidArgumentException(JinjavaInterpreter interpreter, Importable importable, InvalidReason invalidReason, int argumentNumber, Object... errorMessageArgs) { + this.message = String.format("Invalid argument in '%s': %s", + importable.getName(), + String.format("%s %s", formatArgumentNumber(argumentNumber + 1), String.format(invalidReason.getErrorMessage(), errorMessageArgs))); + this.lineNumber = interpreter.getLineNumber(); this.startPosition = interpreter.getPosition(); } - public String getFieldName() { - return fieldName; - } - public String getMessage() { return message; } @@ -29,4 +28,30 @@ public int getLineNumber() { public int getStartPosition() { return startPosition; } + + private static String formatArgumentNumber(int argumentNumber) { + switch (argumentNumber){ + case 1: + return "1st"; + case 2: + return "2nd"; + case 3: + return "3rd"; + case 4: + return "4th"; + case 5: + return "5th"; + case 6: + return "6th"; + case 7: + return "7th"; + case 8: + return "8th"; + case 9: + return "9th"; + default: + return String.valueOf(argumentNumber); + + } + } } diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java new file mode 100644 index 000000000..f2072756d --- /dev/null +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java @@ -0,0 +1,31 @@ +package com.hubspot.jinjava.interpret; + +import com.hubspot.jinjava.lib.Importable; + +public class InvalidInputException extends RuntimeException { + + private final int lineNumber; + private final int startPosition; + private final String message; + + public InvalidInputException(JinjavaInterpreter interpreter, Importable importable, InvalidReason invalidReason, Object... errorMessageArgs) { + this.message = String.format("Invalid input in '%s': input variable %s", + importable.getName(), + String.format(invalidReason.getErrorMessage(), errorMessageArgs)); + + this.lineNumber = interpreter.getLineNumber(); + this.startPosition = interpreter.getPosition(); + } + + public String getMessage() { + return message; + } + + public int getLineNumber() { + return lineNumber; + } + + public int getStartPosition() { + return startPosition; + } +} diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidReason.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidReason.java new file mode 100644 index 000000000..18b258a09 --- /dev/null +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidReason.java @@ -0,0 +1,23 @@ +package com.hubspot.jinjava.interpret; + +public enum InvalidReason { + NUMBER_FORMAT("with value '%s' must be a number"), + NULL("cannot be null"), + STRING("must be a string"), + EXPRESSION_TEST("with value %s must be the name of an expression test"), + TEMPORAL_UNIT("with value %s must be a valid temporal unit"), + JSON_READ("could not be converted to an object"), + JSON_WRITE("object could not be written as a string"), + REGEX("with value %s must be valid regex") + ; + + private final String errorMessage; + + InvalidReason(String errorMessage) { + this.errorMessage = errorMessage; + } + + public String getErrorMessage() { + return errorMessage; + } +} diff --git a/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java b/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java index 77d7a3df1..b59b1af60 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/TemplateSyntaxException.java @@ -7,7 +7,7 @@ public class TemplateSyntaxException extends InterpretException { @Deprecated public TemplateSyntaxException(String code, String message, int lineNumber, int startPosition) { - super("Syntax error in '" + code + "': " + message, lineNumber, startPosition); + super(String.format("Syntax error in '%s': %s", code, message), lineNumber, startPosition); this.code = code; } diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java index b42fa4fbb..db64ab9a7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java @@ -3,8 +3,9 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc(value = "Check if a variable is divisible by a number", params = { @@ -34,8 +35,19 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar return false; } - if (args.length == 0 || args[0] == null || !Number.class.isAssignableFrom(args[0].getClass())) { - throw new InterpretException(getName() + " test requires a numeric argument"); + if (args.length == 0) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (name of expression test to filter by)"); + } + + if (args[0] == null) { + return false; + } + + if (!Number.class.isAssignableFrom(args[0].getClass())) { + throw new InvalidArgumentException(interpreter, + getName(), + String.format(InvalidArgumentException.NUMBER_FORMAT_EXCEPTION_TEMPLATE, "num", args[0].toString()), + "num"); } return ((Number) var).intValue() % ((Number) args[0]).intValue() == 0; diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java index fa81ffe49..8ec638248 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java @@ -4,8 +4,8 @@ import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.el.TruthyTypeConverter; -import com.hubspot.jinjava.interpret.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import de.odysseus.el.misc.BooleanOperations; import de.odysseus.el.misc.TypeConverter; @@ -36,7 +36,7 @@ public String getName() { @Override public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { if (args.length == 0) { - throw new InterpretException(getName() + " test requires 1 argument"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (other object to check equality against)"); } return BooleanOperations.eq(TYPE_CONVERTER, var, args[0]); diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java index 3ddebcd4e..7074fecfa 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java @@ -24,6 +24,7 @@ public String getName() { @Override public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { + if (var == null || !Number.class.isAssignableFrom(var.getClass())) { return false; } diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java index 4481ff04b..b20cf3843 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java @@ -3,8 +3,8 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc(value = "Return true if variable is pointing at same object as other variable", params = @JinjavaParam(value = "other", type = "object", desc = "A second object to check the variables value against"), @@ -24,8 +24,9 @@ public String getName() { @Override public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { + if (args.length == 0) { - throw new InterpretException(getName() + " test requires 1 argument"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (other object to check the variables value against)"); } return var == args[0]; diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java index f6fbf76a4..85e24e6c7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java @@ -1,12 +1,14 @@ package com.hubspot.jinjava.lib.exptest; 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Return true if object is a string which contains a specified other string", + params = @JinjavaParam(value = "string", type = "string", desc = "A second string to check is contained in the first string"), snippets = { @JinjavaSnippet( code = "{% if variable is string_containing 'foo' %}\n" + @@ -27,7 +29,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar } if (args.length == 0) { - throw new InterpretException(getName() + " test requires an argument"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (a second string)"); } if (args[0] == null) { 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 241ec0b38..c29cc53fb 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java @@ -21,14 +21,13 @@ 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.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( value = "Return the absolute value of the argument.", - params = { - @JinjavaParam(value = "number", type = "number", desc = "The number that you want to get the absolute value of") - }, + input = @JinjavaParam(value = "number", type = "number", desc = "The number that you want to get the absolute value of"), snippets = { @JinjavaSnippet( code = "{% set my_number = -53 %}\n" + @@ -63,10 +62,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar try { return new BigDecimal(object.toString()).abs(); } catch (Exception e) { - throw new InvalidArgumentException(interpreter, - getName(), - String.format("Input %s must be a number", getName(), object.toString()), - "number"); + throw new InvalidInputException(interpreter, this, InvalidReason.NUMBER_FORMAT, object.toString()); } } } 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 b38c7539a..0e57657f6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java @@ -16,19 +16,20 @@ 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; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; -import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "adds a number to the existing value", + input = @JinjavaParam(value = "number", type = "number", desc = "Number or numeric variable to add to"), params = { - @JinjavaParam(value = "number", type = "number", desc = "Number or numeric variable to add to"), @JinjavaParam(value = "addend", type = "number", desc = "The number added to the base number") }, snippets = { @@ -46,17 +47,28 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } if (arg.length != 1) { - throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to add to base)"); } + BigDecimal base; try { - BigDecimal base = new BigDecimal(Objects.toString(object)); - BigDecimal addend = new BigDecimal(Objects.toString(arg[0])); + base = new BigDecimal(object.toString()); + } catch (NumberFormatException e) { + throw new InvalidInputException(interpreter, this, InvalidReason.NUMBER_FORMAT, object.toString()); + } - return base.add(addend); - } catch (Exception e) { - throw new InterpretException("filter add error", e); + if (arg[0] == null) { + return base; } + + BigDecimal addend; + try { + addend = new BigDecimal(arg[0]); + } catch (NumberFormatException e) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, arg[0]); + } + + return base.add(addend); } @Override 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 2e8fffa0a..9276dc270 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java @@ -3,13 +3,13 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Renders the attribute of a dictionary", + input = @JinjavaParam(value = "obj", desc = "The dictionary containing the attribute"), params = { - @JinjavaParam(value = "obj", desc = "The dictionary containing the attribute"), @JinjavaParam(value = "name", desc = "The dictionary attribute name to access") }, snippets = { @@ -26,8 +26,9 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (args.length == 0) { - throw new InterpretException(getName() + " requires an attr name to use", interpreter.getLineNumber()); + + if (args.length != 1) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attribute name to use)"); } return interpreter.resolveProperty(var, args[0]); diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java index 33ed9fc68..e3d8947f0 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java @@ -6,42 +6,53 @@ import java.util.stream.Collectors; import com.google.common.primitives.Longs; -import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidReason; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; -public abstract class BaseDateFilter implements Filter { +public abstract class BaseDateFilter implements AdvancedFilter { private static final Map unitMap = Arrays.stream(ChronoUnit.values()) .collect(Collectors.toMap(u -> u.toString().toLowerCase(), u -> u)); - protected long parseDiffAmount(String... args) { + protected long parseDiffAmount(JinjavaInterpreter interpreter, Object... args) { - if (args.length < 2) { - throw new InterpretException(String.format("%s filter requires a number and a string parameter", getName())); + if (args.length != 2) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (diff amount) and 1 string (diff unit) argument"); } - String firstArg = args[0]; - Long diff = Longs.tryParse(firstArg); + Object firstArg = args[0]; + if (firstArg == null) { + firstArg = 0; + } + + Long diff = Longs.tryParse(firstArg.toString()); if (diff == null) { - throw new InterpretException(String.format("%s filter requires a number parameter as first arg", getName())); + throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, firstArg.toString()); } return diff; } - protected ChronoUnit parseChronoUnit(String... args) { + protected ChronoUnit parseChronoUnit(JinjavaInterpreter interpreter, Object... args) { + + if (args.length != 2) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (diff amount) and 1 string (diff unit) argument"); + } - if (args.length < 2) { - throw new InterpretException(String.format("%s filter requires a number and a string parameter", getName())); + Object unitString = args[1]; + if (unitString == null) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 1); } - String secondArg = args[1].toLowerCase(); - return getTemporalUnit(secondArg); + return getTemporalUnit(interpreter, unitString.toString()); } - protected static ChronoUnit getTemporalUnit(String temporalUnit) { + protected ChronoUnit getTemporalUnit(JinjavaInterpreter interpreter, String temporalUnit) { String lowercase = temporalUnit.toLowerCase(); - if (!unitMap.containsKey(temporalUnit)) { - throw new InterpretException(String.format("%s is not a valid temporal unit", lowercase)); + if (!unitMap.containsKey(lowercase)) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.TEMPORAL_UNIT, 1, lowercase); } return unitMap.get(lowercase); } 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 b2b1a7ce1..13d118a58 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java @@ -15,8 +15,8 @@ @JinjavaDoc( value = "A filter that groups up items within a sequence", + input = @JinjavaParam(value = "value", desc = "The sequence or dict that the filter is applied to"), params = { - @JinjavaParam(value = "value", desc = "The sequence or dict that the filter is applied to"), @JinjavaParam(value = "linecount", type = "number", desc = "Number of items to include in the batch", defaultValue = "0"), @JinjavaParam(value = "fill_with", desc = "Value used to fill up missing items") }, diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java index 955fb1ae9..87f542a36 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java @@ -9,8 +9,10 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.lib.fn.Functions; import com.hubspot.jinjava.objects.date.PyishDate; @@ -19,32 +21,32 @@ */ @JinjavaDoc( value = "Calculates the time between two datetime objects", + input = @JinjavaParam(value = "begin", desc = "Datetime object or timestamp at the beginning of the period"), params = { - @JinjavaParam(value = "begin", desc = "Datetime object or timestamp at the beginning of the period"), @JinjavaParam(value = "end", desc = "Datetime object or timestamp at the end of the period"), @JinjavaParam(value = "unit", desc = "Which temporal unit to use"), }, snippets = { @JinjavaSnippet(code = "{% begin|between_times(end, 'hours') %}"), }) -public class BetweenTimesFilter implements AdvancedFilter { +public class BetweenTimesFilter extends BaseDateFilter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { - if (args.length < 2) { - throw new InterpretException(String.format("%s filter requires a datetime and a string parameter", getName())); + if (args.length != 2) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 datetime (end date) and 1 string (diff unit) argument"); } ZonedDateTime start = getZonedDateTime(var); ZonedDateTime end = getZonedDateTime(args[0]); Object args1 = args[1]; - if (!(args1 instanceof String)) { - throw new InterpretException(String.format("%s filter requires a string as the second parameter", getName())); + if (args1 == null) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 1); } - TemporalUnit temporalUnit = BaseDateFilter.getTemporalUnit((String) args[1]); + TemporalUnit temporalUnit = getTemporalUnit(interpreter, args[1].toString()); return temporalUnit.between(start, end); } 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 14a3adbdb..7fe44defa 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java @@ -13,20 +13,19 @@ */ package com.hubspot.jinjava.lib.filter; +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 org.apache.commons.lang3.BooleanUtils; /** * bool(value) Convert value to boolean. */ @JinjavaDoc( value = "Convert value into a boolean.", - params = { - @JinjavaParam(value = "value", desc = "The value to convert to a boolean"), - }, + input = @JinjavaParam(value = "value", desc = "The value to convert to a boolean"), snippets = { @JinjavaSnippet( desc = "This example converts a text string value to a boolean", 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 846c81899..a654f18c6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java @@ -9,9 +9,7 @@ @JinjavaDoc( value = "Capitalize a value. The first character will be uppercase, all others lowercase.", - params = { - @JinjavaParam(value = "string", desc = "String to capitalize the first letter of") - }, + input = @JinjavaParam(value = "string", desc = "String to capitalize the first letter of"), snippets = { @JinjavaSnippet( code = "{% set sentence = \"the first letter of a sentence should always be capitalized.\" %}\n" + @@ -26,6 +24,11 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + + if (var == null) { + return null; + } + if (var instanceof String) { String value = (String) var; return StringUtils.capitalize(value); 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 405171922..1fc336bb2 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java @@ -1,7 +1,5 @@ package com.hubspot.jinjava.lib.filter; -import java.util.Objects; - import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -12,8 +10,8 @@ @JinjavaDoc( value = "Uses whitespace to center the value in a field of a given width.", + input = @JinjavaParam(value = "value", desc = "Value to center"), params = { - @JinjavaParam(value = "value", desc = "Value to center"), @JinjavaParam(value = "width", type = "number", defaultValue = "80", desc = "Width of field to center value in") }, snippets = { @@ -33,14 +31,17 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - String str = Objects.toString(var, ""); + + if (var == null) { + return null; + } int size = 80; if (args.length > 0) { size = NumberUtils.toInt(args[0], 80); } - return StringUtils.center(str, size); + 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 109ccefa9..7ebe152e3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java @@ -22,13 +22,13 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Removes a string from the value from another string", + input = @JinjavaParam(value = "value", desc = "The original string"), params = { - @JinjavaParam(value = "value", desc = "The original string"), @JinjavaParam(value = "to_remove", desc = "String to remove from the original string") }, snippets = { @@ -40,8 +40,9 @@ public class CutFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + if (arg.length != 1) { - throw new InterpretException("filter cut expects 1 arg >>> " + arg.length); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (string to remove from target)"); } String cutee = arg[0]; String origin = Objects.toString(object, ""); 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 5bbb9d03e..86445b66a 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java @@ -9,8 +9,8 @@ @JinjavaDoc( value = "Formats a date object", + input = @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable or UNIX timestamp to format"), params = { - @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable or UNIX timestamp to format"), @JinjavaParam(value = "format", defaultValue = StrftimeFormatter.DEFAULT_DATE_FORMAT, desc = "The format of the date determined by the directives added to this parameter"), @JinjavaParam(value = "timezone", defaultValue = "utc", desc = "Time zone of output date") }, 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 2b8392409..f4aca6507 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java @@ -20,14 +20,14 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.util.ObjectTruthValue; @JinjavaDoc( value = "If the value is undefined it will return the passed default value, otherwise the value of the variable", + input = @JinjavaParam(value = "value", desc = "The variable or value to test"), params = { - @JinjavaParam(value = "value", desc = "The variable or value to test"), @JinjavaParam(value = "default_value", desc = "Value to print when variable is not defined"), @JinjavaParam(value = "boolean", type = "boolean", defaultValue = "False", desc = "Set to True to use with variables which evaluate to false") }, @@ -42,15 +42,15 @@ public class DefaultFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { boolean truthy = false; - if (arg.length == 0) { - throw new InterpretException("default filter requires 1 or 2 args"); + if (args.length != 1 && args.length != 2) { + throw new TemplateSyntaxException(interpreter, getName(), "requires either 1 (default value to use) or 2 (default value to use, ) arguments"); } - if (arg.length == 2) { - truthy = BooleanUtils.toBoolean(arg[1]); + if (args.length == 2) { + truthy = BooleanUtils.toBoolean(args[1]); } if (truthy) { @@ -62,7 +62,7 @@ else if (object != null) { return object; } - return arg[0]; + return args[0]; } @Override 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 c41fc7519..864fe003a 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java @@ -16,8 +16,8 @@ @JinjavaDoc( value = "Sort a dict and yield (key, value) pairs.", + input = @JinjavaParam(value = "value", desc = "Dict to sort"), params = { - @JinjavaParam(value = "value", desc = "Dict to sort"), @JinjavaParam(value = "case_sensitive", type = "boolean", defaultValue = "False", desc = "Determines whether or not the sorting is case sensitive"), @JinjavaParam(value = "by", type = "enum key|value", defaultValue = "key", desc = "Sort by dict key or value") }, diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java index d3a560679..c02de9c22 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns a list containing elements present in the first list but not the second list", + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), params = { - @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), @JinjavaParam(value = "list", type = "sequence", desc = "The second list") }, snippets = { 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 6110676bb..4f61c7782 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java @@ -21,13 +21,16 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Divides the current value by a divisor", + input = @JinjavaParam(value = "value", type = "number", desc = "The numerator to be divided"), params = { - @JinjavaParam(value = "value", type = "number", desc = "The numerator to be divided"), @JinjavaParam(value = "divisor", type = "number", desc = "The divisor to divide the value") }, snippets = { @@ -40,12 +43,16 @@ public class DivideFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { if (arg.length != 1) { - throw new InterpretException("filter multiply expects 1 arg >>> " + arg.length); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (divisor) argument"); } String toMul = arg[0]; Number num; if (toMul != null) { - num = new BigDecimal(toMul); + try { + num = new BigDecimal(toMul); + } catch (NumberFormatException e) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, toMul); + } } else { return object; } @@ -76,8 +83,8 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar if (object instanceof String) { try { return Double.valueOf((String) object) / num.doubleValue(); - } catch (Exception e) { - throw new InterpretException(object + " can't be dealed with multiply filter", e); + } catch (NumberFormatException e) { + throw new InvalidInputException(interpreter, this, InvalidReason.NUMBER_FORMAT, object.toString()); } } return object; 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 5a3fa628c..7fa4d8f67 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java @@ -18,13 +18,13 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Evaluates to true if the value is divisible by the given number", + input = @JinjavaParam(value = "value", type = "number", desc = "The value to be divided"), params = { - @JinjavaParam(value = "value", type = "number", desc = "The value to be divided"), @JinjavaParam(value = "divisor", type = "number", desc = "The divisor to check if the value is divisible by") }, snippets = { @@ -44,7 +44,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } if (object instanceof Number) { if (arg.length != 1) { - throw new InterpretException("filter divisible expects 1 arg >>> " + arg.length); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to divide by)"); } long factor = Long.parseLong(arg[0]); long value = ((Number) object).longValue(); 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 fb33641e8..6212441f3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java @@ -28,9 +28,7 @@ value = "Converts the characters &, <, >, ‘, and ” in string s to HTML-safe sequences. " + "Use this filter if you need to display text that might contain such characters in HTML. " + "Marks return value as markup string.", - params = { - @JinjavaParam(value = "s", desc = "String to escape") - }, + input = @JinjavaParam(value = "s", desc = "String to escape"), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"
This markup is printed as text
\" %}\n" + 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 b69be61be..a74f147f4 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java @@ -29,9 +29,7 @@ value = "Converts the characters { and } in string s to Jinjava-safe sequences. " + "Use this filter if you need to display text that might contain such characters in Jinjava. " + "Marks return value as markup string.", - params = { - @JinjavaParam(value = "s", desc = "String to escape") - }, + input = @JinjavaParam(value = "s", desc = "String to escape"), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"{{This markup is printed as text}}\" %}\n" + 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 c6dfb7326..482ebbc8f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java @@ -26,9 +26,7 @@ @JinjavaDoc( value = "Escapes strings so that they can be safely inserted into a JavaScript variable declaration", - params = { - @JinjavaParam(value = "s", desc = "String to escape") - }, + input = @JinjavaParam(value = "s", desc = "String to escape"), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"This string can safely be inserted into JavaScript\" %}\n" + 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 2820b0323..c892a302e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java @@ -11,9 +11,7 @@ @JinjavaDoc( value = "Escapes strings so that they can be used as JSON values", - params = { - @JinjavaParam(value = "s", desc = "String to escape") - }, + input = @JinjavaParam(value = "s", desc = "String to escape"), snippets = { @JinjavaSnippet( code = "{{String that contains JavaScript|escapejson}}" 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 1cfe50cc6..c3aa553ed 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java @@ -12,8 +12,8 @@ @JinjavaDoc( value = "Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc).", + input = @JinjavaParam(value = "value", desc = "The value to convert to filesize format"), params = { - @JinjavaParam(value = "value", desc = "The value to convert to filesize format"), @JinjavaParam(value = "binary", type = "boolean", defaultValue = "False", desc = "Use binary prefixes (Mebi, Gibi)") }, snippets = { 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 30bfddffe..02cf843e4 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java @@ -9,9 +9,7 @@ @JinjavaDoc( value = "Return the first item of a sequence.", - params = { - @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return first item from") - }, + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return first item from"), snippets = { @JinjavaSnippet( code = "{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}\n" + 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 7086a1058..f3f55c914 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java @@ -13,8 +13,8 @@ @JinjavaDoc( value = "Convert the value into a floating point number.", + input = @JinjavaParam(value = "value", desc = "Value to convert to a float"), params = { - @JinjavaParam(value = "value", desc = "Value to convert to a float"), @JinjavaParam(value = "default", type = "float", defaultValue = "0.0", desc = "Value to return if conversion fails") }, snippets = { 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 3b631bd51..1e5279af1 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java @@ -11,7 +11,7 @@ @JinjavaDoc( value = "Enforce HTML escaping. This will probably double escape variables.", - params = @JinjavaParam(value = "value", desc = "Value to escape"), + input = @JinjavaParam(value = "value", desc = "Value to escape"), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"
This markup is printed as text
\" %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java index 5769a3423..88c1a768f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java @@ -10,8 +10,8 @@ @JinjavaDoc( value = "Apply Python string formatting to an object.", + input = @JinjavaParam(value = "value", desc = "String value to reformat"), params = { - @JinjavaParam(value = "value", desc = "String value to reformat"), @JinjavaParam(value = "args", type = "String...", desc = "Values to insert into string") }, snippets = { 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 47034367f..ccdde5203 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java @@ -7,14 +7,13 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( value = "Converts JSON string to Object", - params = { - @JinjavaParam(value = "s", desc = "JSON String to write to object") - }, + input = @JinjavaParam(value = "string", desc = "JSON String to write to object"), snippets = { @JinjavaSnippet( code = "{{object|fromJson}}" @@ -26,16 +25,20 @@ public class FromJsonFilter implements Filter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + + if (var == null) { + return null; + } + try { if (var instanceof String) { return OBJECT_MAPPER.readValue((String) var, HashMap.class); } else { - throw new InterpretException(String.format("%s filter requires a string parameter", getName())); + throw new InvalidInputException(interpreter, this, InvalidReason.STRING); } - } catch (IOException e) { - throw new InterpretException("Could not convert JSON string to object in `fromjson` filter.", e, interpreter.getLineNumber()); + throw new InvalidInputException(interpreter, this, InvalidReason.JSON_READ); } } 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 8418c0815..2495e31a0 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java @@ -12,15 +12,15 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; @JinjavaDoc( value = "Group a sequence of objects by a common attribute.", + input = @JinjavaParam(value = "value", desc = "The dict to iterate through and group by a common attribute"), params = { - @JinjavaParam(value = "value", desc = "The dict to iterate through and group by a common attribute"), @JinjavaParam(value = "attribute", desc = "The common attribute to group by") }, snippets = { @@ -44,8 +44,9 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (args.length == 0) { - throw new InterpretException(getName() + " requires an attr name to group on", interpreter.getLineNumber()); + + if (args.length != 1) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attr name to group by)"); } String attr = args[0]; 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 b2de21644..135ba392f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java @@ -17,8 +17,8 @@ @JinjavaDoc( value = "Uses whitespace to indent a string.", + input = @JinjavaParam(value = "string", desc = "The string to indent"), params = { - @JinjavaParam(value = "s", desc = "The string to indent"), @JinjavaParam(value = "width", type = "number", defaultValue = "4", desc = "Amount of whitespace to indent"), @JinjavaParam(value = "indentfirst", type = "boolean", defaultValue = "False", desc = "If True, first line will be indented") }, 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 eb1f3a2fd..c5eef572c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java @@ -1,6 +1,5 @@ package com.hubspot.jinjava.lib.filter; -import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; @@ -17,8 +16,8 @@ */ @JinjavaDoc( value = "Convert the value into an integer.", + input = @JinjavaParam(value = "value", desc = "The value to convert to an integer"), params = { - @JinjavaParam(value = "value", desc = "The value to convert to an integer"), @JinjavaParam(value = "default", type = "number", defaultValue = "0", desc = "Value to return if the conversion fails") }, snippets = { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java index 275d299f6..836573f5c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns a list containing elements present in both lists", + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), params = { - @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), @JinjavaParam(value = "list", type = "sequence", desc = "The second list") }, snippets = { 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 a96c23a73..138ba1f70 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Evaluates to true if the value is a valid IPv4 or IPv6 address", + input = @JinjavaParam(value = "value", type = "string", desc = "String to check IP Address"), params = { - @JinjavaParam(value = "value", type = "string", desc = "String to check IP Address"), @JinjavaParam(value = "function", type = "string", desc = "Optional name of function. Supported functions: 'prefix'"), }, snippets = { 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 47a8bd169..9827fed02 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java @@ -17,8 +17,8 @@ @JinjavaDoc( value = "Return a string which is the concatenation of the strings in the sequence.", + input = @JinjavaParam(value = "value", desc = "The values to join"), params = { - @JinjavaParam(value = "value", desc = "The values to join"), @JinjavaParam(value = "d", desc = "The separator string used to join the items"), @JinjavaParam(value = "attr", desc = "Optional dict object attribute to use in joining") }, 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 fe8c7e2f4..0f9093dc7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java @@ -9,9 +9,7 @@ @JinjavaDoc( value = "Return the last item of a sequence", - params = { - @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return last item from") - }, + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return last item from"), snippets = { @JinjavaSnippet( code = "{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}\n" + 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 80449fa42..3cbe4b400 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java @@ -27,7 +27,7 @@ @JinjavaDoc( value = "Return the number of items of a sequence or mapping", - params = @JinjavaParam(value = "object", desc = "The sequence to count"), + input = @JinjavaParam(value = "object", desc = "The sequence to count"), snippets = { @JinjavaSnippet( code = "{% set services = ['Web design', 'SEO', 'Inbound Marketing', 'PPC'] %}\n" + 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 ff37642a5..d373f1b01 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java @@ -12,7 +12,7 @@ @JinjavaDoc( value = "Convert the value into a list. If it was a string the returned list will be a list of characters.", - params = @JinjavaParam(value = "value", desc = "Value to add to a sequence"), + input = @JinjavaParam(value = "value", desc = "Value to add to a sequence"), snippets = { @JinjavaSnippet( code = "{% set one = 1 %}\n" + 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 579f34a82..392c6cee7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java @@ -22,7 +22,7 @@ @JinjavaDoc( value = "Convert a value to lowercase", - params = @JinjavaParam(value = "s", desc = "String to make lowercase"), + input = @JinjavaParam(value = "s", desc = "String to make lowercase"), snippets = { @JinjavaSnippet(code = "{{ \"Text to MAKE Lowercase\"|lowercase }}") }) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java index 0e4753a76..ebfca9fd1 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java @@ -6,15 +6,15 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; @JinjavaDoc( value = "Applies a filter on a sequence of objects or looks up an attribute.", + input = @JinjavaParam(value = "value", type = "object", desc = "Sequence to apply filter or dict to lookup attribute"), params = { - @JinjavaParam(value = "value", type = "object", desc = "Sequence to apply filter or dict to lookup attribute"), @JinjavaParam(value = "attribute", desc = "Filter to apply to an object or dict attribute to lookup") }, snippets = { @@ -38,7 +38,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) ForLoop loop = ObjectIterator.getLoop(var); if (args.length == 0) { - throw new InterpretException(getName() + " filter requires name of filter or attribute to apply to given sequence"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (name of filter or attribute to apply to given sequence)"); } String attr = args[0]; 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 a828fa801..b75b3e623 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java @@ -28,7 +28,7 @@ @JinjavaDoc( value = "Calculates the md5 hash of the given object", - params = @JinjavaParam(value = "value", desc = "Value to get MD5 hash of"), + input = @JinjavaParam(value = "value", desc = "Value to get MD5 hash of"), snippets = { @JinjavaSnippet(code = "{{ content.absolute_url|md5 }}") }) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java index 49b9e82ea..a0644df5c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java @@ -3,6 +3,7 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; +import java.util.Map; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -16,8 +17,8 @@ */ @JinjavaDoc( value = "Subtracts a specified amount of time to a datetime object", + input = @JinjavaParam(value = "var", desc = "Datetime object or timestamp"), params = { - @JinjavaParam(value = "var", desc = "Datetime object or timestamp"), @JinjavaParam(value = "diff", desc = "The amount to subtract from the datetime"), @JinjavaParam(value = "unit", desc = "Which temporal unit to use"), }, @@ -27,10 +28,9 @@ public class MinusTimeFilter extends BaseDateFilter { @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - - long diff = parseDiffAmount(args); - ChronoUnit chronoUnit = parseChronoUnit(args); + public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { + long diff = parseDiffAmount(interpreter, args); + ChronoUnit chronoUnit = parseChronoUnit(interpreter, args); if (var instanceof ZonedDateTime) { ZonedDateTime dateTime = (ZonedDateTime) var; 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 65c9ba6bb..6413edc08 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java @@ -21,13 +21,16 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Multiplies the current object with the given multiplier", + input = @JinjavaParam(value = "value", type = "number", desc = "Base number to be multiplied"), params = { - @JinjavaParam(value = "value", type = "number", desc = "Base number to be multiplied"), @JinjavaParam(value = "multiplier", type = "number", desc = "The multiplier") }, snippets = { @@ -39,11 +42,17 @@ public class MultiplyFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + if (arg.length != 1) { - throw new InterpretException("filter multiply expects 1 arg >>> " + arg.length); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to multiply by)"); } String toMul = arg[0]; - Number num = new BigDecimal(toMul); + Number num; + try { + num = new BigDecimal(toMul); + } catch (NumberFormatException e) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, toMul); + } if (object instanceof Integer) { return num.intValue() * (Integer) object; @@ -72,8 +81,8 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar if (object instanceof String) { try { return num.doubleValue() * Double.parseDouble((String) object); - } catch (Exception e) { - throw new InterpretException(object + " can't be dealed with multiply filter", e); + } catch (NumberFormatException e) { + throw new InvalidInputException(interpreter, this, InvalidReason.NUMBER_FORMAT, object.toString()); } } return object; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java index 7b8a35976..b4d0b5f37 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java @@ -3,6 +3,7 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; +import java.util.Map; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; @@ -16,8 +17,8 @@ */ @JinjavaDoc( value = "Adds a specified amount of time to a datetime object", + input = @JinjavaParam(value = "var", desc = "Datetime object or timestamp"), params = { - @JinjavaParam(value = "var", desc = "Datetime object or timestamp"), @JinjavaParam(value = "diff", desc = "The amount to add to the datetime"), @JinjavaParam(value = "unit", desc = "Which temporal unit to use"), }, @@ -27,10 +28,9 @@ public class PlusTimeFilter extends BaseDateFilter { @Override - public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - - long diff = parseDiffAmount(args); - ChronoUnit chronoUnit = parseChronoUnit(args); + public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { + long diff = parseDiffAmount(interpreter, args); + ChronoUnit chronoUnit = parseChronoUnit(interpreter, args); if (var instanceof ZonedDateTime) { ZonedDateTime dateTime = (ZonedDateTime) var; 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 2fdce9065..d9975d683 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java @@ -23,7 +23,7 @@ @JinjavaDoc( value = "Pretty print a variable. Useful for debugging.", - params = @JinjavaParam(value = "value", type = "object", desc = "Object to Pretty Print"), + input = @JinjavaParam(value = "value", type = "object", desc = "Object to Pretty Print"), snippets = { @JinjavaSnippet( code = "{% set this_var =\"Variable that I want to debug\" %}\n" + 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 9da58e3eb..3d19f87f4 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java @@ -28,7 +28,7 @@ @JinjavaDoc( value = "Return a random item from the sequence.", - params = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return a random item from"), + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return a random item from"), snippets = { @JinjavaSnippet( desc = "The example below is a standard blog loop that returns a single random post.", 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 2ad745456..4deb28ffc 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java @@ -3,19 +3,21 @@ import com.google.re2j.Matcher; import com.google.re2j.Pattern; import com.google.re2j.PatternSyntaxException; - 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Return a copy of the value with all occurrences of a matched regular expression (Java RE2 syntax) " + "replaced with a new one. The first argument is the regular expression to be matched, the second " + "is the replacement string", + input = @JinjavaParam(value = "s", desc = "Base string to find and replace within"), params = { - @JinjavaParam(value = "s", desc = "Base string to find and replace within"), @JinjavaParam(value = "regex", desc = "The regular expression that you want to match and replace"), @JinjavaParam(value = "new", desc = "The new string that you replace the matched substring") }, @@ -35,12 +37,14 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + if (args.length != 2) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 2 arguments (regex string, replacement string)"); + } + if (var == null) { return null; } - if (args.length < 2) { - throw new InterpretException("filter " + getName() + " requires two string args"); - } + if (var instanceof String) { String s = (String) var; String toReplace = args[0]; @@ -51,11 +55,11 @@ public Object filter(Object var, JinjavaInterpreter interpreter, Matcher matcher = p.matcher(s); return matcher.replaceAll(replaceWith); - } catch(PatternSyntaxException e) { - throw new InterpretException(getName() + " filter requires a valid regular expression"); + } catch (PatternSyntaxException e) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.REGEX, 0, toReplace); } } else { - throw new InterpretException(getName() + " filter requires a string parameter"); + throw new InvalidInputException(interpreter, this, InvalidReason.STRING); } } } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java index b5f9c9695..cc7160ba9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java @@ -10,8 +10,8 @@ @JinjavaDoc( value = "Filters a sequence of objects by applying a test to an attribute of an object or the attribute and " + "rejecting the ones with the test succeeding.", + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to test"), params = { - @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to test"), @JinjavaParam(value = "attribute", desc = "Attribute to test for and reject items that contain it"), @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the rejection") }, 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 6808f74d0..385fe3a2c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java @@ -6,17 +6,19 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.lib.exptest.ExpTest; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; @JinjavaDoc( value = "Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.", + input = @JinjavaParam(value = "seq", type = "Sequence to test"), params = { - @JinjavaParam(value = "seq", type = "Sequence to test"), - @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the selection") + @JinjavaParam(value = "exp_test", type = "name of expression test", desc = "Specify which expression test to run for making the selection") }, snippets = { @JinjavaSnippet( @@ -35,12 +37,16 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) List result = new ArrayList<>(); if (args.length == 0) { - throw new InterpretException(getName() + " requires an exp test to filter on", interpreter.getLineNumber()); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (name of expression test to filter by)"); + } + + if (args[0] == null) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 0); } ExpTest expTest = interpreter.getContext().getExpTest(args[0]); if (expTest == null) { - throw new InterpretException("No exp test defined for name '" + args[0] + "'", interpreter.getLineNumber()); + throw new InvalidArgumentException(interpreter, this, InvalidReason.EXPRESSION_TEST, 0, args[0]); } ForLoop loop = ObjectIterator.getLoop(var); 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 3f5db04d5..7e7bfbdc6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java @@ -6,15 +6,15 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Return a copy of the value with all occurrences of a substring replaced with a new one. " + "The first argument is the substring that should be replaced, the second is the replacement " + "string. If the optional third argument count is given, only the first count occurrences are replaced", + input = @JinjavaParam(value = "s", desc = "Base string to find and replace within"), params = { - @JinjavaParam(value = "s", desc = "Base string to find and replace within"), @JinjavaParam(value = "old", desc = "The old substring that you want to match and replace"), @JinjavaParam(value = "new", desc = "The new string that you replace the matched substring"), @JinjavaParam(value = "count", type = "number", desc = "Replace only the first N occurrences") @@ -41,8 +41,10 @@ public Object filter(Object var, JinjavaInterpreter interpreter, if (var == null) { return null; } - if (args.length < 2) { - throw new InterpretException("filter " + getName() + " requires two string args"); + if (args.length != 2 && args.length != 3) { + throw new TemplateSyntaxException(interpreter, + getName(), + "requires 2 arguments (substring to replace, replacement string) or 3 arguments (substring to replace, replacement string, number of occurrences to replace)"); } String s = (String) var; 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 31d431ada..06742f4bd 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java @@ -15,8 +15,6 @@ **********************************************************************/ package com.hubspot.jinjava.lib.filter; -import static com.hubspot.jinjava.util.Logging.ENGINE_LOG; - import java.lang.reflect.Array; import java.util.Collection; @@ -27,7 +25,7 @@ @JinjavaDoc( value = "Reverse the object or return an iterator the iterates over it the other way round.", - params = @JinjavaParam(value = "value", type = "object", desc = "The sequence or dict to reverse the iteration order"), + input = @JinjavaParam(value = "value", type = "object", desc = "The sequence or dict to reverse the iteration order"), snippets = { @JinjavaSnippet( code = "{% set nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] %}\n" + @@ -74,7 +72,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } return String.valueOf(res); } - ENGINE_LOG.warn("filter contain can't be applied to >>> " + object.getClass().getName()); + return object; } 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 28ab56928..e8a0b5a0f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java @@ -2,17 +2,19 @@ import java.math.BigDecimal; import java.math.RoundingMode; -import java.util.Objects; import org.apache.commons.lang3.math.NumberUtils; 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.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( value = "Round the number to a given precision.", + input = @JinjavaParam(value = "value", type = "number", desc = "The number to round"), params = { @JinjavaParam(value = "value", type = "number", desc = "The number to round"), @JinjavaParam(value = "precision", type = "number", defaultValue = "0", desc = "Specifies the precision of rounding"), @@ -32,10 +34,16 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { + + if (var == null) { + return null; + } + BigDecimal result = BigDecimal.ZERO; try { - result = new BigDecimal(Objects.toString(var)); + result = new BigDecimal(var.toString()); } catch (NumberFormatException e) { + throw new InvalidInputException(interpreter, this, InvalidReason.NUMBER_FORMAT, var.toString()); } int precision = 0; 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 844d23185..dc3a9369b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java @@ -1,6 +1,7 @@ package com.hubspot.jinjava.lib.filter; 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; @@ -12,6 +13,7 @@ */ @JinjavaDoc( value = "Mark the value as safe, which means that in an environment with automatic escaping enabled this variable will not be escaped.", + input = @JinjavaParam(value = "value", desc = "Value to mark as safe"), snippets = { @JinjavaSnippet(code = "{{ content.post_list_content|safe }}") }) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java index fbe2766b8..b167df3ae 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java @@ -8,8 +8,10 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.lib.exptest.ExpTest; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; @@ -17,8 +19,8 @@ @JinjavaDoc( value = "Filters a sequence of objects by applying a test to an attribute of an object and only selecting the ones with the test succeeding.", + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test"), params = { - @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test"), @JinjavaParam(value = "attr", desc = "Attribute to test for and select items that contain it"), @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the selection") }, @@ -45,27 +47,26 @@ protected Object applyFilter(Object var, JinjavaInterpreter interpreter, Object[ List result = new ArrayList<>(); if (args.length == 0) { - throw new InterpretException(getName() + " filter requires an attr to filter on", interpreter.getLineNumber()); + throw new TemplateSyntaxException(interpreter, getName(), "requires at least 1 argument (attr to filter on)"); } - if (!(args[0] instanceof String)) { - throw new InterpretException(getName() + " filter requires the filter attr arg to be a string", interpreter.getLineNumber()); + if (args[0] == null) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 0); } - Object[] expArgs = new String[]{}; + String attr = args[0].toString(); - String attr = (String) args[0]; + Object[] expArgs = new String[]{}; ExpTest expTest = interpreter.getContext().getExpTest("truthy"); if (args.length > 1) { - - if (!(args[1] instanceof String)) { - throw new InterpretException(getName() + " filter requires the expression test arg to be a string", interpreter.getLineNumber()); + if (args[1] == null) { + throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 1); } - expTest = interpreter.getContext().getExpTest((String) args[1]); + expTest = interpreter.getContext().getExpTest(args[1].toString()); if (expTest == null) { - throw new InterpretException("No expression test defined with name '" + args[1] + "'", interpreter.getLineNumber()); + throw new InvalidArgumentException(interpreter, this, InvalidReason.EXPRESSION_TEST, 1, args[1].toString()); } if (args.length > 2) { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java index fe4cec106..f9c62dc65 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java @@ -8,14 +8,17 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.lib.exptest.ExpTest; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; @JinjavaDoc( value = "Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.", + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test"), params = { @JinjavaParam(value = "value", type = "sequence"), @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the selection") @@ -37,7 +40,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, List result = new ArrayList<>(); if (args.length == 0) { - throw new InterpretException(getName() + " requires an exp test to filter on", interpreter.getLineNumber()); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (name of expression test to filter by)"); } Object[] expArgs = new Object[]{}; @@ -48,7 +51,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, ExpTest expTest = interpreter.getContext().getExpTest(args[0].toString()); if (expTest == null) { - throw new InterpretException("No exp test defined for name '" + args[0] + "'", interpreter.getLineNumber()); + throw new InvalidArgumentException(interpreter, this, InvalidReason.EXPRESSION_TEST, 0, args[0].toString()); } ForLoop loop = ObjectIterator.getLoop(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 eed0da0c7..fa94bd7a3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java @@ -6,11 +6,13 @@ import java.util.List; 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; @JinjavaDoc( value = "Randomly shuffle a given list, returning a new list with all of the items of the original list in a random order", + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to shuffle"), snippets = { @JinjavaSnippet( desc = "The example below is a standard blog loop that's order is randomized on page load", 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 7620826b8..aa4b44c10 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java @@ -6,17 +6,16 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; @JinjavaDoc( value = "Slice an iterator and return a list of lists containing those items.", + input = @JinjavaParam(value = "value", type = "sequence", desc = "The sequence or dict that the filter is applied to"), params = { - @JinjavaParam(value = "value", type = "sequence", desc = "The sequence or dict that the filter is applied to"), @JinjavaParam(value = "slices", type = "number", desc = "Specifies how many items will be sliced"), - @JinjavaParam(value = "fill_with", desc = "Used to fill missing values on the last iteration") }, snippets = { @JinjavaSnippet( @@ -44,7 +43,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) ForLoop loop = ObjectIterator.getLoop(var); if (args.length == 0) { - throw new InterpretException(getName() + " requires number of slices argument", interpreter.getLineNumber()); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number of slices)"); } int slices = NumberUtils.toInt(args[0], 3); 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 7cda144f5..55d7302ab 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java @@ -15,8 +15,8 @@ @JinjavaDoc( value = "Sort an iterable.", + input = @JinjavaParam(value = "value", type = "iterable", desc = "The sequence or dict to sort through iteration"), params = { - @JinjavaParam(value = "value", type = "iterable", desc = "The sequence or dict to sort through iteration"), @JinjavaParam(value = "reverse", type = "boolean", defaultValue = "False", desc = "Boolean to reverse the sort order"), @JinjavaParam(value = "case_sensitive", type = "boolean", defaultValue = "False", desc = "Determines whether or not the sorting is case sensitive"), @JinjavaParam(value = "attribute", desc = "Specifies an attribute to sort by") @@ -43,7 +43,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { if (var == null) { - return var; + return null; } boolean reverse = false; 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 185ed2163..67cff29ab 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java @@ -23,8 +23,8 @@ */ @JinjavaDoc( value = "Splits the input string into a list on the given separator", + input = @JinjavaParam(value = "string", desc = "The string to split"), params = { - @JinjavaParam(value = "s", desc = "The string to split"), @JinjavaParam(value = "separator", defaultValue = " ", desc = "Specifies the separator to split the variable by"), @JinjavaParam(value = "limit", type = "number", defaultValue = "0", desc = "Limits resulting list by putting remainder of string into last list item") }, 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 f2f7828fe..cbb107b81 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java @@ -3,11 +3,13 @@ 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; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( value = "Returns string value of object", + input = @JinjavaParam(value = "value", desc = "The value to turn into a string"), snippets = { @JinjavaSnippet( code = "{% set number_to_string = 45 %}\n" + 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 661a35f58..65867c9c8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java @@ -3,14 +3,16 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.lib.fn.Functions; @JinjavaDoc( value = "Converts a datetime string and datetime format to a datetime object", + input = @JinjavaParam(value = "datetimeString", desc = "Datetime string"), params = { - @JinjavaParam(value = "datetimeString", desc = "Datetime string"), @JinjavaParam(value = "datetimeFormat", desc = "Format of the datetime string"), }, snippets = { @@ -21,12 +23,16 @@ public class StringToTimeFilter implements Filter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (!(var instanceof String)) { - throw new InterpretException(String.format("%s filter requires a string as input", getName())); + if (args.length != 1) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (datetime format string)"); + } + + if (var == null) { + return null; } - if (args.length < 1) { - throw new InterpretException(String.format("%s filter requires a datetime format parameter", getName())); + if (!(var instanceof String)) { + throw new InvalidInputException(interpreter, this, InvalidReason.STRING); } return Functions.stringToTime((String) var, args[0]); 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 d4ba4fc45..533b51118 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java @@ -5,6 +5,7 @@ import org.jsoup.Jsoup; 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; @@ -13,6 +14,7 @@ */ @JinjavaDoc( value = "Strip SGML/XML tags and replace adjacent whitespace by one space.", + input = @JinjavaParam(value = "string", desc = "string to strip tags from"), snippets = { @JinjavaSnippet( code = "{% set some_html = \"
Some text
\" %}\n" + @@ -24,6 +26,7 @@ public class StripTagsFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + if (!(object instanceof String)) { return object; } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java index e227ff50c..8d3d4f904 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java @@ -13,8 +13,8 @@ @JinjavaDoc( value = "Returns the sum of a sequence of numbers plus the value of parameter ‘start’ (which defaults to 0). When the sequence is empty it returns start.", + input = @JinjavaParam(value = "value", type = "iterable", desc = "Selects the sequence or dict to sum values from"), params = { - @JinjavaParam(value = "value", type = "iterable", desc = "Selects the sequence or dict to sum values from"), @JinjavaParam(value = "attribute", desc = "Specify an optional attribute of dict to sum"), @JinjavaParam(value = "start", type = "number", defaultValue = "0", desc = "Sets a value to return, if there is nothing in the variable to sum") }, diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java index 8a0643249..9d59c7ef5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns a list containing elements present in only one list.", + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), params = { - @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), @JinjavaParam(value = "list", type = "sequence", desc = "The second list") }, snippets = { 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 2b01d04b9..7dd46bf03 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java @@ -3,6 +3,7 @@ import org.apache.commons.lang3.text.WordUtils; 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; @@ -13,6 +14,7 @@ */ @JinjavaDoc( value = "Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.", + input = @JinjavaParam(value = "string", type = "string", desc = "the string to titlecase"), snippets = { @JinjavaSnippet( code = "{{ \"My title should be titlecase\"|title }} " 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 ef657641f..a074e20ca 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java @@ -5,15 +5,14 @@ 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.InterpretException; +import com.hubspot.jinjava.interpret.InvalidInputException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( value = "Writes object as a JSON string", - params = { - @JinjavaParam(value = "o", desc = "Object to write to JSON") - }, + input = @JinjavaParam(value = "object", desc = "Object to write to JSON"), snippets = { @JinjavaSnippet( code = "{{object|tojson}}" @@ -28,7 +27,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) try { return OBJECT_MAPPER.writeValueAsString(var); } catch (JsonProcessingException e) { - throw new InterpretException("Could not write object as string for `tojson` filter.", e, interpreter.getLineNumber()); + throw new InvalidInputException(interpreter, this, InvalidReason.JSON_WRITE); } } 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 b32335997..d3edbb8f3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java @@ -5,6 +5,7 @@ import org.apache.commons.lang3.StringUtils; 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; @@ -13,6 +14,7 @@ */ @JinjavaDoc( value = "Strip leading and trailing whitespace.", + input = @JinjavaParam(value = "string", type = "string", desc = "the string to strip whitespace from"), snippets = { @JinjavaSnippet( code = "{{ \" remove whitespace \"|trim }}") 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 18f757dc3..22c26eec7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java @@ -26,8 +26,8 @@ "If the second parameter is true the filter will cut the text at length. Otherwise it will discard the last word. " + "If the text was in fact truncated it will append an ellipsis sign (\"...\"). If you want a different ellipsis sign " + "than \"...\" you can specify it using the third parameter.", + input = @JinjavaParam(value = "string", desc = "The string to truncate"), params = { - @JinjavaParam(value = "s", desc = "The string to truncate"), @JinjavaParam(value = "length", type = "number", defaultValue = "255", desc = "Specifies the length at which to truncate the text (includes HTML characters)"), @JinjavaParam(value = "killwords", type = "boolean", defaultValue = "False", desc = "If true, the string will cut text at length"), @JinjavaParam(value = "end", defaultValue = "...", desc = "The characters that will be added to indicate where the text was truncated") 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 06018c0e2..8d923dc03 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java @@ -19,12 +19,15 @@ import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.lib.fn.Functions; -@JinjavaDoc(value = "Truncates a given string, respecting html markup (i.e. will properly close all nested tags)", params = { - @JinjavaParam(value = "html", desc = "HTML to truncate"), - @JinjavaParam(value = "length", type = "number", defaultValue = "255", desc = "Length at which to truncate text (HTML characters not included)"), - @JinjavaParam(value = "end", defaultValue = "...", desc = "The characters that will be added to indicate where the text was truncated"), - @JinjavaParam(value = "breakword", type = "boolean", defaultValue = "false", desc = "If set to true, text will be truncated in the middle of words") -}, snippets = { +@JinjavaDoc( + value = "Truncates a given string, respecting html markup (i.e. will properly close all nested tags)", + input = @JinjavaParam(value = "html", desc = "HTML to truncate"), + params = { + @JinjavaParam(value = "length", type = "number", defaultValue = "255", desc = "Length at which to truncate text (HTML characters not included)"), + @JinjavaParam(value = "end", defaultValue = "...", desc = "The characters that will be added to indicate where the text was truncated"), + @JinjavaParam(value = "breakword", type = "boolean", defaultValue = "false", desc = "If set to true, text will be truncated in the middle of words") + }, + snippets = { @JinjavaSnippet(code = "{{ \"

I want to truncate this text without breaking my HTML

\"|truncatehtml(28, '..', false) }}", output = "

I want to truncate this text without breaking my HTML

") }) public class TruncateHtmlFilter implements Filter { diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java index a7466b639..e9503d34e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns a list containing elements present in either list", + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), params = { - @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), @JinjavaParam(value = "list", type = "sequence", desc = "The second list") }, snippets = { 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 caf72f9fa..d21c9b053 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java @@ -12,8 +12,8 @@ @JinjavaDoc( value = "Extract a unique set from a sequence of objects", + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to filter"), params = { - @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to filter"), @JinjavaParam(value = "attr", type = "Optional attribute on object to use as unique identifier") }, snippets = { 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 e482c95be..c2eac829e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java @@ -8,9 +8,7 @@ @JinjavaDoc( value = "Gets the UNIX timestamp value (in milliseconds) of a date object", - params = { - @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable"), - }, + input = @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable"), snippets = { @JinjavaSnippet(code = "{% mydatetime|unixtimestamp %}"), }) 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 53df759d6..5f7ccd417 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java @@ -16,11 +16,13 @@ package com.hubspot.jinjava.lib.filter; 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; @JinjavaDoc( value = "Convert a value to uppercase", + input = @JinjavaParam(value = "string", type = "string", desc = "the string to uppercase"), snippets = { @JinjavaSnippet(code = "{{ \"text to make uppercase\"|uppercase }}") }) 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 3a5f83907..886933214 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java @@ -10,11 +10,13 @@ import org.apache.commons.lang3.StringUtils; 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; @JinjavaDoc( value = "Escape strings for use in URLs (uses UTF-8 encoding). It accepts both dictionaries and regular strings as well as pairwise iterables.", + input = @JinjavaParam(value = "url", type = "string", desc = "the url to escape"), snippets = { @JinjavaSnippet(code = "{{ \"Escape & URL encode this string\"|urlencode }}") }) 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 5686a8393..a18f21a7c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java @@ -15,8 +15,8 @@ @JinjavaDoc( value = "Converts URLs in plain text into clickable links.", + input = @JinjavaParam(value = "value", type="string", desc = "string URL to convert to an anchor"), params = { - @JinjavaParam(value = "value", desc = "string URL to convert to an anchor"), @JinjavaParam(value = "trim_url_limit", type = "number", desc = "Sets a character limit"), @JinjavaParam(value = "nofollow", type = "boolean", defaultValue = "False", desc = "Adds nofollow to generated link tag"), @JinjavaParam(value = "target", desc = "Adds target attr to generated link tag") 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 c01e4a40d..348527c2b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java @@ -5,11 +5,13 @@ import java.util.regex.Pattern; 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; @JinjavaDoc( value = "Counts the words in the given string", + input = @JinjavaParam(value = "string", type = "string", desc = "string to count the words from"), snippets = { @JinjavaSnippet( code = "{% set count_words = \"Count the number of words in this variable\" %}\n" + 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 492b0808e..3b150c35f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java @@ -13,8 +13,8 @@ @JinjavaDoc( value = "Return a copy of the string passed to the filter wrapped after 79 characters.", + input = @JinjavaParam(value = "s", type = "string", desc = "String to wrap after a certain number of characters"), params = { - @JinjavaParam(value = "s", desc = "String to wrap after a certain number of chracters"), @JinjavaParam(value = "width", type = "number", defaultValue = "79", desc = "Sets the width of spaces at which to wrap the text"), @JinjavaParam(value = "break_long_words", type = "boolean", defaultValue = "True", desc = "If true, long words will be broken when wrapped") }, 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 ebc331c40..c7dfd767f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java @@ -16,8 +16,8 @@ @JinjavaDoc( value = "Create an HTML/XML attribute string based on the items in a dict.", + input = @JinjavaParam(value = "dict", type = "dict", desc = "Dict to filter"), params = { - @JinjavaParam(value = "d", type = "dict", desc = "Dict to filter"), @JinjavaParam(value = "autospace", type = "boolean", defaultValue = "True", desc = "Automatically prepend a space in front of the item") }, snippets = { diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java index 005d980bd..645bf2738 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java @@ -9,7 +9,7 @@ import org.junit.Test; import com.hubspot.jinjava.Jinjava; -import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @@ -37,7 +37,7 @@ public void itReadsStringAsObject() { assertThat(nested.get("third")).isEqualTo(4); } - @Test(expected = InterpretException.class) + @Test(expected = InvalidArgumentException.class) public void itFailsWhenStringIsNotJson() { String nestedJson = "blah"; @@ -45,7 +45,7 @@ public void itFailsWhenStringIsNotJson() { filter.filter(nestedJson, interpreter); } - @Test(expected = InterpretException.class) + @Test(expected = InvalidArgumentException.class) public void itFailsWhenParameterIsNotString() { Integer nestedJson = 456; diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilterTest.java index f4cea074d..43c8a43b8 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilterTest.java @@ -7,6 +7,7 @@ import com.hubspot.jinjava.Jinjava; import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; public class RegexReplaceFilterTest { @@ -34,7 +35,7 @@ public void itMatchesRegexAndReplacesString() { assertThat(filter.filter("It costs $300", interpreter, "[^a-zA-Z]", "")).isEqualTo("Itcosts"); } - @Test(expected = InterpretException.class) + @Test(expected = InvalidArgumentException.class) public void isThrowsExceptionOnInvalidRegex() { filter.filter("It costs $300", interpreter, "[", ""); } From 1c6276ddb56cff3c7c8a541393d7d2364f549a7d Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 27 Feb 2019 14:16:26 -0500 Subject: [PATCH 3/6] More documentation changes. --- src/main/java/com/hubspot/jinjava/Jinjava.java | 3 +++ .../jinjava/doc/annotations/JinjavaDoc.java | 2 +- .../interpret/InvalidArgumentException.java | 6 ++++++ .../jinjava/interpret/InvalidInputException.java | 6 ++++++ .../hubspot/jinjava/interpret/TemplateError.java | 14 +++++++++++++- .../lib/exptest/IsContainingAllExpTest.java | 11 +++++++++++ .../jinjava/lib/exptest/IsContainingExpTest.java | 11 +++++++++++ .../jinjava/lib/exptest/IsDefinedExpTest.java | 2 ++ .../jinjava/lib/exptest/IsDivisibleByExpTest.java | 14 ++++++-------- .../jinjava/lib/exptest/IsEqualToExpTest.java | 3 ++- .../hubspot/jinjava/lib/exptest/IsEvenExpTest.java | 4 +++- .../jinjava/lib/exptest/IsIterableExpTest.java | 2 ++ .../jinjava/lib/exptest/IsLowerExpTest.java | 4 +++- .../jinjava/lib/exptest/IsMappingExpTest.java | 2 ++ .../hubspot/jinjava/lib/exptest/IsNoneExpTest.java | 2 ++ .../jinjava/lib/exptest/IsNumberExpTest.java | 2 ++ .../hubspot/jinjava/lib/exptest/IsOddExpTest.java | 4 +++- .../jinjava/lib/exptest/IsSameAsExpTest.java | 4 +++- .../jinjava/lib/exptest/IsSequenceExpTest.java | 2 ++ .../lib/exptest/IsStringContainingExpTest.java | 3 ++- .../jinjava/lib/exptest/IsStringExpTest.java | 2 ++ .../lib/exptest/IsStringStartingWithExpTest.java | 3 +++ .../jinjava/lib/exptest/IsTruthyExpTest.java | 2 ++ .../jinjava/lib/exptest/IsUndefinedExpTest.java | 2 ++ .../jinjava/lib/exptest/IsUpperExpTest.java | 2 ++ .../jinjava/lib/exptest/IsWithinExpTest.java | 11 +++++++++++ .../jinjava/lib/filter/FromJsonFilterTest.java | 6 +++--- 27 files changed, 110 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/Jinjava.java b/src/main/java/com/hubspot/jinjava/Jinjava.java index 10c35978e..3c722f7c6 100644 --- a/src/main/java/com/hubspot/jinjava/Jinjava.java +++ b/src/main/java/com/hubspot/jinjava/Jinjava.java @@ -30,6 +30,7 @@ import com.hubspot.jinjava.interpret.FatalTemplateErrorsException; import com.hubspot.jinjava.interpret.InterpretException; import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.RenderResult; import com.hubspot.jinjava.interpret.TemplateError; @@ -206,6 +207,8 @@ public RenderResult renderForResult(String template, Map bindings, Ji return new RenderResult(TemplateError.fromSyntaxError(e), interpreter.getContext(), interpreter.getErrorsCopy()); } catch (InvalidArgumentException e) { return new RenderResult(TemplateError.fromInvalidArgumentException(e), interpreter.getContext(), interpreter.getErrorsCopy()); + } catch (InvalidInputException e) { + return new RenderResult(TemplateError.fromInvalidInputException(e), interpreter.getContext(), interpreter.getErrorsCopy()); } catch (Exception e) { return new RenderResult(TemplateError.fromException(e), interpreter.getContext(), interpreter.getErrorsCopy()); diff --git a/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java b/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java index e0efa143e..af96714ff 100644 --- a/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java +++ b/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaDoc.java @@ -11,7 +11,7 @@ String value() default ""; - JinjavaParam input(); + JinjavaParam[] input() default {}; JinjavaParam[] params() default {}; diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java index 0a83bcd81..aabba0363 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java @@ -7,6 +7,7 @@ public class InvalidArgumentException extends RuntimeException { private final int lineNumber; private final int startPosition; private final String message; + private final String name; public InvalidArgumentException(JinjavaInterpreter interpreter, Importable importable, InvalidReason invalidReason, int argumentNumber, Object... errorMessageArgs) { this.message = String.format("Invalid argument in '%s': %s", @@ -15,6 +16,7 @@ public InvalidArgumentException(JinjavaInterpreter interpreter, Importable impor this.lineNumber = interpreter.getLineNumber(); this.startPosition = interpreter.getPosition(); + this.name = importable.getName(); } public String getMessage() { @@ -29,6 +31,10 @@ public int getStartPosition() { return startPosition; } + public String getName() { + return name; + } + private static String formatArgumentNumber(int argumentNumber) { switch (argumentNumber){ case 1: diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java index f2072756d..fd82e23cc 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidInputException.java @@ -7,6 +7,7 @@ public class InvalidInputException extends RuntimeException { private final int lineNumber; private final int startPosition; private final String message; + private final String name; public InvalidInputException(JinjavaInterpreter interpreter, Importable importable, InvalidReason invalidReason, Object... errorMessageArgs) { this.message = String.format("Invalid input in '%s': input variable %s", @@ -15,6 +16,7 @@ public InvalidInputException(JinjavaInterpreter interpreter, Importable importab this.lineNumber = interpreter.getLineNumber(); this.startPosition = interpreter.getPosition(); + this.name = importable.getName(); } public String getMessage() { @@ -28,4 +30,8 @@ public int getLineNumber() { public int getStartPosition() { return startPosition; } + + public String getName() { + return name; + } } diff --git a/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java b/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java index 2c3e606b0..bcdc45cd2 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java +++ b/src/main/java/com/hubspot/jinjava/interpret/TemplateError.java @@ -27,6 +27,7 @@ public enum ErrorReason { MISSING, DISABLED, INVALID_ARGUMENT, + INVALID_INPUT, OTHER } @@ -73,7 +74,18 @@ public static TemplateError fromInvalidArgumentException(InvalidArgumentExceptio ErrorReason.INVALID_ARGUMENT, ErrorItem.PROPERTY, ex.getMessage(), - ex.getFieldName(), + ex.getName(), + ex.getLineNumber(), + ex.getStartPosition(), + ex); + } + + public static TemplateError fromInvalidInputException(InvalidInputException ex) { + return new TemplateError(ErrorType.FATAL, + ErrorReason.INVALID_INPUT, + ErrorItem.PROPERTY, + ex.getMessage(), + ex.getName(), ex.getLineNumber(), ex.getStartPosition(), ex); diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java index 4db9f6853..701aa7358 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java @@ -2,10 +2,21 @@ 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; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; +@JinjavaDoc( + value = "Returns true if a list contains all values in a second list", + input = @JinjavaParam(value = "list", type="list"), + params = @JinjavaParam(value = "list_two", type="list", desc = "The second list to check if every element is in the first list"), + snippets = { + @JinjavaSnippet( + code = "{{ [1, 2, 3] is containingall [2, 3] }}") + }) public class IsContainingAllExpTest implements ExpTest { @Override diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java index 03000287f..e0e958a4c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java @@ -2,10 +2,21 @@ 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; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; +@JinjavaDoc( + value = "Returns true if a list contains a value", + input = @JinjavaParam(value = "list", type = "list"), + params = @JinjavaParam(value = "value", type = "object", desc = "The value to check is in the list"), + snippets = { + @JinjavaSnippet( + code = "{{ [1, 2, 3] is containing 2 }}") + }) public class IsContainingExpTest implements ExpTest { @Override diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java index b990187ba..50071abd9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if the variable is defined", + input = @JinjavaParam(value = "value", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is defined %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java index db64ab9a7..951fdf9fc 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java @@ -4,13 +4,14 @@ import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidReason; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.TemplateSyntaxException; -@JinjavaDoc(value = "Check if a variable is divisible by a number", - params = { - @JinjavaParam(value = "num", type = "number", desc = "The number to check whether a number is divisble by") - }, +@JinjavaDoc( + value = "Returns true if a variable is divisible by a number", + input = @JinjavaParam(value = "num", type = "number"), + params = @JinjavaParam(value = "divisor", type = "number", desc = "The number to check whether a number is divisible by"), snippets = { @JinjavaSnippet( code = "{% if variable is divisbleby 5 %}\n" + @@ -44,10 +45,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar } if (!Number.class.isAssignableFrom(args[0].getClass())) { - throw new InvalidArgumentException(interpreter, - getName(), - String.format(InvalidArgumentException.NUMBER_FORMAT_EXCEPTION_TEMPLATE, "num", args[0].toString()), - "num"); + throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0].toString()); } return ((Number) var).intValue() % ((Number) args[0]).intValue() == 0; diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java index 8ec638248..2bb6718d5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java @@ -11,7 +11,8 @@ import de.odysseus.el.misc.TypeConverter; @JinjavaDoc( - value = "Check if an object has the same value as another object", + value = "Returns true if an object has the same value as another object", + input = @JinjavaParam(value = "first", type = "object"), params = { @JinjavaParam(value = "other", type = "object", desc = "Another object to check equality against") }, diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java index 7074fecfa..1761cefc1 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( - value = "Return true if the value is even", + value = "Returns true if the value is even", + input = @JinjavaParam(value = "num", type = "number"), snippets = { @JinjavaSnippet( code = "{% if variable is even %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java index e810e8bd5..94e478e99 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if the object is iterable (sequence, dict, etc)", + input = @JinjavaParam(value = "object", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is iterable %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java index aa2775db1..e426980f8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java @@ -3,11 +3,13 @@ import org.apache.commons.lang3.StringUtils; 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; @JinjavaDoc( - value = "Return true if the given string is all lowercased", + value = "Return true if the given string is all lowercase", + input = @JinjavaParam(value = "string", type = "string"), snippets = { @JinjavaSnippet( code = "{% if variable is lower %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java index c262644b9..e41243256 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java @@ -3,11 +3,13 @@ import java.util.Map; 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; @JinjavaDoc( value = "Return true if the given object is a dict", + input = @JinjavaParam(value = "object", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is mapping %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java index ebfd1fe69..9e77a6e9f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if the given object is null / none", + input = @JinjavaParam(value = "object", type = "object"), snippets = { @JinjavaSnippet( code = "{% unless variable is none %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java index 1316100bf..24260f825 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if the object is a number", + input = @JinjavaParam(value = "object", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is number %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java index 5df14e94f..2135f2208 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( - value = "Return true if the object is an odd number", + value = "Return true if a number is an odd number", + input = @JinjavaParam(value = "num", type = "number"), snippets = { @JinjavaSnippet( code = "{% if variable is odd %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java index b20cf3843..282c7b1bb 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java @@ -6,7 +6,9 @@ import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.TemplateSyntaxException; -@JinjavaDoc(value = "Return true if variable is pointing at same object as other variable", +@JinjavaDoc( + value = "Return true if variable is pointing at same object as other variable", + input = @JinjavaParam(value = "object", type = "object"), params = @JinjavaParam(value = "other", type = "object", desc = "A second object to check the variables value against"), snippets = { @JinjavaSnippet( diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java index 24847a32e..f46d18d87 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if the variable is a sequence. Sequences are variables that are iterable.", + input = @JinjavaParam(value = "object", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is sequence %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java index 85e24e6c7..a26bfe674 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java @@ -8,7 +8,8 @@ @JinjavaDoc( value = "Return true if object is a string which contains a specified other string", - params = @JinjavaParam(value = "string", type = "string", desc = "A second string to check is contained in the first string"), + input = @JinjavaParam(value = "string", type = "string"), + params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is contained in the first string"), snippets = { @JinjavaSnippet( code = "{% if variable is string_containing 'foo' %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java index 154b64cc8..73d0fa2f2 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if object is a string", + input = @JinjavaParam(value = "value", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is string %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java index 16d7e3d71..e88254ddc 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java @@ -1,12 +1,15 @@ package com.hubspot.jinjava.lib.exptest; 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @JinjavaDoc( value = "Return true if object is a string which starts with a specified other string", + input = @JinjavaParam(value = "string", type = "string"), + params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is the start of the first string"), snippets = { @JinjavaSnippet( code = "{% if variable is string_startingwith 'foo' %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java index 1cb611925..96f6b0638 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java @@ -1,12 +1,14 @@ package com.hubspot.jinjava.lib.exptest; 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.util.ObjectTruthValue; @JinjavaDoc( value = "Return true if object is 'truthy'", + input = @JinjavaParam(value = "value", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is truthy %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java index da7274607..45eacc899 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java @@ -1,11 +1,13 @@ package com.hubspot.jinjava.lib.exptest; 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; @JinjavaDoc( value = "Return true if object is undefined", + input = @JinjavaParam(value = "value", type = "object"), snippets = { @JinjavaSnippet( code = "{% if variable is undefined %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java index ee5842714..20dda09d8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java @@ -3,11 +3,13 @@ import org.apache.commons.lang3.StringUtils; 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; @JinjavaDoc( value = "Return true if string is all uppercased", + input = @JinjavaParam(value = "value", type = "string"), snippets = { @JinjavaSnippet( code = "{% if variable is upper %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java index df22645e1..16675fbfb 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java @@ -2,10 +2,21 @@ 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; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; +@JinjavaDoc( + value = "Returns true if a value is within a list", + input = @JinjavaParam(value = "value", type="object"), + params = @JinjavaParam(value = "list", type="list", desc = "A list to check if the value is in."), + snippets = { + @JinjavaSnippet( + code = "{{ 2 is within [1, 2, 3] }}") + }) public class IsWithinExpTest implements ExpTest { @Override diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java index 645bf2738..fe6dca52c 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/FromJsonFilterTest.java @@ -9,7 +9,7 @@ import org.junit.Test; import com.hubspot.jinjava.Jinjava; -import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; @@ -37,7 +37,7 @@ public void itReadsStringAsObject() { assertThat(nested.get("third")).isEqualTo(4); } - @Test(expected = InvalidArgumentException.class) + @Test(expected = InvalidInputException.class) public void itFailsWhenStringIsNotJson() { String nestedJson = "blah"; @@ -45,7 +45,7 @@ public void itFailsWhenStringIsNotJson() { filter.filter(nestedJson, interpreter); } - @Test(expected = InvalidArgumentException.class) + @Test(expected = InvalidInputException.class) public void itFailsWhenParameterIsNotString() { Integer nestedJson = 456; From 0d5aeecf97da10ebcfdcf83894a1d84f6621c00f Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 27 Feb 2019 14:25:32 -0500 Subject: [PATCH 4/6] catch errors in resolver. --- .../java/com/hubspot/jinjava/el/ExpressionResolver.java | 8 +++++++- src/main/java/com/hubspot/jinjava/tree/TagNode.java | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java b/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java index b34172c5f..ca6b1ab9f 100644 --- a/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java +++ b/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java @@ -13,8 +13,11 @@ import com.google.common.collect.ImmutableMap; import com.hubspot.jinjava.el.ext.NamedParameter; +import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.interpret.DisabledException; import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.TemplateError; import com.hubspot.jinjava.interpret.TemplateError.ErrorItem; @@ -23,7 +26,6 @@ import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.interpret.UnknownTokenException; import com.hubspot.jinjava.interpret.errorcategory.BasicTemplateErrorCategory; -import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.lib.fn.ELFunctionDefinition; import de.odysseus.el.tree.TreeBuilderException; @@ -97,6 +99,10 @@ public Object resolveExpression(String expression) { } catch (DeferredValueException e) { // Re-throw so that it can be handled in JinjavaInterpreter throw e; + } catch (InvalidInputException e) { + interpreter.addError(TemplateError.fromInvalidInputException(e)); + } catch (InvalidArgumentException e) { + interpreter.addError(TemplateError.fromInvalidArgumentException(e)); } catch (Exception e) { interpreter.addError(TemplateError.fromException(new InterpretException( String.format("Error resolving expression [%s]: " + getRootCauseMessage(e), expression), e, interpreter.getLineNumber(), interpreter.getPosition()))); diff --git a/src/main/java/com/hubspot/jinjava/tree/TagNode.java b/src/main/java/com/hubspot/jinjava/tree/TagNode.java index d79e745b3..a286c1ded 100644 --- a/src/main/java/com/hubspot/jinjava/tree/TagNode.java +++ b/src/main/java/com/hubspot/jinjava/tree/TagNode.java @@ -16,6 +16,8 @@ package com.hubspot.jinjava.tree; import com.hubspot.jinjava.interpret.InterpretException; +import com.hubspot.jinjava.interpret.InvalidArgumentException; +import com.hubspot.jinjava.interpret.InvalidInputException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.lib.tag.Tag; @@ -57,7 +59,7 @@ public OutputNode render(JinjavaInterpreter interpreter) { return tag.interpretOutput(this, interpreter); } catch (DeferredValueException e) { return new RenderedOutputNode(reconstructImage()); - } catch (InterpretException e) { + } catch (InterpretException|InvalidInputException| InvalidArgumentException e) { throw e; } catch (Exception e) { throw new InterpretException("Error rendering tag", e, master.getLineNumber(), master.getStartPosition()); From 52efbad9ebc0eb2de54759fe27549d27dac74134 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Fri, 1 Mar 2019 14:13:06 -0500 Subject: [PATCH 5/6] Add required params --- .../jinjava/doc/annotations/JinjavaParam.java | 2 ++ .../lib/exptest/IsContainingAllExpTest.java | 4 ++-- .../jinjava/lib/exptest/IsContainingExpTest.java | 4 ++-- .../jinjava/lib/exptest/IsDefinedExpTest.java | 2 +- .../jinjava/lib/exptest/IsDivisibleByExpTest.java | 4 ++-- .../jinjava/lib/exptest/IsEqualToExpTest.java | 4 ++-- .../hubspot/jinjava/lib/exptest/IsEvenExpTest.java | 2 +- .../jinjava/lib/exptest/IsIterableExpTest.java | 2 +- .../jinjava/lib/exptest/IsLowerExpTest.java | 2 +- .../jinjava/lib/exptest/IsMappingExpTest.java | 2 +- .../hubspot/jinjava/lib/exptest/IsNoneExpTest.java | 2 +- .../jinjava/lib/exptest/IsNumberExpTest.java | 2 +- .../hubspot/jinjava/lib/exptest/IsOddExpTest.java | 2 +- .../jinjava/lib/exptest/IsSameAsExpTest.java | 4 ++-- .../jinjava/lib/exptest/IsSequenceExpTest.java | 2 +- .../lib/exptest/IsStringContainingExpTest.java | 6 +++--- .../jinjava/lib/exptest/IsStringExpTest.java | 2 +- .../lib/exptest/IsStringStartingWithExpTest.java | 8 ++++---- .../jinjava/lib/exptest/IsTruthyExpTest.java | 2 +- .../jinjava/lib/exptest/IsUndefinedExpTest.java | 2 +- .../jinjava/lib/exptest/IsUpperExpTest.java | 2 +- .../jinjava/lib/exptest/IsWithinExpTest.java | 4 ++-- .../com/hubspot/jinjava/lib/filter/AbsFilter.java | 2 +- .../jinjava/lib/filter/AbstractSetFilter.java | 10 ++++++++-- .../com/hubspot/jinjava/lib/filter/AddFilter.java | 14 +++++++------- .../com/hubspot/jinjava/lib/filter/AttrFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/BaseDateFilter.java | 4 ++-- .../hubspot/jinjava/lib/filter/BatchFilter.java | 2 +- .../jinjava/lib/filter/BetweenTimesFilter.java | 8 ++++---- .../com/hubspot/jinjava/lib/filter/BoolFilter.java | 2 +- .../jinjava/lib/filter/CapitalizeFilter.java | 2 +- .../hubspot/jinjava/lib/filter/CenterFilter.java | 2 +- .../com/hubspot/jinjava/lib/filter/CutFilter.java | 6 +++--- .../jinjava/lib/filter/DateTimeFormatFilter.java | 2 +- .../hubspot/jinjava/lib/filter/DefaultFilter.java | 10 +++++----- .../hubspot/jinjava/lib/filter/DictSortFilter.java | 2 +- .../jinjava/lib/filter/DifferenceFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/DivideFilter.java | 6 +++--- .../jinjava/lib/filter/DivisibleFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/EscapeFilter.java | 2 +- .../jinjava/lib/filter/EscapeJinjavaFilter.java | 2 +- .../hubspot/jinjava/lib/filter/EscapeJsFilter.java | 2 +- .../jinjava/lib/filter/EscapeJsonFilter.java | 2 +- .../jinjava/lib/filter/FileSizeFormatFilter.java | 2 +- .../hubspot/jinjava/lib/filter/FirstFilter.java | 2 +- .../hubspot/jinjava/lib/filter/FloatFilter.java | 2 +- .../jinjava/lib/filter/ForceEscapeFilter.java | 2 +- .../hubspot/jinjava/lib/filter/FormatFilter.java | 2 +- .../hubspot/jinjava/lib/filter/FromJsonFilter.java | 2 +- .../hubspot/jinjava/lib/filter/GroupByFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/IndentFilter.java | 2 +- .../com/hubspot/jinjava/lib/filter/IntFilter.java | 2 +- .../jinjava/lib/filter/IntersectFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/IpAddrFilter.java | 4 ++-- .../com/hubspot/jinjava/lib/filter/JoinFilter.java | 4 ++-- .../com/hubspot/jinjava/lib/filter/LastFilter.java | 2 +- .../hubspot/jinjava/lib/filter/LengthFilter.java | 2 +- .../com/hubspot/jinjava/lib/filter/ListFilter.java | 2 +- .../hubspot/jinjava/lib/filter/LowerFilter.java | 2 +- .../com/hubspot/jinjava/lib/filter/MapFilter.java | 6 +++--- .../com/hubspot/jinjava/lib/filter/Md5Filter.java | 2 +- .../jinjava/lib/filter/MinusTimeFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/MultiplyFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/PlusTimeFilter.java | 6 +++--- .../jinjava/lib/filter/PrettyPrintFilter.java | 2 +- .../hubspot/jinjava/lib/filter/RandomFilter.java | 2 +- .../jinjava/lib/filter/RegexReplaceFilter.java | 8 ++++---- .../jinjava/lib/filter/RejectAttrFilter.java | 4 ++-- .../hubspot/jinjava/lib/filter/RejectFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/ReplaceFilter.java | 8 ++++---- .../hubspot/jinjava/lib/filter/ReverseFilter.java | 2 +- .../hubspot/jinjava/lib/filter/RoundFilter.java | 5 ++--- .../com/hubspot/jinjava/lib/filter/SafeFilter.java | 2 +- .../jinjava/lib/filter/SelectAttrFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/SelectFilter.java | 3 +-- .../hubspot/jinjava/lib/filter/ShuffleFilter.java | 2 +- .../hubspot/jinjava/lib/filter/SliceFilter.java | 6 +++--- .../com/hubspot/jinjava/lib/filter/SortFilter.java | 2 +- .../hubspot/jinjava/lib/filter/SplitFilter.java | 4 ++-- .../hubspot/jinjava/lib/filter/StringFilter.java | 2 +- .../jinjava/lib/filter/StringToTimeFilter.java | 6 +++--- .../jinjava/lib/filter/StripTagsFilter.java | 2 +- .../com/hubspot/jinjava/lib/filter/SumFilter.java | 4 ++-- .../lib/filter/SymmetricDifferenceFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/TitleFilter.java | 2 +- .../hubspot/jinjava/lib/filter/ToJsonFilter.java | 2 +- .../com/hubspot/jinjava/lib/filter/TrimFilter.java | 2 +- .../hubspot/jinjava/lib/filter/TruncateFilter.java | 2 +- .../jinjava/lib/filter/TruncateHtmlFilter.java | 2 +- .../hubspot/jinjava/lib/filter/UnionFilter.java | 6 +++--- .../hubspot/jinjava/lib/filter/UniqueFilter.java | 2 +- .../jinjava/lib/filter/UnixTimestampFilter.java | 2 +- .../hubspot/jinjava/lib/filter/UpperFilter.java | 2 +- .../jinjava/lib/filter/UrlEncodeFilter.java | 2 +- .../hubspot/jinjava/lib/filter/UrlizeFilter.java | 2 +- .../jinjava/lib/filter/WordCountFilter.java | 2 +- .../hubspot/jinjava/lib/filter/WordWrapFilter.java | 2 +- .../hubspot/jinjava/lib/filter/XmlAttrFilter.java | 2 +- .../java/com/hubspot/jinjava/lib/fn/Functions.java | 6 +++--- 99 files changed, 180 insertions(+), 174 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaParam.java b/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaParam.java index c8f4e4448..49db56167 100644 --- a/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaParam.java +++ b/src/main/java/com/hubspot/jinjava/doc/annotations/JinjavaParam.java @@ -17,4 +17,6 @@ String defaultValue() default ""; + boolean required() default false; + } diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java index 701aa7358..d581b64ff 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingAllExpTest.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns true if a list contains all values in a second list", - input = @JinjavaParam(value = "list", type="list"), - params = @JinjavaParam(value = "list_two", type="list", desc = "The second list to check if every element is in the first list"), + input = @JinjavaParam(value = "list", type="list", required = true), + params = @JinjavaParam(value = "list_two", type="list", desc = "The second list to check if every element is in the first list", required = true), snippets = { @JinjavaSnippet( code = "{{ [1, 2, 3] is containingall [2, 3] }}") diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java index e0e958a4c..019d2b772 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsContainingExpTest.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns true if a list contains a value", - input = @JinjavaParam(value = "list", type = "list"), - params = @JinjavaParam(value = "value", type = "object", desc = "The value to check is in the list"), + input = @JinjavaParam(value = "list", type = "list", required = true), + params = @JinjavaParam(value = "value", type = "object", desc = "The value to check is in the list", required = true), snippets = { @JinjavaSnippet( code = "{{ [1, 2, 3] is containing 2 }}") diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java index 50071abd9..bb332d6ab 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDefinedExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if the variable is defined", - input = @JinjavaParam(value = "value", type = "object"), + input = @JinjavaParam(value = "value", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is defined %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java index 951fdf9fc..91e330c25 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsDivisibleByExpTest.java @@ -10,8 +10,8 @@ @JinjavaDoc( value = "Returns true if a variable is divisible by a number", - input = @JinjavaParam(value = "num", type = "number"), - params = @JinjavaParam(value = "divisor", type = "number", desc = "The number to check whether a number is divisible by"), + input = @JinjavaParam(value = "num", type = "number", required = true), + params = @JinjavaParam(value = "divisor", type = "number", desc = "The number to check whether a number is divisible by", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is divisbleby 5 %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java index 2bb6718d5..af05387f3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java @@ -12,9 +12,9 @@ @JinjavaDoc( value = "Returns true if an object has the same value as another object", - input = @JinjavaParam(value = "first", type = "object"), + input = @JinjavaParam(value = "first", type = "object", required = true), params = { - @JinjavaParam(value = "other", type = "object", desc = "Another object to check equality against") + @JinjavaParam(value = "other", type = "object", desc = "Another object to check equality against", required = true) }, snippets = { @JinjavaSnippet( diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java index 1761cefc1..30f3b32ff 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEvenExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Returns true if the value is even", - input = @JinjavaParam(value = "num", type = "number"), + input = @JinjavaParam(value = "num", type = "number", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is even %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java index 94e478e99..0e48566e6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsIterableExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if the object is iterable (sequence, dict, etc)", - input = @JinjavaParam(value = "object", type = "object"), + input = @JinjavaParam(value = "object", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is iterable %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java index e426980f8..e75a9a30f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsLowerExpTest.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Return true if the given string is all lowercase", - input = @JinjavaParam(value = "string", type = "string"), + input = @JinjavaParam(value = "string", type = "string", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is lower %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java index e41243256..f80e87c5a 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsMappingExpTest.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Return true if the given object is a dict", - input = @JinjavaParam(value = "object", type = "object"), + input = @JinjavaParam(value = "object", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is mapping %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java index 9e77a6e9f..edeea0934 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNoneExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if the given object is null / none", - input = @JinjavaParam(value = "object", type = "object"), + input = @JinjavaParam(value = "object", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% unless variable is none %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java index 24260f825..1022678cf 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsNumberExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if the object is a number", - input = @JinjavaParam(value = "object", type = "object"), + input = @JinjavaParam(value = "object", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is number %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java index 2135f2208..700ae4308 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsOddExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if a number is an odd number", - input = @JinjavaParam(value = "num", type = "number"), + input = @JinjavaParam(value = "num", type = "number", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is odd %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java index 282c7b1bb..96441fbef 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSameAsExpTest.java @@ -8,8 +8,8 @@ @JinjavaDoc( value = "Return true if variable is pointing at same object as other variable", - input = @JinjavaParam(value = "object", type = "object"), - params = @JinjavaParam(value = "other", type = "object", desc = "A second object to check the variables value against"), + input = @JinjavaParam(value = "object", type = "object", required = true), + params = @JinjavaParam(value = "other", type = "object", desc = "A second object to check the variables value against", required = true), snippets = { @JinjavaSnippet( code = "{% if var_one is sameas var_two %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java index f46d18d87..db67ec188 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsSequenceExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if the variable is a sequence. Sequences are variables that are iterable.", - input = @JinjavaParam(value = "object", type = "object"), + input = @JinjavaParam(value = "object", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is sequence %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java index a26bfe674..6b6060974 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringContainingExpTest.java @@ -8,8 +8,8 @@ @JinjavaDoc( value = "Return true if object is a string which contains a specified other string", - input = @JinjavaParam(value = "string", type = "string"), - params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is contained in the first string"), + input = @JinjavaParam(value = "string", type = "string", required = true), + params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is contained in the first string", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is string_containing 'foo' %}\n" + @@ -30,7 +30,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar } if (args.length == 0) { - throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (a second string)"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (other string to compare to)"); } if (args[0] == null) { diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java index 73d0fa2f2..072440960 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if object is a string", - input = @JinjavaParam(value = "value", type = "object"), + input = @JinjavaParam(value = "value", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is string %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java index e88254ddc..06ca062eb 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsStringStartingWithExpTest.java @@ -3,13 +3,13 @@ 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.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; @JinjavaDoc( value = "Return true if object is a string which starts with a specified other string", - input = @JinjavaParam(value = "string", type = "string"), - params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is the start of the first string"), + input = @JinjavaParam(value = "value", type = "string", required = true), + params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is the start of the first string", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is string_startingwith 'foo' %}\n" + @@ -30,7 +30,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar } if (args.length == 0) { - throw new InterpretException(getName() + " test requires an argument"); + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (other string to compare to)"); } if (args[0] == null) { diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java index 96f6b0638..1dba5aee7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsTruthyExpTest.java @@ -8,7 +8,7 @@ @JinjavaDoc( value = "Return true if object is 'truthy'", - input = @JinjavaParam(value = "value", type = "object"), + input = @JinjavaParam(value = "value", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is truthy %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java index 45eacc899..f3f6f3c8c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUndefinedExpTest.java @@ -7,7 +7,7 @@ @JinjavaDoc( value = "Return true if object is undefined", - input = @JinjavaParam(value = "value", type = "object"), + input = @JinjavaParam(value = "value", type = "object", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is undefined %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java index 20dda09d8..6b04fbda4 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsUpperExpTest.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Return true if string is all uppercased", - input = @JinjavaParam(value = "value", type = "string"), + input = @JinjavaParam(value = "value", type = "string", required = true), snippets = { @JinjavaSnippet( code = "{% if variable is upper %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java index 16675fbfb..e45c9a7c8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsWithinExpTest.java @@ -11,8 +11,8 @@ @JinjavaDoc( value = "Returns true if a value is within a list", - input = @JinjavaParam(value = "value", type="object"), - params = @JinjavaParam(value = "list", type="list", desc = "A list to check if the value is in."), + input = @JinjavaParam(value = "value", type = "object", required = true), + params = @JinjavaParam(value = "list", type = "list", desc = "A list to check if the value is in.", required = true), snippets = { @JinjavaSnippet( code = "{{ 2 is within [1, 2, 3] }}") 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 c29cc53fb..9643b4613 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java @@ -27,7 +27,7 @@ @JinjavaDoc( value = "Return the absolute value of the argument.", - input = @JinjavaParam(value = "number", type = "number", desc = "The number that you want to get the absolute value of"), + input = @JinjavaParam(value = "number", type = "number", desc = "The number that you want to get the absolute value of", required = true), snippets = { @JinjavaSnippet( code = "{% set my_number = -53 %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/AbstractSetFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/AbstractSetFilter.java index 10a784cff..318464014 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AbstractSetFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AbstractSetFilter.java @@ -3,13 +3,19 @@ import java.util.LinkedHashSet; import java.util.Set; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.TemplateSyntaxException; import com.hubspot.jinjava.util.ForLoop; import com.hubspot.jinjava.util.ObjectIterator; public abstract class AbstractSetFilter implements AdvancedFilter { - protected Object parseArgs(Object[] args) { - return args.length > 0 ? args[0] : null; + protected Object parseArgs(JinjavaInterpreter interpreter, Object[] args) { + if (args.length < 1) { + throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (a list to perform set function)"); + } + + return args[0]; } protected Set objectToSet(Object var) { 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 0e57657f6..57379d0e5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java @@ -28,9 +28,9 @@ @JinjavaDoc( value = "adds a number to the existing value", - input = @JinjavaParam(value = "number", type = "number", desc = "Number or numeric variable to add to"), + input = @JinjavaParam(value = "number", type = "number", desc = "Number or numeric variable to add to", required = true), params = { - @JinjavaParam(value = "addend", type = "number", desc = "The number added to the base number") + @JinjavaParam(value = "addend", type = "number", desc = "The number added to the base number", required = true) }, snippets = { @JinjavaSnippet( @@ -40,13 +40,13 @@ public class AddFilter implements Filter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { if (object == null) { return null; } - if (arg.length != 1) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to add to base)"); } @@ -57,15 +57,15 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar throw new InvalidInputException(interpreter, this, InvalidReason.NUMBER_FORMAT, object.toString()); } - if (arg[0] == null) { + if (args[0] == null) { return base; } BigDecimal addend; try { - addend = new BigDecimal(arg[0]); + addend = new BigDecimal(args[0]); } catch (NumberFormatException e) { - throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, arg[0]); + throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0]); } return base.add(addend); 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 9276dc270..0673400ce 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java @@ -8,9 +8,9 @@ @JinjavaDoc( value = "Renders the attribute of a dictionary", - input = @JinjavaParam(value = "obj", desc = "The dictionary containing the attribute"), + input = @JinjavaParam(value = "obj", desc = "The dictionary containing the attribute", required = true), params = { - @JinjavaParam(value = "name", desc = "The dictionary attribute name to access") + @JinjavaParam(value = "name", desc = "The dictionary attribute name to access", required = true) }, snippets = { @JinjavaSnippet( @@ -27,7 +27,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (args.length != 1) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attribute name to use)"); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java index e3d8947f0..6fdf46cbd 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BaseDateFilter.java @@ -18,7 +18,7 @@ public abstract class BaseDateFilter implements AdvancedFilter { protected long parseDiffAmount(JinjavaInterpreter interpreter, Object... args) { - if (args.length != 2) { + if (args.length < 2) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (diff amount) and 1 string (diff unit) argument"); } @@ -36,7 +36,7 @@ protected long parseDiffAmount(JinjavaInterpreter interpreter, Object... args) { protected ChronoUnit parseChronoUnit(JinjavaInterpreter interpreter, Object... args) { - if (args.length != 2) { + if (args.length < 2) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (diff amount) and 1 string (diff unit) argument"); } 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 13d118a58..61c4dfc2f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java @@ -15,7 +15,7 @@ @JinjavaDoc( value = "A filter that groups up items within a sequence", - input = @JinjavaParam(value = "value", desc = "The sequence or dict that the filter is applied to"), + input = @JinjavaParam(value = "value", desc = "The sequence or dict that the filter is applied to", required = true), params = { @JinjavaParam(value = "linecount", type = "number", desc = "Number of items to include in the batch", defaultValue = "0"), @JinjavaParam(value = "fill_with", desc = "Value used to fill up missing items") diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java index 87f542a36..7eb46b16b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BetweenTimesFilter.java @@ -21,10 +21,10 @@ */ @JinjavaDoc( value = "Calculates the time between two datetime objects", - input = @JinjavaParam(value = "begin", desc = "Datetime object or timestamp at the beginning of the period"), + input = @JinjavaParam(value = "begin", desc = "Datetime object or timestamp at the beginning of the period", required = true), params = { - @JinjavaParam(value = "end", desc = "Datetime object or timestamp at the end of the period"), - @JinjavaParam(value = "unit", desc = "Which temporal unit to use"), + @JinjavaParam(value = "end", desc = "Datetime object or timestamp at the end of the period", required = true), + @JinjavaParam(value = "unit", desc = "Which temporal unit to use", required = true), }, snippets = { @JinjavaSnippet(code = "{% begin|between_times(end, 'hours') %}"), @@ -34,7 +34,7 @@ public class BetweenTimesFilter extends BaseDateFilter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { - if (args.length != 2) { + if (args.length < 2) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 datetime (end date) and 1 string (diff unit) argument"); } 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 7fe44defa..f7716b085 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/BoolFilter.java @@ -25,7 +25,7 @@ */ @JinjavaDoc( value = "Convert value into a boolean.", - input = @JinjavaParam(value = "value", desc = "The value to convert to a boolean"), + input = @JinjavaParam(value = "value", desc = "The value to convert to a boolean", required = true), snippets = { @JinjavaSnippet( desc = "This example converts a text string value to a boolean", 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 a654f18c6..aa2cde927 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CapitalizeFilter.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Capitalize a value. The first character will be uppercase, all others lowercase.", - input = @JinjavaParam(value = "string", desc = "String to capitalize the first letter of"), + input = @JinjavaParam(value = "string", desc = "String to capitalize the first letter of", required = true), snippets = { @JinjavaSnippet( code = "{% set sentence = \"the first letter of a sentence should always be capitalized.\" %}\n" + 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 1fc336bb2..8dbeeb5dd 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CenterFilter.java @@ -10,7 +10,7 @@ @JinjavaDoc( value = "Uses whitespace to center the value in a field of a given width.", - input = @JinjavaParam(value = "value", desc = "Value to center"), + input = @JinjavaParam(value = "value", desc = "Value to center", required = true), params = { @JinjavaParam(value = "width", type = "number", defaultValue = "80", desc = "Width of field to center value in") }, 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 7ebe152e3..78ac7985e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java @@ -27,9 +27,9 @@ @JinjavaDoc( value = "Removes a string from the value from another string", - input = @JinjavaParam(value = "value", desc = "The original string"), + input = @JinjavaParam(value = "value", desc = "The original string", required = true), params = { - @JinjavaParam(value = "to_remove", desc = "String to remove from the original string") + @JinjavaParam(value = "to_remove", desc = "String to remove from the original string", required = true) }, snippets = { @JinjavaSnippet( @@ -41,7 +41,7 @@ public class CutFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { - if (arg.length != 1) { + if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (string to remove from target)"); } String cutee = arg[0]; 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 86445b66a..06303243f 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Formats a date object", - input = @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable or UNIX timestamp to format"), + input = @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable or UNIX timestamp to format", required = true), params = { @JinjavaParam(value = "format", defaultValue = StrftimeFormatter.DEFAULT_DATE_FORMAT, desc = "The format of the date determined by the directives added to this parameter"), @JinjavaParam(value = "timezone", defaultValue = "utc", desc = "Time zone of output date") 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 f4aca6507..55fb86f2a 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java @@ -26,9 +26,9 @@ @JinjavaDoc( value = "If the value is undefined it will return the passed default value, otherwise the value of the variable", - input = @JinjavaParam(value = "value", desc = "The variable or value to test"), + input = @JinjavaParam(value = "value", desc = "The variable or value to test", required = true), params = { - @JinjavaParam(value = "default_value", desc = "Value to print when variable is not defined"), + @JinjavaParam(value = "default_value", desc = "Value to print when variable is not defined", required = true), @JinjavaParam(value = "boolean", type = "boolean", defaultValue = "False", desc = "Set to True to use with variables which evaluate to false") }, snippets = { @@ -45,11 +45,11 @@ public class DefaultFilter implements Filter { public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { boolean truthy = false; - if (args.length != 1 && args.length != 2) { - throw new TemplateSyntaxException(interpreter, getName(), "requires either 1 (default value to use) or 2 (default value to use, ) arguments"); + 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 == 2) { + if (args.length > 2) { truthy = BooleanUtils.toBoolean(args[1]); } 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 864fe003a..4bc07b071 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DictSortFilter.java @@ -16,7 +16,7 @@ @JinjavaDoc( value = "Sort a dict and yield (key, value) pairs.", - input = @JinjavaParam(value = "value", desc = "Dict to sort"), + input = @JinjavaParam(value = "value", desc = "Dict to sort", required = true), params = { @JinjavaParam(value = "case_sensitive", type = "boolean", defaultValue = "False", desc = "Determines whether or not the sorting is case sensitive"), @JinjavaParam(value = "by", type = "enum key|value", defaultValue = "key", desc = "Sort by dict key or value") diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java index c02de9c22..81c7429f8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DifferenceFilter.java @@ -11,9 +11,9 @@ @JinjavaDoc( value = "Returns a list containing elements present in the first list but not the second list", - input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list", required = true), params = { - @JinjavaParam(value = "list", type = "sequence", desc = "The second list") + @JinjavaParam(value = "list", type = "sequence", desc = "The second list", required = true) }, snippets = { @JinjavaSnippet( @@ -23,7 +23,7 @@ public class DifferenceFilter extends AbstractSetFilter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { - return new ArrayList<>(Sets.difference(objectToSet(var), objectToSet(parseArgs(args)))); + return new ArrayList<>(Sets.difference(objectToSet(var), objectToSet(parseArgs(interpreter, args)))); } @Override 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 4f61c7782..04c8e46c3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java @@ -29,9 +29,9 @@ @JinjavaDoc( value = "Divides the current value by a divisor", - input = @JinjavaParam(value = "value", type = "number", desc = "The numerator to be divided"), + input = @JinjavaParam(value = "value", type = "number", desc = "The numerator to be divided", required = true), params = { - @JinjavaParam(value = "divisor", type = "number", desc = "The divisor to divide the value") + @JinjavaParam(value = "divisor", type = "number", desc = "The divisor to divide the value", required = true) }, snippets = { @JinjavaSnippet( @@ -42,7 +42,7 @@ public class DivideFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { - if (arg.length != 1) { + if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 number (divisor) argument"); } String toMul = arg[0]; 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 7fa4d8f67..6ef1c0400 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DivisibleFilter.java @@ -23,9 +23,9 @@ @JinjavaDoc( value = "Evaluates to true if the value is divisible by the given number", - input = @JinjavaParam(value = "value", type = "number", desc = "The value to be divided"), + input = @JinjavaParam(value = "value", type = "number", desc = "The value to be divided", required = true), params = { - @JinjavaParam(value = "divisor", type = "number", desc = "The divisor to check if the value is divisible by") + @JinjavaParam(value = "divisor", type = "number", desc = "The divisor to check if the value is divisible by", required = true) }, snippets = { @JinjavaSnippet( @@ -43,7 +43,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar return false; } if (object instanceof Number) { - if (arg.length != 1) { + if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to divide by)"); } long factor = Long.parseLong(arg[0]); 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 6212441f3..198514ee1 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeFilter.java @@ -28,7 +28,7 @@ value = "Converts the characters &, <, >, ‘, and ” in string s to HTML-safe sequences. " + "Use this filter if you need to display text that might contain such characters in HTML. " + "Marks return value as markup string.", - input = @JinjavaParam(value = "s", desc = "String to escape"), + input = @JinjavaParam(value = "s", desc = "String to escape", required = true), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"
This markup is printed as text
\" %}\n" + 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 a74f147f4..d8bb71b02 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJinjavaFilter.java @@ -29,7 +29,7 @@ value = "Converts the characters { and } in string s to Jinjava-safe sequences. " + "Use this filter if you need to display text that might contain such characters in Jinjava. " + "Marks return value as markup string.", - input = @JinjavaParam(value = "s", desc = "String to escape"), + input = @JinjavaParam(value = "s", desc = "String to escape", required = true), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"{{This markup is printed as text}}\" %}\n" + 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 482ebbc8f..6e386aedf 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsFilter.java @@ -26,7 +26,7 @@ @JinjavaDoc( value = "Escapes strings so that they can be safely inserted into a JavaScript variable declaration", - input = @JinjavaParam(value = "s", desc = "String to escape"), + input = @JinjavaParam(value = "s", desc = "String to escape", required = true), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"This string can safely be inserted into JavaScript\" %}\n" + 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 c892a302e..f0fd72997 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/EscapeJsonFilter.java @@ -11,7 +11,7 @@ @JinjavaDoc( value = "Escapes strings so that they can be used as JSON values", - input = @JinjavaParam(value = "s", desc = "String to escape"), + input = @JinjavaParam(value = "s", desc = "String to escape", required = true), snippets = { @JinjavaSnippet( code = "{{String that contains JavaScript|escapejson}}" 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 c3aa553ed..53e897854 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FileSizeFormatFilter.java @@ -12,7 +12,7 @@ @JinjavaDoc( value = "Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc).", - input = @JinjavaParam(value = "value", desc = "The value to convert to filesize format"), + input = @JinjavaParam(value = "value", desc = "The value to convert to filesize format", required = true), params = { @JinjavaParam(value = "binary", type = "boolean", defaultValue = "False", desc = "Use binary prefixes (Mebi, Gibi)") }, 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 02cf843e4..247faf1ee 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FirstFilter.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Return the first item of a sequence.", - input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return first item from"), + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return first item from", required = true), snippets = { @JinjavaSnippet( code = "{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}\n" + 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 f3f55c914..f6bc10209 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FloatFilter.java @@ -13,7 +13,7 @@ @JinjavaDoc( value = "Convert the value into a floating point number.", - input = @JinjavaParam(value = "value", desc = "Value to convert to a float"), + input = @JinjavaParam(value = "value", desc = "Value to convert to a float", required = true), params = { @JinjavaParam(value = "default", type = "float", defaultValue = "0.0", desc = "Value to return if conversion fails") }, 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 1e5279af1..ca1bbe351 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ForceEscapeFilter.java @@ -11,7 +11,7 @@ @JinjavaDoc( value = "Enforce HTML escaping. This will probably double escape variables.", - input = @JinjavaParam(value = "value", desc = "Value to escape"), + input = @JinjavaParam(value = "value", desc = "Value to escape", required = true), snippets = { @JinjavaSnippet( code = "{% set escape_string = \"
This markup is printed as text
\" %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java index 88c1a768f..f85a80c67 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FormatFilter.java @@ -10,7 +10,7 @@ @JinjavaDoc( value = "Apply Python string formatting to an object.", - input = @JinjavaParam(value = "value", desc = "String value to reformat"), + input = @JinjavaParam(value = "value", desc = "String value to reformat", required = true), params = { @JinjavaParam(value = "args", type = "String...", desc = "Values to insert into string") }, 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 ccdde5203..25c4f5f6d 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FromJsonFilter.java @@ -13,7 +13,7 @@ @JinjavaDoc( value = "Converts JSON string to Object", - input = @JinjavaParam(value = "string", desc = "JSON String to write to object"), + input = @JinjavaParam(value = "string", desc = "JSON String to write to object", required = true), snippets = { @JinjavaSnippet( code = "{{object|fromJson}}" 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 2495e31a0..58f85fd74 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java @@ -19,9 +19,9 @@ @JinjavaDoc( value = "Group a sequence of objects by a common attribute.", - input = @JinjavaParam(value = "value", desc = "The dict to iterate through and group by a common attribute"), + input = @JinjavaParam(value = "value", desc = "The dict to iterate through and group by a common attribute", required = true), params = { - @JinjavaParam(value = "attribute", desc = "The common attribute to group by") + @JinjavaParam(value = "attribute", desc = "The common attribute to group by", required = true) }, snippets = { @JinjavaSnippet( @@ -45,7 +45,7 @@ public String getName() { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (args.length != 1) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attr name to group by)"); } 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 135ba392f..7892ff18e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IndentFilter.java @@ -17,7 +17,7 @@ @JinjavaDoc( value = "Uses whitespace to indent a string.", - input = @JinjavaParam(value = "string", desc = "The string to indent"), + input = @JinjavaParam(value = "string", desc = "The string to indent", required = true), params = { @JinjavaParam(value = "width", type = "number", defaultValue = "4", desc = "Amount of whitespace to indent"), @JinjavaParam(value = "indentfirst", type = "boolean", defaultValue = "False", desc = "If True, first line will be indented") 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 c5eef572c..4cc37b25b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IntFilter.java @@ -16,7 +16,7 @@ */ @JinjavaDoc( value = "Convert the value into an integer.", - input = @JinjavaParam(value = "value", desc = "The value to convert to an integer"), + input = @JinjavaParam(value = "value", desc = "The value to convert to an integer", required = true), params = { @JinjavaParam(value = "default", type = "number", defaultValue = "0", desc = "Value to return if the conversion fails") }, diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java index 836573f5c..1d3961a30 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IntersectFilter.java @@ -11,9 +11,9 @@ @JinjavaDoc( value = "Returns a list containing elements present in both lists", - input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list", required = true), params = { - @JinjavaParam(value = "list", type = "sequence", desc = "The second list") + @JinjavaParam(value = "list", type = "sequence", desc = "The second list", required = true) }, snippets = { @JinjavaSnippet( @@ -23,7 +23,7 @@ public class IntersectFilter extends AbstractSetFilter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { - return new ArrayList<>(Sets.intersection(objectToSet(var), objectToSet(parseArgs(args)))); + return new ArrayList<>(Sets.intersection(objectToSet(var), objectToSet(parseArgs(interpreter, args)))); } @Override 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 138ba1f70..c32a0be49 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -11,9 +11,9 @@ @JinjavaDoc( value = "Evaluates to true if the value is a valid IPv4 or IPv6 address", - input = @JinjavaParam(value = "value", type = "string", desc = "String to check IP Address"), + input = @JinjavaParam(value = "value", type = "string", desc = "String to check IP Address", required = true), params = { - @JinjavaParam(value = "function", type = "string", desc = "Optional name of function. Supported functions: 'prefix'"), + @JinjavaParam(value = "function", type = "string", defaultValue = "prefix", desc = "Name of function. Supported functions: 'prefix'"), }, snippets = { @JinjavaSnippet( 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 9827fed02..d2a8c93f5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/JoinFilter.java @@ -17,9 +17,9 @@ @JinjavaDoc( value = "Return a string which is the concatenation of the strings in the sequence.", - input = @JinjavaParam(value = "value", desc = "The values to join"), + input = @JinjavaParam(value = "value", desc = "The values to join", required = true), params = { - @JinjavaParam(value = "d", desc = "The separator string used to join the items"), + @JinjavaParam(value = "d", desc = "The separator string used to join the items", defaultValue = "(empty String)"), @JinjavaParam(value = "attr", desc = "Optional dict object attribute to use in joining") }, snippets = { 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 0f9093dc7..01a3ec215 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LastFilter.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Return the last item of a sequence", - input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return last item from"), + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return last item from", required = true), snippets = { @JinjavaSnippet( code = "{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}\n" + 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 3cbe4b400..f020ef9ac 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LengthFilter.java @@ -27,7 +27,7 @@ @JinjavaDoc( value = "Return the number of items of a sequence or mapping", - input = @JinjavaParam(value = "object", desc = "The sequence to count"), + input = @JinjavaParam(value = "object", desc = "The sequence to count", required = true), snippets = { @JinjavaSnippet( code = "{% set services = ['Web design', 'SEO', 'Inbound Marketing', 'PPC'] %}\n" + 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 d373f1b01..bd11da32c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ListFilter.java @@ -12,7 +12,7 @@ @JinjavaDoc( value = "Convert the value into a list. If it was a string the returned list will be a list of characters.", - input = @JinjavaParam(value = "value", desc = "Value to add to a sequence"), + input = @JinjavaParam(value = "value", desc = "Value to add to a sequence", required = true), snippets = { @JinjavaSnippet( code = "{% set one = 1 %}\n" + 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 392c6cee7..b75d7f0a6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/LowerFilter.java @@ -22,7 +22,7 @@ @JinjavaDoc( value = "Convert a value to lowercase", - input = @JinjavaParam(value = "s", desc = "String to make lowercase"), + input = @JinjavaParam(value = "s", desc = "String to make lowercase", required = true), snippets = { @JinjavaSnippet(code = "{{ \"Text to MAKE Lowercase\"|lowercase }}") }) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java index ebfca9fd1..730e99873 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MapFilter.java @@ -13,9 +13,9 @@ @JinjavaDoc( value = "Applies a filter on a sequence of objects or looks up an attribute.", - input = @JinjavaParam(value = "value", type = "object", desc = "Sequence to apply filter or dict to lookup attribute"), + input = @JinjavaParam(value = "value", type = "object", desc = "Sequence to apply filter or dict to lookup attribute", required = true), params = { - @JinjavaParam(value = "attribute", desc = "Filter to apply to an object or dict attribute to lookup") + @JinjavaParam(value = "attribute", desc = "Filter to apply to an object or dict attribute to lookup", required = true) }, snippets = { @JinjavaSnippet( @@ -37,7 +37,7 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { ForLoop loop = ObjectIterator.getLoop(var); - if (args.length == 0) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (name of filter or attribute to apply to given sequence)"); } 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 b75b3e623..157391a35 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/Md5Filter.java @@ -28,7 +28,7 @@ @JinjavaDoc( value = "Calculates the md5 hash of the given object", - input = @JinjavaParam(value = "value", desc = "Value to get MD5 hash of"), + input = @JinjavaParam(value = "value", desc = "Value to get MD5 hash of", required = true), snippets = { @JinjavaSnippet(code = "{{ content.absolute_url|md5 }}") }) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java index a0644df5c..488337337 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MinusTimeFilter.java @@ -17,10 +17,10 @@ */ @JinjavaDoc( value = "Subtracts a specified amount of time to a datetime object", - input = @JinjavaParam(value = "var", desc = "Datetime object or timestamp"), + input = @JinjavaParam(value = "var", desc = "Datetime object or timestamp", required = true), params = { - @JinjavaParam(value = "diff", desc = "The amount to subtract from the datetime"), - @JinjavaParam(value = "unit", desc = "Which temporal unit to use"), + @JinjavaParam(value = "diff", desc = "The amount to subtract from the datetime", required = true), + @JinjavaParam(value = "unit", desc = "Which temporal unit to use", required = true), }, snippets = { @JinjavaSnippet(code = "{% mydatetime|minus_time(3, 'days') %}"), 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 6413edc08..72777a7ac 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/MultiplyFilter.java @@ -29,9 +29,9 @@ @JinjavaDoc( value = "Multiplies the current object with the given multiplier", - input = @JinjavaParam(value = "value", type = "number", desc = "Base number to be multiplied"), + input = @JinjavaParam(value = "value", type = "number", desc = "Base number to be multiplied", required = true), params = { - @JinjavaParam(value = "multiplier", type = "number", desc = "The multiplier") + @JinjavaParam(value = "multiplier", type = "number", desc = "The multiplier", required = true) }, snippets = { @JinjavaSnippet( @@ -43,7 +43,7 @@ public class MultiplyFilter implements Filter { @Override public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { - if (arg.length != 1) { + if (arg.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number to multiply by)"); } String toMul = arg[0]; diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java index b4d0b5f37..63da311da 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PlusTimeFilter.java @@ -17,10 +17,10 @@ */ @JinjavaDoc( value = "Adds a specified amount of time to a datetime object", - input = @JinjavaParam(value = "var", desc = "Datetime object or timestamp"), + input = @JinjavaParam(value = "var", desc = "Datetime object or timestamp", required = true), params = { - @JinjavaParam(value = "diff", desc = "The amount to add to the datetime"), - @JinjavaParam(value = "unit", desc = "Which temporal unit to use"), + @JinjavaParam(value = "diff", desc = "The amount to add to the datetime", required = true), + @JinjavaParam(value = "unit", desc = "Which temporal unit to use", required = true), }, snippets = { @JinjavaSnippet(code = "{% mydatetime|plus_time(3, 'days') %}"), 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 d9975d683..8f82ce9fe 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilter.java @@ -23,7 +23,7 @@ @JinjavaDoc( value = "Pretty print a variable. Useful for debugging.", - input = @JinjavaParam(value = "value", type = "object", desc = "Object to Pretty Print"), + input = @JinjavaParam(value = "value", type = "object", desc = "Object to Pretty Print", required = true), snippets = { @JinjavaSnippet( code = "{% set this_var =\"Variable that I want to debug\" %}\n" + 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 3d19f87f4..724b19a82 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RandomFilter.java @@ -28,7 +28,7 @@ @JinjavaDoc( value = "Return a random item from the sequence.", - input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return a random item from"), + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to return a random item from", required = true), snippets = { @JinjavaSnippet( desc = "The example below is a standard blog loop that returns a single random post.", 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 4deb28ffc..7cda384f8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RegexReplaceFilter.java @@ -16,10 +16,10 @@ value = "Return a copy of the value with all occurrences of a matched regular expression (Java RE2 syntax) " + "replaced with a new one. The first argument is the regular expression to be matched, the second " + "is the replacement string", - input = @JinjavaParam(value = "s", desc = "Base string to find and replace within"), + input = @JinjavaParam(value = "s", desc = "Base string to find and replace within", required = true), params = { - @JinjavaParam(value = "regex", desc = "The regular expression that you want to match and replace"), - @JinjavaParam(value = "new", desc = "The new string that you replace the matched substring") + @JinjavaParam(value = "regex", desc = "The regular expression that you want to match and replace", required = true), + @JinjavaParam(value = "new", desc = "The new string that you replace the matched substring", required = true) }, snippets = { @JinjavaSnippet( @@ -37,7 +37,7 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (args.length != 2) { + if (args.length < 2) { throw new TemplateSyntaxException(interpreter, getName(), "requires 2 arguments (regex string, replacement string)"); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java index cc7160ba9..391b89cdc 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RejectAttrFilter.java @@ -10,9 +10,9 @@ @JinjavaDoc( value = "Filters a sequence of objects by applying a test to an attribute of an object or the attribute and " + "rejecting the ones with the test succeeding.", - input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to test"), + input = @JinjavaParam(value = "seq", type = "sequence", desc = "Sequence to test", required = true), params = { - @JinjavaParam(value = "attribute", desc = "Attribute to test for and reject items that contain it"), + @JinjavaParam(value = "attribute", desc = "Attribute to test for and reject items that contain it", required = true), @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the rejection") }, snippets = { 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 385fe3a2c..da29d9c9e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java @@ -16,9 +16,9 @@ @JinjavaDoc( value = "Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.", - input = @JinjavaParam(value = "seq", type = "Sequence to test"), + input = @JinjavaParam(value = "seq", type = "Sequence to test", required = true), params = { - @JinjavaParam(value = "exp_test", type = "name of expression test", desc = "Specify which expression test to run for making the selection") + @JinjavaParam(value = "exp_test", type = "name of expression test", desc = "Specify which expression test to run for making the selection", required = true) }, snippets = { @JinjavaSnippet( @@ -36,7 +36,7 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { List result = new ArrayList<>(); - if (args.length == 0) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (name of expression test to filter by)"); } 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 7e7bfbdc6..b20eb9030 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ReplaceFilter.java @@ -13,10 +13,10 @@ value = "Return a copy of the value with all occurrences of a substring replaced with a new one. " + "The first argument is the substring that should be replaced, the second is the replacement " + "string. If the optional third argument count is given, only the first count occurrences are replaced", - input = @JinjavaParam(value = "s", desc = "Base string to find and replace within"), + input = @JinjavaParam(value = "s", desc = "Base string to find and replace within", required = true), params = { - @JinjavaParam(value = "old", desc = "The old substring that you want to match and replace"), - @JinjavaParam(value = "new", desc = "The new string that you replace the matched substring"), + @JinjavaParam(value = "old", desc = "The old substring that you want to match and replace", required = true), + @JinjavaParam(value = "new", desc = "The new string that you replace the matched substring", required = true), @JinjavaParam(value = "count", type = "number", desc = "Replace only the first N occurrences") }, snippets = { @@ -41,7 +41,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, if (var == null) { return null; } - if (args.length != 2 && args.length != 3) { + if (args.length < 2) { throw new TemplateSyntaxException(interpreter, getName(), "requires 2 arguments (substring to replace, replacement string) or 3 arguments (substring to replace, replacement string, number of occurrences to replace)"); 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 06742f4bd..cc01c40d5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ReverseFilter.java @@ -25,7 +25,7 @@ @JinjavaDoc( value = "Reverse the object or return an iterator the iterates over it the other way round.", - input = @JinjavaParam(value = "value", type = "object", desc = "The sequence or dict to reverse the iteration order"), + input = @JinjavaParam(value = "value", type = "object", desc = "The sequence or dict to reverse the iteration order", required = true), snippets = { @JinjavaSnippet( code = "{% set nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] %}\n" + 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 e8a0b5a0f..31f9f1508 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/RoundFilter.java @@ -14,9 +14,8 @@ @JinjavaDoc( value = "Round the number to a given precision.", - input = @JinjavaParam(value = "value", type = "number", desc = "The number to round"), + input = @JinjavaParam(value = "value", type = "number", desc = "The number to round", required = true), params = { - @JinjavaParam(value = "value", type = "number", desc = "The number to round"), @JinjavaParam(value = "precision", type = "number", defaultValue = "0", desc = "Specifies the precision of rounding"), @JinjavaParam(value = "method", type = "enum common|ceil|floor", defaultValue = "common", desc = "Method of rounding: 'common' rounds either up or down, 'ceil' always rounds up, and 'floor' always rounds down.") }, @@ -39,7 +38,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) return null; } - BigDecimal result = BigDecimal.ZERO; + BigDecimal result; try { result = new BigDecimal(var.toString()); } catch (NumberFormatException e) { 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 dc3a9369b..112be2a96 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SafeFilter.java @@ -13,7 +13,7 @@ */ @JinjavaDoc( value = "Mark the value as safe, which means that in an environment with automatic escaping enabled this variable will not be escaped.", - input = @JinjavaParam(value = "value", desc = "Value to mark as safe"), + input = @JinjavaParam(value = "value", desc = "Value to mark as safe", required = true), snippets = { @JinjavaSnippet(code = "{{ content.post_list_content|safe }}") }) diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java index b167df3ae..5cc921bc6 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SelectAttrFilter.java @@ -19,9 +19,9 @@ @JinjavaDoc( value = "Filters a sequence of objects by applying a test to an attribute of an object and only selecting the ones with the test succeeding.", - input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test"), + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test", required = true), params = { - @JinjavaParam(value = "attr", desc = "Attribute to test for and select items that contain it"), + @JinjavaParam(value = "attr", desc = "Attribute to test for and select items that contain it", required = true), @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the selection") }, snippets = { @@ -46,7 +46,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, protected Object applyFilter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs, boolean acceptObjects) { List result = new ArrayList<>(); - if (args.length == 0) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires at least 1 argument (attr to filter on)"); } diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java index f9c62dc65..6a17c41fa 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java @@ -18,9 +18,8 @@ @JinjavaDoc( value = "Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.", - input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test"), + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to test", required = true), params = { - @JinjavaParam(value = "value", type = "sequence"), @JinjavaParam(value = "exp_test", type = "name of expression test", defaultValue = "truthy", desc = "Specify which expression test to run for making the selection") }, snippets = { 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 fa94bd7a3..93b2650e5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ShuffleFilter.java @@ -12,7 +12,7 @@ @JinjavaDoc( value = "Randomly shuffle a given list, returning a new list with all of the items of the original list in a random order", - input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to shuffle"), + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to shuffle", required = true), snippets = { @JinjavaSnippet( desc = "The example below is a standard blog loop that's order is randomized on page load", 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 aa4b44c10..3b36e72cf 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SliceFilter.java @@ -13,9 +13,9 @@ @JinjavaDoc( value = "Slice an iterator and return a list of lists containing those items.", - input = @JinjavaParam(value = "value", type = "sequence", desc = "The sequence or dict that the filter is applied to"), + input = @JinjavaParam(value = "value", type = "sequence", desc = "The sequence or dict that the filter is applied to", required = true), params = { - @JinjavaParam(value = "slices", type = "number", desc = "Specifies how many items will be sliced"), + @JinjavaParam(value = "slices", type = "number", desc = "Specifies how many items will be sliced", required = true), }, snippets = { @JinjavaSnippet( @@ -42,7 +42,7 @@ public String getName() { public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { ForLoop loop = ObjectIterator.getLoop(var); - if (args.length == 0) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (number of 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 55d7302ab..4da3c2d43 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SortFilter.java @@ -15,7 +15,7 @@ @JinjavaDoc( value = "Sort an iterable.", - input = @JinjavaParam(value = "value", type = "iterable", desc = "The sequence or dict to sort through iteration"), + input = @JinjavaParam(value = "value", type = "iterable", desc = "The sequence or dict to sort through iteration", required = true), params = { @JinjavaParam(value = "reverse", type = "boolean", defaultValue = "False", desc = "Boolean to reverse the sort order"), @JinjavaParam(value = "case_sensitive", type = "boolean", defaultValue = "False", desc = "Determines whether or not the sorting is case sensitive"), 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 67cff29ab..1bce6b2f2 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SplitFilter.java @@ -23,7 +23,7 @@ */ @JinjavaDoc( value = "Splits the input string into a list on the given separator", - input = @JinjavaParam(value = "string", desc = "The string to split"), + input = @JinjavaParam(value = "string", desc = "The string to split", required = true), params = { @JinjavaParam(value = "separator", defaultValue = " ", desc = "Specifies the separator to split the variable by"), @JinjavaParam(value = "limit", type = "number", defaultValue = "0", desc = "Limits resulting list by putting remainder of string into last list item") @@ -53,7 +53,7 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) splitter = Splitter.on(args[0]); } else { - splitter = Splitter.on(CharMatcher.WHITESPACE); + splitter = Splitter.on(CharMatcher.whitespace()); } if (args.length > 1) { 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 cbb107b81..3abe18c45 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StringFilter.java @@ -9,7 +9,7 @@ @JinjavaDoc( value = "Returns string value of object", - input = @JinjavaParam(value = "value", desc = "The value to turn into a string"), + input = @JinjavaParam(value = "value", desc = "The value to turn into a string", required = true), snippets = { @JinjavaSnippet( code = "{% set number_to_string = 45 %}\n" + 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 65867c9c8..7a63188f3 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StringToTimeFilter.java @@ -11,9 +11,9 @@ @JinjavaDoc( value = "Converts a datetime string and datetime format to a datetime object", - input = @JinjavaParam(value = "datetimeString", desc = "Datetime string"), + input = @JinjavaParam(value = "datetimeString", desc = "Datetime string", required = true), params = { - @JinjavaParam(value = "datetimeFormat", desc = "Format of the datetime string"), + @JinjavaParam(value = "datetimeFormat", desc = "Format of the datetime string", required = true), }, snippets = { @JinjavaSnippet(code = "{% mydatetime|unixtimestamp %}"), @@ -23,7 +23,7 @@ public class StringToTimeFilter implements Filter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { - if (args.length != 1) { + if (args.length < 1) { throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (datetime format string)"); } 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 533b51118..09c2e09e7 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/StripTagsFilter.java @@ -14,7 +14,7 @@ */ @JinjavaDoc( value = "Strip SGML/XML tags and replace adjacent whitespace by one space.", - input = @JinjavaParam(value = "string", desc = "string to strip tags from"), + input = @JinjavaParam(value = "string", desc = "string to strip tags from", required = true), snippets = { @JinjavaSnippet( code = "{% set some_html = \"
Some text
\" %}\n" + diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java index 8d3d4f904..9e9457142 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SumFilter.java @@ -13,10 +13,10 @@ @JinjavaDoc( value = "Returns the sum of a sequence of numbers plus the value of parameter ‘start’ (which defaults to 0). When the sequence is empty it returns start.", - input = @JinjavaParam(value = "value", type = "iterable", desc = "Selects the sequence or dict to sum values from"), + input = @JinjavaParam(value = "value", type = "iterable", desc = "Selects the sequence or dict to sum values from", required = true), params = { + @JinjavaParam(value = "start", type = "number", defaultValue = "0", desc = "Sets a value to return, if there is nothing in the variable to sum"), @JinjavaParam(value = "attribute", desc = "Specify an optional attribute of dict to sum"), - @JinjavaParam(value = "start", type = "number", defaultValue = "0", desc = "Sets a value to return, if there is nothing in the variable to sum") }, snippets = { @JinjavaSnippet( diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java index 9d59c7ef5..3aee129f9 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/SymmetricDifferenceFilter.java @@ -11,9 +11,9 @@ @JinjavaDoc( value = "Returns a list containing elements present in only one list.", - input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list", required = true), params = { - @JinjavaParam(value = "list", type = "sequence", desc = "The second list") + @JinjavaParam(value = "list", type = "sequence", desc = "The second list", required = true) }, snippets = { @JinjavaSnippet( @@ -23,7 +23,7 @@ public class SymmetricDifferenceFilter extends AbstractSetFilter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { - return new ArrayList<>(Sets.symmetricDifference(objectToSet(var), objectToSet(parseArgs(args)))); + return new ArrayList<>(Sets.symmetricDifference(objectToSet(var), objectToSet(parseArgs(interpreter, args)))); } @Override 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 7dd46bf03..93063ece8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TitleFilter.java @@ -14,7 +14,7 @@ */ @JinjavaDoc( value = "Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.", - input = @JinjavaParam(value = "string", type = "string", desc = "the string to titlecase"), + input = @JinjavaParam(value = "string", type = "string", desc = "the string to titlecase", required = true), snippets = { @JinjavaSnippet( code = "{{ \"My title should be titlecase\"|title }} " 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 a074e20ca..34c38c306 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/ToJsonFilter.java @@ -12,7 +12,7 @@ @JinjavaDoc( value = "Writes object as a JSON string", - input = @JinjavaParam(value = "object", desc = "Object to write to JSON"), + input = @JinjavaParam(value = "object", desc = "Object to write to JSON", required = true), snippets = { @JinjavaSnippet( code = "{{object|tojson}}" 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 d3edbb8f3..b7a74d6e2 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TrimFilter.java @@ -14,7 +14,7 @@ */ @JinjavaDoc( value = "Strip leading and trailing whitespace.", - input = @JinjavaParam(value = "string", type = "string", desc = "the string to strip whitespace from"), + input = @JinjavaParam(value = "string", type = "string", desc = "the string to strip whitespace from", required = true), snippets = { @JinjavaSnippet( code = "{{ \" remove whitespace \"|trim }}") 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 22c26eec7..85548bd42 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateFilter.java @@ -26,7 +26,7 @@ "If the second parameter is true the filter will cut the text at length. Otherwise it will discard the last word. " + "If the text was in fact truncated it will append an ellipsis sign (\"...\"). If you want a different ellipsis sign " + "than \"...\" you can specify it using the third parameter.", - input = @JinjavaParam(value = "string", desc = "The string to truncate"), + input = @JinjavaParam(value = "string", desc = "The string to truncate", required = true), params = { @JinjavaParam(value = "length", type = "number", defaultValue = "255", desc = "Specifies the length at which to truncate the text (includes HTML characters)"), @JinjavaParam(value = "killwords", type = "boolean", defaultValue = "False", desc = "If true, the string will cut text at length"), 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 8d923dc03..0a336d4ee 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/TruncateHtmlFilter.java @@ -21,7 +21,7 @@ @JinjavaDoc( value = "Truncates a given string, respecting html markup (i.e. will properly close all nested tags)", - input = @JinjavaParam(value = "html", desc = "HTML to truncate"), + input = @JinjavaParam(value = "html", desc = "HTML to truncate", required = true), params = { @JinjavaParam(value = "length", type = "number", defaultValue = "255", desc = "Length at which to truncate text (HTML characters not included)"), @JinjavaParam(value = "end", defaultValue = "...", desc = "The characters that will be added to indicate where the text was truncated"), diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java index e9503d34e..c1a781ae8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UnionFilter.java @@ -11,9 +11,9 @@ @JinjavaDoc( value = "Returns a list containing elements present in either list", - input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list"), + input = @JinjavaParam(value = "value", type = "sequence", desc = "The first list", required = true), params = { - @JinjavaParam(value = "list", type = "sequence", desc = "The second list") + @JinjavaParam(value = "list", type = "sequence", desc = "The second list", required = true) }, snippets = { @JinjavaSnippet( @@ -23,7 +23,7 @@ public class UnionFilter extends AbstractSetFilter { @Override public Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { - return new ArrayList<>(Sets.union(objectToSet(var), objectToSet(parseArgs(args)))); + return new ArrayList<>(Sets.union(objectToSet(var), objectToSet(parseArgs(interpreter, args)))); } @Override 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 d21c9b053..0b5741309 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UniqueFilter.java @@ -12,7 +12,7 @@ @JinjavaDoc( value = "Extract a unique set from a sequence of objects", - input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to filter"), + input = @JinjavaParam(value = "sequence", type = "sequence", desc = "Sequence to filter", required = true), params = { @JinjavaParam(value = "attr", type = "Optional attribute on object to use as unique identifier") }, 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 c2eac829e..12dbf34fa 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UnixTimestampFilter.java @@ -8,7 +8,7 @@ @JinjavaDoc( value = "Gets the UNIX timestamp value (in milliseconds) of a date object", - input = @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable"), + input = @JinjavaParam(value = "value", defaultValue = "current time", desc = "The date variable", required = true), snippets = { @JinjavaSnippet(code = "{% mydatetime|unixtimestamp %}"), }) 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 5f7ccd417..c53fd1b9e 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UpperFilter.java @@ -22,7 +22,7 @@ @JinjavaDoc( value = "Convert a value to uppercase", - input = @JinjavaParam(value = "string", type = "string", desc = "the string to uppercase"), + input = @JinjavaParam(value = "string", type = "string", desc = "the string to uppercase", required = true), snippets = { @JinjavaSnippet(code = "{{ \"text to make uppercase\"|uppercase }}") }) 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 886933214..30eea05de 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UrlEncodeFilter.java @@ -16,7 +16,7 @@ @JinjavaDoc( value = "Escape strings for use in URLs (uses UTF-8 encoding). It accepts both dictionaries and regular strings as well as pairwise iterables.", - input = @JinjavaParam(value = "url", type = "string", desc = "the url to escape"), + input = @JinjavaParam(value = "url", type = "string", desc = "the url to escape", required = true), snippets = { @JinjavaSnippet(code = "{{ \"Escape & URL encode this string\"|urlencode }}") }) 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 a18f21a7c..8417084eb 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/UrlizeFilter.java @@ -15,7 +15,7 @@ @JinjavaDoc( value = "Converts URLs in plain text into clickable links.", - input = @JinjavaParam(value = "value", type="string", desc = "string URL to convert to an anchor"), + input = @JinjavaParam(value = "value", type="string", desc = "string URL to convert to an anchor", required = true), params = { @JinjavaParam(value = "trim_url_limit", type = "number", desc = "Sets a character limit"), @JinjavaParam(value = "nofollow", type = "boolean", defaultValue = "False", desc = "Adds nofollow to generated link tag"), 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 348527c2b..18f2dd85b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/WordCountFilter.java @@ -11,7 +11,7 @@ @JinjavaDoc( value = "Counts the words in the given string", - input = @JinjavaParam(value = "string", type = "string", desc = "string to count the words from"), + input = @JinjavaParam(value = "string", type = "string", desc = "string to count the words from", required = true), snippets = { @JinjavaSnippet( code = "{% set count_words = \"Count the number of words in this variable\" %}\n" + 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 3b150c35f..882355563 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/WordWrapFilter.java @@ -13,7 +13,7 @@ @JinjavaDoc( value = "Return a copy of the string passed to the filter wrapped after 79 characters.", - input = @JinjavaParam(value = "s", type = "string", desc = "String to wrap after a certain number of characters"), + input = @JinjavaParam(value = "s", type = "string", desc = "String to wrap after a certain number of characters", required = true), params = { @JinjavaParam(value = "width", type = "number", defaultValue = "79", desc = "Sets the width of spaces at which to wrap the text"), @JinjavaParam(value = "break_long_words", type = "boolean", defaultValue = "True", desc = "If true, long words will be broken when wrapped") 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 c7dfd767f..960fb150c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/XmlAttrFilter.java @@ -16,7 +16,7 @@ @JinjavaDoc( value = "Create an HTML/XML attribute string based on the items in a dict.", - input = @JinjavaParam(value = "dict", type = "dict", desc = "Dict to filter"), + input = @JinjavaParam(value = "dict", type = "dict", desc = "Dict to filter", required = true), params = { @JinjavaParam(value = "autospace", type = "boolean", defaultValue = "True", desc = "Automatically prepend a space in front of the item") }, diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java index 8b22e53a2..8799b12f8 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java @@ -63,7 +63,7 @@ public static List immutableListOf(Object... items) { } @JinjavaDoc(value = "return datetime of beginning of the day", params = { - @JinjavaParam(value = "timezone", type = "string", defaultValue = "utc", desc = "Optional timezone"), + @JinjavaParam(value = "timezone", type = "string", defaultValue = "utc", desc = "timezone"), }) public static ZonedDateTime today(String... var) { @@ -156,8 +156,8 @@ public static long unixtimestamp(Object var) { } @JinjavaDoc(value = "converts a string and datetime format into a datetime object", params = { - @JinjavaParam(value = "var", type = "datetimeString", defaultValue = "datetime as string"), - @JinjavaParam(value = "var", type = "datetimeFormat", defaultValue = "format of the datetime string") + @JinjavaParam(value = "var", type = "datetimeString", desc = "datetime as string"), + @JinjavaParam(value = "var", type = "datetimeFormat", desc = "format of the datetime string") }) public static PyishDate stringToTime(String datetimeString, String datetimeFormat) { From a0870226ad0a46b630f1a1905d77478753809e52 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Fri, 1 Mar 2019 14:18:03 -0500 Subject: [PATCH 6/6] better construction. --- .../interpret/InvalidArgumentException.java | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java index aabba0363..f42c76aeb 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java +++ b/src/main/java/com/hubspot/jinjava/interpret/InvalidArgumentException.java @@ -36,28 +36,17 @@ public String getName() { } private static String formatArgumentNumber(int argumentNumber) { - switch (argumentNumber){ - case 1: - return "1st"; - case 2: - return "2nd"; - case 3: - return "3rd"; - case 4: - return "4th"; - case 5: - return "5th"; - case 6: - return "6th"; - case 7: - return "7th"; - case 8: - return "8th"; - case 9: - return "9th"; - default: - return String.valueOf(argumentNumber); + String base = "th"; + int remainder = argumentNumber % 10; + if (remainder == 1) { + base = "st"; + } else if (remainder == 2) { + base = "nd"; + } else if (remainder == 3) { + base = "rd"; } + + return String.format("%d%s", argumentNumber, base); } }