-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1031 from HubSpot/allow-snake-case
Add `allow_snake_case` filter and legacy override to serialize with snake_case
- Loading branch information
Showing
12 changed files
with
318 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/hubspot/jinjava/lib/filter/AllowSnakeCaseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
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.objects.collections.PyMap; | ||
import com.hubspot.jinjava.objects.collections.SizeLimitingPyMap; | ||
import com.hubspot.jinjava.objects.collections.SnakeCaseAccessibleMap; | ||
import java.util.Map; | ||
|
||
@JinjavaDoc( | ||
value = "Allow keys on the provided camelCase map to be accessed using snake_case", | ||
input = @JinjavaParam( | ||
value = "map", | ||
type = "dict", | ||
desc = "The dict to make keys accessible using snake_case", | ||
required = true | ||
), | ||
snippets = { @JinjavaSnippet(code = "{{ {'fooBar': 'baz'}|allow_snake_case }}") } | ||
) | ||
public class AllowSnakeCaseFilter implements Filter { | ||
public static final String NAME = "allow_snake_case"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { | ||
if (!(var instanceof Map)) { | ||
return var; | ||
} | ||
Map<String, Object> map = (Map<String, Object>) var; | ||
if (map instanceof PyMap) { | ||
map = ((PyMap) map).toMap(); | ||
} | ||
return new SnakeCaseAccessibleMap( | ||
new SizeLimitingPyMap(map, interpreter.getConfig().getMaxMapSize()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/hubspot/jinjava/objects/collections/SnakeCaseAccessibleMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.hubspot.jinjava.objects.collections; | ||
|
||
import com.google.common.base.CaseFormat; | ||
import com.hubspot.jinjava.lib.filter.AllowSnakeCaseFilter; | ||
import com.hubspot.jinjava.objects.serialization.PyishSerializable; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class SnakeCaseAccessibleMap extends PyMap implements PyishSerializable { | ||
|
||
public SnakeCaseAccessibleMap(Map<String, Object> map) { | ||
super(map); | ||
} | ||
|
||
@Override | ||
public Object get(Object key) { | ||
Object result = super.get(key); | ||
if (result == null && key instanceof String) { | ||
return getWithCamelCase((String) key); | ||
} | ||
return result; | ||
} | ||
|
||
private Object getWithCamelCase(String key) { | ||
if (key == null) { | ||
return null; | ||
} | ||
if (key.indexOf('_') == -1) { | ||
return null; | ||
} | ||
return super.get(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, key)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T extends Appendable & CharSequence> T appendPyishString(T appendable) | ||
throws IOException { | ||
return (T) appendable | ||
.append(PyishSerializable.writeValueAsString(toMap())) | ||
.append('|') | ||
.append(AllowSnakeCaseFilter.NAME); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/hubspot/jinjava/objects/serialization/BothCasingBeanSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.hubspot.jinjava.objects.serialization; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.hubspot.jinjava.lib.filter.AllowSnakeCaseFilter; | ||
import java.io.IOException; | ||
|
||
public class BothCasingBeanSerializer<T> extends JsonSerializer<T> { | ||
private final JsonSerializer<T> orignalSerializer; | ||
|
||
private BothCasingBeanSerializer(JsonSerializer<T> jsonSerializer) { | ||
this.orignalSerializer = jsonSerializer; | ||
} | ||
|
||
public static <T> BothCasingBeanSerializer<T> wrapping( | ||
JsonSerializer<T> jsonSerializer | ||
) { | ||
return new BothCasingBeanSerializer<>(jsonSerializer); | ||
} | ||
|
||
@Override | ||
public void serialize( | ||
T value, | ||
JsonGenerator gen, | ||
SerializerProvider serializerProvider | ||
) | ||
throws IOException { | ||
if ( | ||
Boolean.TRUE.equals( | ||
serializerProvider.getAttribute(PyishObjectMapper.ALLOW_SNAKE_CASE_ATTRIBUTE) | ||
) | ||
) { | ||
// if it's directly for output, then we don't want to add the additional filter characters, | ||
// as doing so would make the "|allow_snake_case" appear in the final output. | ||
StringBuilder sb = new StringBuilder(); | ||
sb | ||
.append(PyishSerializable.writeValueAsString(value)) | ||
.append('|') | ||
.append(AllowSnakeCaseFilter.NAME); | ||
gen.writeRawValue(sb.toString()); | ||
} else { | ||
orignalSerializer.serialize(value, gen, serializerProvider); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.