Skip to content

Commit

Permalink
feat(core): add a toIon Pebble filter
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jul 29, 2024
1 parent 9c2158c commit ac53596
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public Map<String, Filter> getFilters() {
filters.put("startsWith", new StartsWithFilter());
filters.put("endsWith", new EndsWithFilter());
filters.put("values", new ValuesFilter());
filters.put("toIon", new ToIonFilter());
return filters;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.kestra.core.runners.pebble.filters;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kestra.core.serializers.JacksonMapper;
import io.pebbletemplates.pebble.error.PebbleException;
import io.pebbletemplates.pebble.extension.Filter;
import io.pebbletemplates.pebble.template.EvaluationContext;
import io.pebbletemplates.pebble.template.PebbleTemplate;

import java.util.List;
import java.util.Map;

public class ToIonFilter implements Filter {
private static final ObjectMapper MAPPER = JacksonMapper.ofIon();

@Override
public List<String> getArgumentNames() {
return null;
}

@Override
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException {
if (input == null) {
return "null";
}

try {
return MAPPER.writeValueAsString(input);
} catch (JsonProcessingException e) {
throw new PebbleException(e, "Unable to transform to ion value '" + input + "' with type '" + input.getClass().getName() + "'", lineNumber, self.getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.kestra.core.runners.pebble.filters;

import com.google.common.collect.ImmutableMap;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.runners.VariableRenderer;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.*;

@KestraTest
class ToIonFilterTest {
@Inject
VariableRenderer variableRenderer;

@Test
void toIonFilter() throws IllegalVariableEvaluationException {
ZonedDateTime date = ZonedDateTime.parse("2013-09-08T16:19:00+02").withZoneSameLocal(ZoneId.systemDefault());

ImmutableMap<String, Object> vars = ImmutableMap.of(
"vars", ImmutableMap.of("second", Map.of(
"string", "string",
"int", 1,
"float", 1.123F,
"list", Arrays.asList(
"string",
1,
1.123F
),
"bool", true,
"date", date,
"map", Map.of(
"string", "string",
"int", 1,
"float", 1.123F
)
))
);

String render = variableRenderer.render("{{ vars.second.string | toIon }}", vars);
assertThat(render, is("\"string\""));

render = variableRenderer.render("{{ vars.second.int | toIon }}", vars);
assertThat(render, is("1"));

render = variableRenderer.render("{{ vars.second.float | toIon }}", vars);
assertThat(render, is("1.1230000257492065e0"));

render = variableRenderer.render("{{ vars.second.list | toIon }}", vars);
assertThat(render, is("[\"string\",1,1.1230000257492065e0]"));

render = variableRenderer.render("{{ vars.second.bool | toIon }}", vars);
assertThat(render, is("true"));

render = variableRenderer.render("{{ vars.second.date | toIon }}", vars);
assertThat(render, startsWith("ZonedDateTime::\"" + date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)));

render = variableRenderer.render("{{ vars.second.map | toIon }}", vars);
assertThat(render, containsString("int:1"));
assertThat(render, containsString("float:1.1230000257492065e0"));
assertThat(render, containsString("string:\"string\""));
assertThat(render, startsWith("{"));
assertThat(render, endsWith("}"));

render = variableRenderer.render("{{ {\"empty_object\":{}} | toIon }}", Map.of());
assertThat(render, is("{empty_object:{}}"));

render = variableRenderer.render("{{ null | toIon }}", Map.of());
assertThat(render, is("null"));
}

@Test
void exception() {
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ | toIon }}", Map.of()));

assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ {not: json} | toIon }}", Map.of()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ToJsonFilterTest {
VariableRenderer variableRenderer;

@Test
void jsonEncodeFilter() throws IllegalVariableEvaluationException {
void toJsonFilter() throws IllegalVariableEvaluationException {
ZonedDateTime date = ZonedDateTime.parse("2013-09-08T16:19:00+02").withZoneSameLocal(ZoneId.systemDefault());

ImmutableMap<String, Object> vars = ImmutableMap.of(
Expand Down Expand Up @@ -95,7 +95,7 @@ void jsonFilter() throws IllegalVariableEvaluationException {
))
);

String render = variableRenderer.render("{{ vars.second.string | toJson }}", vars);
String render = variableRenderer.render("{{ vars.second.string | json }}", vars);
assertThat(render, is("\"string\""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FromJsonFunctionTest {
VariableRenderer variableRenderer;

@Test
void jsonDecodeFunction() throws IllegalVariableEvaluationException {
void fronJsonFunction() throws IllegalVariableEvaluationException {
String render = variableRenderer.render("{{ fromJson('{\"test1\": 1, \"test2\": 2, \"test3\": 3}').test1 }}", Map.of());
assertThat(render, is("1"));

Expand Down

0 comments on commit ac53596

Please sign in to comment.