Repository: nifi
Updated Branches:
  refs/heads/master 34627f78b -> 21ed55669


NIFI-3206: Add ifElse function to Expression Language

This closes #1333

Signed-off-by: jpercivall <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/21ed5566
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/21ed5566
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/21ed5566

Branch: refs/heads/master
Commit: 21ed556691719c592df89f36f0ccec26364e3997
Parents: 34627f7
Author: Matt Burgess <[email protected]>
Authored: Thu Dec 15 13:12:55 2016 -0500
Committer: jpercivall <[email protected]>
Committed: Fri Dec 16 15:13:01 2016 -0500

----------------------------------------------------------------------
 .../language/antlr/AttributeExpressionLexer.g   |  1 +
 .../language/antlr/AttributeExpressionParser.g  |  2 +-
 .../attribute/expression/language/Query.java    |  8 +++
 .../evaluation/functions/IfElseEvaluator.java   | 53 ++++++++++++++++++++
 .../expression/language/TestQuery.java          | 17 +++++++
 .../asciidoc/expression-language-guide.adoc     | 37 ++++++++++++--
 6 files changed, 112 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/21ed5566/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g
----------------------------------------------------------------------
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 071fda9..6c0bcff 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';

http://git-wip-us.apache.org/repos/asf/nifi/blob/21ed5566/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g
----------------------------------------------------------------------
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 11cbec8..576f011 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!;
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/21ed5566/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java
----------------------------------------------------------------------
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 06caf53..18396aa 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());
             }

http://git-wip-us.apache.org/repos/asf/nifi/blob/21ed5566/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IfElseEvaluator.java
----------------------------------------------------------------------
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 0000000..79bc4a5
--- /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<Boolean> subject;
+    private final Evaluator<String> trueEvaluator;
+    private final Evaluator<String> falseEvaluator;
+
+    public IfElseEvaluator(final Evaluator<Boolean> subject, final 
Evaluator<String> trueEvaluator, final Evaluator<String> falseEvaluator) {
+        this.subject = subject;
+        this.trueEvaluator = trueEvaluator;
+        this.falseEvaluator = falseEvaluator;
+    }
+
+    @Override
+    public QueryResult<String> evaluate(final Map<String, String> attributes) {
+        final QueryResult<Boolean> 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;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/nifi/blob/21ed5566/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java
----------------------------------------------------------------------
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 b666b26..2dad9ca 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<String, String> 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<String, 
String> attributes, final Object expectedResult) {
         verifyEquals(expression,attributes, null, expectedResult);
     }

http://git-wip-us.apache.org/repos/asf/nifi/blob/21ed5566/nifi-docs/src/main/asciidoc/expression-language-guide.adoc
----------------------------------------------------------------------
diff --git a/nifi-docs/src/main/asciidoc/expression-language-guide.adoc 
b/nifi-docs/src/main/asciidoc/expression-language-guide.adoc
index 7af3be7..2b629e5 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
 

Reply via email to