-
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.
fix(core): fix null coalesce, created undefined coalesce, allow empty…
- Loading branch information
Showing
6 changed files
with
90 additions
and
5 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
31 changes: 31 additions & 0 deletions
31
...src/main/java/io/kestra/core/runners/pebble/expression/UndefinedCoalescingExpression.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,31 @@ | ||
package io.kestra.core.runners.pebble.expression; | ||
|
||
import io.pebbletemplates.pebble.error.AttributeNotFoundException; | ||
import io.pebbletemplates.pebble.node.expression.BinaryExpression; | ||
import io.pebbletemplates.pebble.node.expression.Expression; | ||
import io.pebbletemplates.pebble.template.EvaluationContextImpl; | ||
import io.pebbletemplates.pebble.template.PebbleTemplateImpl; | ||
|
||
public class UndefinedCoalescingExpression extends BinaryExpression<Object> { | ||
public UndefinedCoalescingExpression() { | ||
} | ||
|
||
public UndefinedCoalescingExpression(Expression<?> left, Expression<?> right) { | ||
super(left, right); | ||
} | ||
|
||
@Override | ||
public Object evaluate(PebbleTemplateImpl self, EvaluationContextImpl context) { | ||
try { | ||
return getLeftExpression().evaluate(self, context); | ||
} catch (AttributeNotFoundException e) { | ||
return getRightExpression().evaluate(self, context); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s ?? %s", getLeftExpression(), getRightExpression()); | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
...test/java/io/kestra/core/runners/pebble/expression/UndefinedCoalescingExpressionTest.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,33 @@ | ||
package io.kestra.core.runners.pebble.expression; | ||
|
||
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.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@KestraTest | ||
class UndefinedCoalescingExpressionTest { | ||
@Inject | ||
VariableRenderer variableRenderer; | ||
|
||
@Test | ||
void nullOrUndefined() throws IllegalVariableEvaluationException { | ||
Map<String, Object> vars = new HashMap<>(); | ||
vars.put("null", null); | ||
|
||
String render = variableRenderer.render("{{ null ??? 'IS NULL' }}", vars); | ||
|
||
assertThat(render, is("")); | ||
|
||
render = variableRenderer.render("{{ undefined ??? 'IS UNDEFINED' }}", vars); | ||
|
||
assertThat(render, is("IS UNDEFINED")); | ||
} | ||
} |
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