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

Add raw object to group in groupby filter. #218

Merged
merged 2 commits into from
Aug 10, 2018
Merged
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
20 changes: 17 additions & 3 deletions src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.hubspot.jinjava.lib.filter;

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

import com.google.common.collect.LinkedListMultimap;
Expand Down Expand Up @@ -50,36 +52,48 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args)

ForLoop loop = ObjectIterator.getLoop(var);
Multimap<String, Object> groupBuckets = LinkedListMultimap.create();
Map<String, Object> groupMapRaw = new HashMap<>();

while (loop.hasNext()) {
Object val = loop.next();

String grouper = Objects.toString(interpreter.resolveProperty(val, attr));
Object resolvedProperty = interpreter.resolveProperty(val, attr);
String grouper = Objects.toString(resolvedProperty);
groupBuckets.put(grouper, val);

if (!groupMapRaw.containsKey(grouper)) {
groupMapRaw.put(grouper, resolvedProperty);
}
}

List<Group> groups = new ArrayList<>();
for (String grouper : groupBuckets.keySet()) {
List<Object> list = Lists.newArrayList(groupBuckets.get(grouper));
groups.add(new Group(grouper, list));
groups.add(new Group(grouper, groupMapRaw.get(grouper), list));
}

return groups;
}

public static class Group {
private final String grouper;
private final Object grouperObject;
private final List<Object> list;

public Group(String grouper, List<Object> list) {
public Group(String grouper, Object grouperObject, List<Object> list) {
this.grouper = grouper;
this.grouperObject = grouperObject;
this.list = list;
}

public String getGrouper() {
return grouper;
}

public Object getGrouperObject() {
return grouperObject;
}

public List<Object> getList() {
return list;
}
Expand Down