Skip to content

Commit

Permalink
Fixes #159: Adds dict support for DefaultFilter (#386)
Browse files Browse the repository at this point in the history
Fixes #159: Adds dict support for DefaultFilter
  • Loading branch information
boulter authored Jan 17, 2020
2 parents 8e7a237 + ea62429 commit cd4bd32
Show file tree
Hide file tree
Showing 70 changed files with 212 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Filters {
*/
public static class OverrideDateFilter extends DatetimeFilter {
@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) {
return Functions.dateTimeFormat(ZonedDateTime.now(), arg);
}
}
Expand All @@ -45,7 +45,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return null;
}
}
Expand All @@ -57,7 +57,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
if(var == null) {
return "";
}
Expand All @@ -73,7 +73,7 @@ public String getName() {
return "money_with_currency";
}
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
Object val = super.filter(var, interpreter, args);
if(val.toString().length() == 0) {
return "";
Expand All @@ -89,7 +89,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
double grams = toDouble(Objects.toString(var));
return String.format("%.2f", grams / 1000);
}
Expand All @@ -102,7 +102,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return super.filter(var, interpreter, args) + " kg";
}
}
Expand All @@ -114,7 +114,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return "/files/1/[shop_id]/[shop_id]/assets/" + var;
}
}
Expand All @@ -126,7 +126,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return "/global/" + var;
}
}
Expand All @@ -138,7 +138,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return "/shopify/" + var;
}
}
Expand All @@ -150,7 +150,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return String.format("<script src=\"%s\" type=\"text/javascript\"></script>", var);
}
}
Expand All @@ -162,7 +162,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String media = "all";
if(args.length > 0) {
media = args[0];
Expand All @@ -178,7 +178,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String url = args[0];
String title = "";
if(args.length > 1) {
Expand All @@ -195,7 +195,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String alt = "";
if(args.length > 0) {
alt = args[0];
Expand All @@ -212,7 +212,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String label = Objects.toString(var);
String tag = args[0];

Expand All @@ -227,7 +227,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String tag = Objects.toString(var);
String cssClass = "active";
if(args.length > 0) {
Expand All @@ -251,7 +251,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String label = Objects.toString(var);
String tag = args[0];

Expand All @@ -270,7 +270,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
String label = Objects.toString(var);
String tag = args[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class AbsFilter implements Filter {

@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) {

if (object == null) {
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.hubspot.jinjava.lib.filter;

import java.math.BigDecimal;
import java.util.Objects;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
Expand All @@ -40,7 +41,7 @@
public class AddFilter implements Filter {

@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) {

if (object == null) {
return null;
Expand All @@ -63,7 +64,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar

BigDecimal addend;
try {
addend = new BigDecimal(args[0]);
addend = new BigDecimal(Objects.toString(args[0]));
} catch (NumberFormatException e) {
throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface AdvancedFilter extends Importable, Filter {
Object filter(Object var, JinjavaInterpreter interpreter, Object[] args, Map<String, Object> kwargs);

// Default implementation to maintain backward-compatibility with old Filters
default Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
default Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
return filter(var, interpreter, (Object[]) args, new HashMap<>());
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/hubspot/jinjava/lib/filter/AttrFilter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hubspot.jinjava.lib.filter;

import java.util.Objects;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
Expand All @@ -25,13 +27,13 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {

if (args.length < 1) {
throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (attribute name to use)");
}

return interpreter.resolveProperty(var, args[0]);
return interpreter.resolveProperty(var, Objects.toString(args[0]));
}

}
5 changes: 3 additions & 2 deletions src/main/java/com/hubspot/jinjava/lib/filter/BatchFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.math.NumberUtils;

Expand Down Expand Up @@ -53,12 +54,12 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {
if (var == null || args.length == 0) {
return Collections.emptyList();
}

int lineCount = NumberUtils.toInt(args[0], 0);
int lineCount = NumberUtils.toInt(Objects.toString(args[0]), 0);
if (lineCount == 0) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getName() {

@Override
public Object filter(Object var, JinjavaInterpreter interpreter,
String... args) {
Object... args) {
if (var == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {

if (var == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hubspot.jinjava.lib.filter;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

Expand Down Expand Up @@ -30,15 +32,15 @@ public String getName() {
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object var, JinjavaInterpreter interpreter, Object... args) {

if (var == null) {
return null;
}

int size = 80;
if (args.length > 0) {
size = NumberUtils.toInt(args[0], 80);
size = NumberUtils.toInt(Objects.toString(args[0]), 80);
}

return StringUtils.center(var.toString(), size);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/hubspot/jinjava/lib/filter/CutFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
public class CutFilter implements Filter {

@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) {

if (arg.length < 1) {
throw new TemplateSyntaxException(interpreter, getName(), "requires 1 argument (string to remove from target)");
}
String cutee = arg[0];
String cutee = Objects.toString(arg[0]);
String origin = Objects.toString(object, "");
return StringUtils.replace(origin, cutee, "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hubspot.jinjava.lib.filter;

import java.util.Objects;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
Expand Down Expand Up @@ -27,10 +29,15 @@ public String getName() {

@Override
public Object filter(Object var, JinjavaInterpreter interpreter,
String... args) {
Object... args) {

String[] filterArgs = new String[args.length];
for (int i = 0; i < filterArgs.length; i++) {
filterArgs[i] = Objects.toString(args[i]);
}

if (args.length > 0) {
return Functions.dateTimeFormat(var, args);
return Functions.dateTimeFormat(var, filterArgs);
} else {
return Functions.dateTimeFormat(var);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class DatetimeFilter extends DateTimeFormatFilter {

@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object... arg) {
return super.filter(object, interpreter, arg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
**********************************************************************/
package com.hubspot.jinjava.lib.filter;

import java.util.Objects;

import org.apache.commons.lang3.BooleanUtils;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.objects.PyWrapper;
import com.hubspot.jinjava.util.ObjectTruthValue;

@JinjavaDoc(
Expand All @@ -42,15 +45,15 @@
public class DefaultFilter implements Filter {

@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object... args) {
boolean truthy = false;

if (args.length < 1) {
throw new TemplateSyntaxException(interpreter, getName(), "requires either 1 (default value to use) or 2 (default value to use, default with variables that evaluate to false) arguments");
}

if (args.length > 1) {
truthy = BooleanUtils.toBoolean(args[1]);
truthy = BooleanUtils.toBoolean(Objects.toString(args[1]));
}

if (truthy) {
Expand All @@ -61,7 +64,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar
return object;
}

return args[0];
return (args[0] instanceof PyWrapper) ? args[0] : Objects.toString(args[0]);
}

@Override
Expand Down
Loading

0 comments on commit cd4bd32

Please sign in to comment.