Skip to content

Commit

Permalink
#568: Support array of List in sort filter (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebussieres committed Jan 1, 2021
1 parent 4fc15f7 commit fe65d33
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
*/
package com.mitchellbosecke.pebble.extension.core;

import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.extension.Filter;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -29,7 +31,16 @@ public List<Comparable> apply(Object input, Map<String, Object> args, PebbleTemp
if (input == null) {
return null;
}
List<Comparable> collection = (List<Comparable>) input;

List<Comparable> collection;
if (input instanceof List) {
collection = (List<Comparable>) input;
} else if (input instanceof Comparable[]) {
collection = Arrays.asList((Comparable[]) input);
} else {
throw new PebbleException(null, "Unsupported input type for sort filter", lineNumber,
self.getName());
}
Collections.sort(collection);
return collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
*/
package com.mitchellbosecke.pebble;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import com.mitchellbosecke.pebble.error.ParserException;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.loader.StringLoader;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;

class ArraySyntaxTest {

Expand Down Expand Up @@ -146,15 +144,15 @@ void testArrayWithComplexExpressions() throws PebbleException, IOException {
Map<String, Object> context = new HashMap<>();
context.put("three", new Object() {

public Integer number = 3;
public final Integer number = 3;
});
context.put("numbers", new HashMap<String, Object>() {

{
this.put("four", new String[]{"4"});
this.put("five", new Object() {

private String value = "five";
private final String value = "five";

public String getValue() {
return this.value;
Expand Down Expand Up @@ -334,6 +332,20 @@ void testSortFilter2() throws PebbleException, IOException {
assertEquals("[0, 1, 2, 3]", writer.toString());
}

@Test
void testSortFilterFromArray() throws PebbleException, IOException {

PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
.strictVariables(false).build();

String source = "{{ 'q,g,s,c,w' | split(',') | sort }}";
PebbleTemplate template = pebble.getTemplate(source);

Writer writer = new StringWriter();
template.evaluate(writer, new HashMap<>());
assertEquals("[c, g, q, s, w]", writer.toString());
}

@Test
void testForTag() throws PebbleException, IOException {

Expand Down Expand Up @@ -804,7 +816,7 @@ void testProblematicSubscriptSyntax3() throws PebbleException, IOException {
{
this.put("first-name", new Object() {

private String name = "Bob";
private final String name = "Bob";

public String getName() {
return this.name;
Expand Down

0 comments on commit fe65d33

Please sign in to comment.