-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add a toIon Pebble filter
- Loading branch information
1 parent
9c2158c
commit ac53596
Showing
5 changed files
with
126 additions
and
3 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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/io/kestra/core/runners/pebble/filters/ToIonFilter.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,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()); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
core/src/test/java/io/kestra/core/runners/pebble/filters/ToIonFilterTest.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,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())); | ||
} | ||
} |
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