Skip to content

Commit

Permalink
Coalesce Function (#397)
Browse files Browse the repository at this point in the history
* Adds coalesce function
  • Loading branch information
andresmarenco authored Sep 21, 2023
1 parent 8d59bd2 commit 34846d2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/references/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Available through the _ExpressionConfiguration.StandardFunctionsDictionary_ cons
|--------------------------------------------|-------------------------------------------------------------------------------------------------------------------------|
| ABS(value) | Absolute (non-negative) value |
| CEILING(value) | Rounds the given value an integer using the rounding mode CEILING |
| COALESCE(value, ...) | Returns the first non-null parameter, or NULL if all parameters are null |
| FACT(base) | Calculates the factorial of a base value |
| FLOOR(value) | Rounds the given value an integer using the rounding mode FLOOR |
| IF(condition, resultIfTrue, resultIfFalse) | Conditional evaluation function. If _condition_ is true, the _resultIfTrue_ is returned, else the _resultIfFalse_ value |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public class ExpressionConfiguration {
// basic functions
Map.entry("ABS", new AbsFunction()),
Map.entry("CEILING", new CeilingFunction()),
Map.entry("COALESCE", new CoalesceFunction()),
Map.entry("FACT", new FactFunction()),
Map.entry("FLOOR", new FloorFunction()),
Map.entry("IF", new IfFunction()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2012-2022 Udo Klimaschewski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ezylang.evalex.functions.basic;

import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

/**
* Returns the first non-null parameter, or {@link EvaluationValue#nullValue()} if all parameters
* are null.
*/
@FunctionParameter(name = "value", isVarArg = true)
public class CoalesceFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
for (EvaluationValue parameter : parameterValues) {
if (!parameter.isNullValue()) {
return parameter;
}
}
return EvaluationValue.nullValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;
import com.ezylang.evalex.config.TestConfigurationProvider;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.parser.ParseException;
import com.ezylang.evalex.parser.Token;
Expand Down Expand Up @@ -320,4 +321,31 @@ void testLog10Zero() {
.isInstanceOf(EvaluationException.class)
.hasMessage("Parameter must not be zero");
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"COALESCE(null,1) : 1",
"COALESCE(null,\"abc\",1,null,2,3) : abc",
"COALESCE(1,null) : 1",
"COALESCE(null,null,null,null,1.1) : 1.1",
"COALESCE(null,null,null) :",
"COALESCE(null) :"
})
void testCoalesce(String expression, String expectedResult)
throws EvaluationException, ParseException {
EvaluationValue evaluationValue =
new Expression(
expression,
TestConfigurationProvider.StandardConfigurationWithAdditionalTestOperators)
.evaluate();

if (expectedResult == null) {
assertThat(evaluationValue.isNullValue()).isTrue();
} else {
assertThat(evaluationValue.isNullValue()).isFalse();
assertThat(evaluationValue.getStringValue()).isEqualTo(expectedResult);
}
}
}

0 comments on commit 34846d2

Please sign in to comment.