-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 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
25 changes: 25 additions & 0 deletions
25
core/src/main/java/io/kestra/core/runners/pebble/filters/StringFilter.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,25 @@ | ||
package io.kestra.core.runners.pebble.filters; | ||
|
||
import io.pebbletemplates.pebble.error.PebbleException; | ||
import io.pebbletemplates.pebble.template.EvaluationContext; | ||
import io.pebbletemplates.pebble.template.PebbleTemplate; | ||
import io.pebbletemplates.pebble.extension.Filter; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class StringFilter implements Filter { | ||
|
||
@Override | ||
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException { | ||
if (input == null) return null; | ||
|
||
if (input instanceof String) return input; | ||
|
||
return input.toString(); | ||
} | ||
|
||
@Override | ||
public List<String> getArgumentNames() { | ||
return null; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/test/java/io/kestra/core/runners/pebble/filters/StringFilterTest.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,35 @@ | ||
package io.kestra.core.runners.pebble.filters; | ||
|
||
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.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@KestraTest | ||
public class StringFilterTest { | ||
|
||
@Inject | ||
VariableRenderer variableRenderer; | ||
|
||
static Stream<Arguments> source() { | ||
return Stream.of( | ||
Arguments.of("{{ 12.3 | string | className }}", String.class.getName()), | ||
Arguments.of("{{ {\"field\":\"hello\"} | string | className }}", String.class.getName()) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("source") | ||
void run(String exp, String expected) throws IllegalVariableEvaluationException { | ||
String render = variableRenderer.render(exp, Map.of()); | ||
assertThat(render, is(expected)); | ||
} | ||
} |