Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #159: Adds dict support for DefaultFilter #386

Merged
merged 5 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 @@
package com.hubspot.jinjava.lib.filter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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

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

List<String> stringArgs = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do this all in one loop. In fact, you don't need stringArgs at all. I know this was just moved from another file, but might as well clean it up while we're in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍

for (Object arg: args) {
stringArgs.add(Objects.toString(arg));
}
String[] filterArgs = new String[stringArgs.size()];
for (int i = 0; i < filterArgs.length; i++) {
filterArgs[i] = stringArgs.get(i);
}

if (args.length > 0) {
return Functions.dateTimeFormat(var, args);
return Functions.dateTimeFormat(var, filterArgs);
} else {
return Functions.dateTimeFormat(var);
}
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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only real significant change, right?

Copy link
Contributor Author

@gabru-md gabru-md Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. basically checking if the argument is an instance of PyWrapper then we do not need to convert it into String.

}

@Override
Expand Down
Loading