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

toyaml/fromyaml filters, almost the same as tojson/fromjson using jackson #524

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

michaelpro1 marked this conversation as resolved.
Show resolved Hide resolved
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ protected void registerDefaults() {
WordCountFilter.class,
WordWrapFilter.class,
ToJsonFilter.class,
FromJsonFilter.class
FromJsonFilter.class,
ToYamlFilter.class,
FromYamlFilter.class
);
}

Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/filter/FromYamlFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.hubspot.jinjava.lib.filter;

import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
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.InvalidInputException;
import com.hubspot.jinjava.interpret.InvalidReason;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import java.io.IOException;

@JinjavaDoc(
value = "Converts YAML string to Object",
michaelpro1 marked this conversation as resolved.
Show resolved Hide resolved
input = @JinjavaParam(
value = "string",
desc = "YAML String to write to object",
michaelpro1 marked this conversation as resolved.
Show resolved Hide resolved
required = true
),
snippets = { @JinjavaSnippet(code = "{{object|fromYaml}}") }
)
public class FromYamlFilter implements Filter {
private static final YAMLMapper OBJECT_MAPPER = new YAMLMapper();

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
if (var == null) {
return null;
}

if (!(var instanceof String)) {
throw new InvalidInputException(interpreter, this, InvalidReason.STRING);
}
try {
return OBJECT_MAPPER.readValue((String) var, Object.class);
} catch (IOException e) {
throw new InvalidInputException(interpreter, this, InvalidReason.JSON_READ);
}
}

@Override
public String getName() {
return "fromyaml";
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/filter/ToYamlFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hubspot.jinjava.lib.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
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.InvalidInputException;
import com.hubspot.jinjava.interpret.InvalidReason;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;

@JinjavaDoc(
value = "Writes object as a YAML string",
input = @JinjavaParam(
value = "object",
desc = "Object to write to YAML",
required = true
),
snippets = { @JinjavaSnippet(code = "{{object|toyaml}}") }
)
public class ToYamlFilter implements Filter {
private static final YAMLMapper OBJECT_MAPPER = new YAMLMapper()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
try {
return OBJECT_MAPPER.writeValueAsString(var);
} catch (JsonProcessingException e) {
throw new InvalidInputException(interpreter, this, InvalidReason.JSON_WRITE);
}
}

@Override
public String getName() {
return "toyaml";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.hubspot.jinjava.lib.filter;

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

import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.BaseInterpretingTest;
import com.hubspot.jinjava.interpret.InvalidInputException;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;

public class FromYamlFilterTest extends BaseInterpretingTest {
private FromYamlFilter filter;

@Before
public void setup() {
filter = new FromYamlFilter();
}

@Test(expected = InvalidInputException.class)
public void itFailsWhenParameterIsNotString() {
Integer json = 456;

filter.filter(json, interpreter);
}

@Test(expected = InvalidInputException.class)
public void itFailsWhenYamlIsInvalid() {
String json = "a: b:";

filter.filter(json, interpreter);
}

@Test
public void itRendersTrivialYamlObject() {
String trivialYamlObject = "a: 100\n" + "b: 200";

Map<String, Object> vars = ImmutableMap.of("test", trivialYamlObject);
String template = "{% set obj = test | fromyaml %}{{ obj.a }} {{ obj.b }}";
String renderedJinjava = jinjava.render(template, vars);

assertThat(renderedJinjava).isEqualTo("100 200");
}

@Test
public void itRendersTrivialYamlArray() {
String trivialYamlArray = "- one\n" + "- two\n" + "- three";

Map<String, Object> vars = ImmutableMap.of("test", trivialYamlArray);
String template =
"{% set obj = test | fromyaml %}{{ obj[0] }} {{ obj[1] }} {{ obj[2] }}";
String renderedJinjava = jinjava.render(template, vars);

assertThat(renderedJinjava).isEqualTo("one two three");
}

@Test
public void itRendersNestedObjectYaml() {
String nestedObject =
"first: 1\n" + "nested:\n" + " second: string\n" + " third: 4";

Map<String, Object> vars = ImmutableMap.of("test", nestedObject);
String template =
"{% set obj = test | fromyaml %}{{ obj.first }} {{ obj.nested.second }} {{ obj.nested.third }}";
String renderedJinjava = jinjava.render(template, vars);

assertThat(renderedJinjava).isEqualTo("1 string 4");
}

@Test
public void itRendersNestedYamlWithArray() {
String nestedObjectWithArray =
"a:\n" + " b:\n" + " c:\n" + " - 1\n" + " - 2\n" + " - 3";

Map<String, Object> vars = ImmutableMap.of("test", nestedObjectWithArray);
String template = "{% set obj = test | fromyaml %}{{ obj.a.b.c }}";
String renderedJinjava = jinjava.render(template, vars);

assertThat(renderedJinjava).isEqualTo("[1, 2, 3]");
}

@Test
public void itRendersArrayOfObjects() {
String arrayOfObjects = "- a: 1\n" + "- a: 2\n" + "- a: 3";

Map<String, Object> vars = ImmutableMap.of("test", arrayOfObjects);
String template =
"{% set obj = test | fromyaml %}{{ obj[0].a }} {{ obj[1].a }} {{ obj[2].a }}";
String renderedJinjava = jinjava.render(template, vars);

assertThat(renderedJinjava).isEqualTo("1 2 3");
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/ToYamlFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.hubspot.jinjava.lib.filter;

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

import com.hubspot.jinjava.BaseInterpretingTest;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;

public class ToYamlFilterTest extends BaseInterpretingTest {
private ToYamlFilter filter;

@Before
public void setup() {
filter = new ToYamlFilter();
}

@Test
public void itWritesObjectAsString() {
int[] testArray = new int[] { 4, 1, 2 };
assertThat(filter.filter(testArray, interpreter))
.isEqualTo("- 4\n" + "- 1\n" + "- 2\n");

Map<String, Object> testMap = new HashMap<>();
testMap.put("testArray", testArray);
testMap.put("testString", "testString");
assertThat(filter.filter(testMap, interpreter))
.isEqualTo(
"testArray:\n" + "- 4\n" + "- 1\n" + "- 2\n" + "testString: \"testString\"\n"
);
}
}