Skip to content

Commit

Permalink
Map toString with keys in order.
Browse files Browse the repository at this point in the history
  • Loading branch information
hs-lsong committed Oct 25, 2023
1 parent fb624a1 commit 47afafe
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.objects.PyWrapper;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SizeLimitingPyMap extends PyMap implements PyWrapper {
private int maxSize;
Expand Down Expand Up @@ -39,6 +42,31 @@ public Object put(String s, Object o) {
return super.put(s, o);
}

@Override
public String toString() {
if (isEmpty()) {
return "{}";
}
List<Entry<String, Object>> list = entrySet()
.stream()
.sorted(Entry.comparingByKey())
.collect(Collectors.toList());
Iterator<Entry<String, Object>> i = list.iterator();
if (!i.hasNext()) return "{}";
StringBuilder sb = new StringBuilder();
sb.append('{');
for (;;) {
Entry<String, Object> e = i.next();
String key = e.getKey();
Object value = e.getValue();
sb.append(key);
sb.append('=');
sb.append(value == this ? "(this Map)" : value);
if (!i.hasNext()) return sb.append('}').toString();
sb.append(',').append(' ');
}
}

@Override
public void putAll(Map<? extends String, ?> m) {
if (m == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.annotations.Beta;
Expand Down Expand Up @@ -49,6 +51,12 @@ private static ObjectMapper getPyishObjectMapper() {
.addSerializer(PyishSerializable.class, PyishSerializer.INSTANCE)
);
mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());
mapper.setConfig(
mapper
.getSerializationConfig()
.with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
);
return mapper;
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.UnknownTokenException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;

public class ExpressionNodeTest extends BaseInterpretingTest {
Expand Down Expand Up @@ -210,6 +212,34 @@ public void itEscapesValueWhenContextSet() throws Exception {
assertThat(val("{{ a }}")).isEqualTo("foo &lt; bar");
}

@Test
public void itTestsMapSerializedInOrder() {
Jinjava jinjava = new Jinjava();
JinjavaInterpreter interpreter = jinjava.newInterpreter();
context = interpreter.getContext();
Map<String, String> fooMap = new HashMap<>();
fooMap.put("bbc", "b");
fooMap.put("abc", "a");
context.put("a", fooMap);

String output = new TreeParser(interpreter, "{{a}}")
.buildTree()
.getChildren()
.getFirst()
.render(interpreter)
.getValue();
assertThat(output).isEqualTo("{abc=a, bbc=b}");
}

@Test
public void itTestsMapSerializedInOrderWithLegacy() {
Map<String, String> fooMap = new HashMap<>();
fooMap.put("bbc", "b");
fooMap.put("abc", "a");
context.put("a", fooMap);
assertThat(val("{{ a }}")).isEqualTo("{'abc': 'a', 'bbc': 'b'}");
}

private String val(String jinja) {
return parse(jinja).render(interpreter).getValue();
}
Expand Down

0 comments on commit 47afafe

Please sign in to comment.