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

Changes DefaultFilter to implement AdvancedFilter #389

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
**********************************************************************/
package com.hubspot.jinjava.lib.filter;

import java.util.Map;
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 @@ -39,18 +43,18 @@
desc = "If you want to use default with variables that evaluate to false you have to set the second parameter to true",
code = "{{ ''|default('the string was empty', true) }}")
})
public class DefaultFilter implements Filter {
public class DefaultFilter implements AdvancedFilter {

@Override
public Object filter(Object object, JinjavaInterpreter interpreter, String... args) {
public Object filter(Object object, JinjavaInterpreter interpreter, Object[] args, Map<String, Object> kwargs) {
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 +65,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.hubspot.jinjava.lib.filter;

import java.util.HashMap;

import com.hubspot.jinjava.Jinjava;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Created by manishdevgan on 25/06/19.
*/

public class DefaultFilterTest {
Jinjava jinjava;

@Before
public void setup() {
jinjava = new Jinjava();
jinjava.getGlobalContext().registerClasses(DefaultFilter.class);
}

@Test
public void itSetsDefaultStringValues() {
assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ d }}", new HashMap<>())).isEqualTo("some random value"); System.out.println(jinjava.render("{% set d=d | default(\"some random value\") %}{{ d }}", new HashMap<>()));
}

@Test
public void itSetsDefaultObjectValue() {
assertThat(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Value = {{ d.key }}", new HashMap<>())).isEqualTo("Value = value"); System.out.println(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Value = {{ d.key }}", new HashMap<>()));
}

@Test
public void itChecksForType() {
assertThat(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Type = {{ type(d.key) }}", new HashMap<>()))
.isEqualTo("Type = str");
assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ type(d) }}", new HashMap<>()))
.isEqualTo("str");
System.out.println(jinjava.render("{% set d=d | default(\"some random value\") %}{{ type(d) }}", new HashMap<>()));
}
}