diff --git a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g index 071fda9a2a..6c0bcff19a 100644 --- a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g +++ b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g @@ -182,6 +182,7 @@ SUBSTRING : 'substring'; REPLACE : 'replace'; REPLACE_FIRST : 'replaceFirst'; REPLACE_ALL : 'replaceAll'; +IF_ELSE : 'ifElse'; // 4 arg functions GET_DELIMITED_FIELD : 'getDelimitedField'; diff --git a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g index 11cbec866a..576f0118cb 100644 --- a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g +++ b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g @@ -77,7 +77,7 @@ zeroArgString : (TO_UPPER | TO_LOWER | TRIM | TO_STRING | URL_ENCODE | URL_DECOD oneArgString : ((SUBSTRING_BEFORE | SUBSTRING_BEFORE_LAST | SUBSTRING_AFTER | SUBSTRING_AFTER_LAST | REPLACE_NULL | REPLACE_EMPTY | PREPEND | APPEND | FORMAT | STARTS_WITH | ENDS_WITH | CONTAINS | JOIN | JSON_PATH | FROM_RADIX) LPAREN! anyArg RPAREN!) | (TO_RADIX LPAREN! anyArg (COMMA! anyArg)? RPAREN!); -twoArgString : ((REPLACE | REPLACE_FIRST | REPLACE_ALL) LPAREN! anyArg COMMA! anyArg RPAREN!) | +twoArgString : ((REPLACE | REPLACE_FIRST | REPLACE_ALL | IF_ELSE) LPAREN! anyArg COMMA! anyArg RPAREN!) | (SUBSTRING LPAREN! anyArg (COMMA! anyArg)? RPAREN!); fiveArgString : GET_DELIMITED_FIELD LPAREN! anyArg (COMMA! anyArg (COMMA! anyArg (COMMA! anyArg (COMMA! anyArg)?)?)?)? RPAREN!; diff --git a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java index 06caf5361f..18396aac8e 100644 --- a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java +++ b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java @@ -54,6 +54,7 @@ import org.apache.nifi.attribute.expression.language.evaluation.functions.Greate import org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanOrEqualEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.functions.HostnameEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.functions.IPEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.functions.IfElseEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.functions.InEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.functions.IndexOfEvaluator; import org.apache.nifi.attribute.expression.language.evaluation.functions.IsEmptyEvaluator; @@ -126,6 +127,7 @@ import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.tree.Tree; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.FROM_RADIX; +import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.IF_ELSE; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.MATH; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.ALL_ATTRIBUTES; import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.ALL_DELINEATED_VALUES; @@ -1361,6 +1363,12 @@ public class Query { return addToken(new JsonPathEvaluator(toStringEvaluator(subjectEvaluator), toStringEvaluator(argEvaluators.get(0), "first argument to jsonPath")), "jsonPath"); } + case IF_ELSE: { + verifyArgCount(argEvaluators, 2, "ifElse"); + return addToken(new IfElseEvaluator(toBooleanEvaluator(subjectEvaluator), + toStringEvaluator(argEvaluators.get(0), "argument to return if true"), + toStringEvaluator(argEvaluators.get(1), "argument to return if false")), "ifElse"); + } default: throw new AttributeExpressionLanguageParsingException("Expected a Function-type expression but got " + tree.toString()); } diff --git a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IfElseEvaluator.java b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IfElseEvaluator.java new file mode 100644 index 0000000000..79bc4a592b --- /dev/null +++ b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IfElseEvaluator.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.nifi.attribute.expression.language.evaluation.functions; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +import java.util.Map; + +public class IfElseEvaluator extends StringEvaluator { + + private final Evaluator subject; + private final Evaluator trueEvaluator; + private final Evaluator falseEvaluator; + + public IfElseEvaluator(final Evaluator subject, final Evaluator trueEvaluator, final Evaluator falseEvaluator) { + this.subject = subject; + this.trueEvaluator = trueEvaluator; + this.falseEvaluator = falseEvaluator; + } + + @Override + public QueryResult evaluate(final Map attributes) { + final QueryResult subjectValue = subject.evaluate(attributes); + if (subjectValue == null) { + return new StringQueryResult(null); + } + final String ifElseValue = (Boolean.TRUE.equals(subjectValue.getValue())) ? trueEvaluator.evaluate(attributes).getValue() : falseEvaluator.evaluate(attributes).getValue(); + return new StringQueryResult(ifElseValue); + } + + @Override + public Evaluator getSubjectEvaluator() { + return subject; + } + +} diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java index b666b261d3..2dad9cae7a 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java @@ -1684,6 +1684,23 @@ public class TestQuery { verifyEquals("${string:unescapeHtml4()}", attributes, "special ♣"); } + @Test + public void testIfElse() { + final Map attributes = new HashMap<>(); + verifyEquals("${attr:isNull():ifElse('a', 'b')}", attributes, "a"); + verifyEquals("${attr:ifElse('a', 'b')}", attributes, "b"); + attributes.put("attr", "hello"); + verifyEquals("${attr:isNull():ifElse('a', 'b')}", attributes, "b"); + verifyEquals("${attr:ifElse('a', 'b')}", attributes, "b"); + attributes.put("attr", "true"); + verifyEquals("${attr:ifElse('a', 'b')}", attributes, "a"); + + verifyEquals("${attr2:isNull():ifElse('a', 'b')}", attributes, "a"); + verifyEquals("${literal(true):ifElse('a', 'b')}", attributes, "a"); + verifyEquals("${literal(true):ifElse(false, 'b')}", attributes, "false"); + + } + private void verifyEquals(final String expression, final Map attributes, final Object expectedResult) { verifyEquals(expression,attributes, null, expectedResult); } diff --git a/nifi-docs/src/main/asciidoc/expression-language-guide.adoc b/nifi-docs/src/main/asciidoc/expression-language-guide.adoc index 7af3be73f9..2b629e56a6 100644 --- a/nifi-docs/src/main/asciidoc/expression-language-guide.adoc +++ b/nifi-docs/src/main/asciidoc/expression-language-guide.adoc @@ -228,7 +228,7 @@ interpreted as literals. For example these two expressions are valid (without th One of the most powerful features of the Expression Language is the ability to compare an attribute value against some other value. This is used often, for example, to configure how a Processor should route data. The following functions are used for performing boolean logic, such as comparing two values. -Each of these functions returns a value of type Boolean. +Each of these functions are designed to work on values of type Boolean. [.function] @@ -480,6 +480,37 @@ ${filename:toLower():equals( ${filename} ):or( +[.function] +=== ifElse + +*Description*: [.description]#Evaluates the first argument if the Subject evaluates to true, or the second argument +if the Subject evaluates to false.# + +*Subject Type*: [.subject]#Boolean# + +*Arguments*: + + - [.argName]#_EvaluateIfTrue_# : [.argDesc]#The value to return if the Subject is true# + - [.argName]#_EvaluateIfFalse_# : [.argDesc]#The value to return if the Subject is false# + +*Return Type*: [.returnType]#String# + +*Examples*: If the "filename" attribute has the value "a brand new filename.txt", the "nullFilename" attribute has +the value null, and the "bool" attribute has the value "true", then the following expressions will provide +the following results: + + + +.ifElse Examples +|=================================================================== +| Expression | Value +| `${bool:ifElse('a','b')}` | `a` +| `${literal(true):ifElse('a','b')}` | `a` +| `${nullFilename:isNull():ifElse('file does not exist', 'located file')}` | `file does not exist` +| `${nullFilename:ifElse('found', 'not_found')}` | `not_found` +| `${filename:ifElse('found', 'not_found')}` | `not_found` +| `${filename:isNull():not():ifElse('found', 'not_found')}` | `found` +|=================================================================== @@ -963,10 +994,6 @@ Expressions will provide the following results: will return 0. - - - - [[encode]] == Encode/Decode Functions