Skip to content

Commit

Permalink
fix(core): fix null coalesce, created undefined coalesce, allow empty…
Browse files Browse the repository at this point in the history
… string (#4401)

close #2396
close #4134
  • Loading branch information
Skraye authored Jul 23, 2024
1 parent 6ff7403 commit 0338f23
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kestra.core.runners.pebble;

import io.kestra.core.runners.pebble.expression.NullCoalescingExpression;
import io.kestra.core.runners.pebble.expression.UndefinedCoalescingExpression;
import io.kestra.core.runners.pebble.filters.*;
import io.kestra.core.runners.pebble.functions.*;
import io.kestra.core.runners.pebble.tests.JsonTest;
Expand Down Expand Up @@ -55,6 +56,7 @@ public List<BinaryOperator> getBinaryOperators() {
List<BinaryOperator> operators = new ArrayList<>();

operators.add(new BinaryOperatorImpl("??", 120, NullCoalescingExpression::new, NORMAL, Associativity.LEFT));
operators.add(new BinaryOperatorImpl("???", 120, UndefinedCoalescingExpression::new, NORMAL, Associativity.LEFT));

return operators;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public NullCoalescingExpression(Expression<?> left, Expression<?> right) {
@Override
public Object evaluate(PebbleTemplateImpl self, EvaluationContextImpl context) {
try {
return getLeftExpression().evaluate(self, context);
var result = getLeftExpression().evaluate(self, context);
if (result != null) {
return result;
} else {
return getRightExpression().evaluate(self, context);
}
} catch (AttributeNotFoundException e) {
return getRightExpression().evaluate(self, context);
}
Expand Down
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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

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;
Expand Down Expand Up @@ -77,4 +77,18 @@ void emptyObject() throws IllegalVariableEvaluationException {

assertThat(render, is("{}"));
}

@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("IS NULL"));

render = variableRenderer.render("{{ undefined ?? 'IS UNDEFINED' }}", vars);

assertThat(render, is("IS UNDEFINED"));
}
}
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"));
}
}
2 changes: 1 addition & 1 deletion ui/src/components/flows/FlowRun.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
purgeInputs(inputs){
for (let input in inputs) {
if (inputs[input] === undefined || inputs[input] === "") {
inputs[input] = null;
delete inputs[input];
}
}
return inputs;
Expand Down

0 comments on commit 0338f23

Please sign in to comment.