Repository: nifi-minifi-cpp Updated Branches: refs/heads/master 3d9fad569 -> c99b3fa49
MINIFICPP-415 Added implementation of matches() EL function This closes #270. Signed-off-by: Marc Parisi <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/commit/c99b3fa4 Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/tree/c99b3fa4 Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/diff/c99b3fa4 Branch: refs/heads/master Commit: c99b3fa494c15dcf1c264e7d67d729c8325ca0df Parents: 3d9fad5 Author: Andrew I. Christianson <[email protected]> Authored: Mon Feb 26 10:47:26 2018 -0500 Committer: Marc Parisi <[email protected]> Committed: Wed Feb 28 11:21:35 2018 -0500 ---------------------------------------------------------------------- EXPRESSIONS.md | 42 ++++++++++++++++++++ extensions/expression-language/Expression.cpp | 13 ++++++ .../ExpressionLanguageTests.cpp | 24 +++++++++++ 3 files changed, 79 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/c99b3fa4/EXPRESSIONS.md ---------------------------------------------------------------------- diff --git a/EXPRESSIONS.md b/EXPRESSIONS.md index a0a7df4..d46990e 100644 --- a/EXPRESSIONS.md +++ b/EXPRESSIONS.md @@ -173,6 +173,10 @@ token, filename. - [`fromRadix`](#fromradix) - [`random`](#random) +### Searching + +- [`matches`](#matches) + ## Planned Features ### String Manipulation @@ -183,6 +187,17 @@ token, filename. - `prepend` - `length` +### Searching + +- `startsWith` +- `endsWith` +- `contains` +- `in` +- `find` +- `indexOf` +- `lastIndexOf` +- `jsonPath` + ### Boolean Logic - `isNull` @@ -752,3 +767,30 @@ random number generator. **Return Type**: Number **Examples**: `${random():mod(10):plus(1)}` returns random number between 1 and 10 inclusive. + +## Searching + +### matches + +**Description**: Returns true if the Subject exactly matches the Regular Expression provided by the argument. + +**Subject Type**: String + +**Arguments**: + +| Argument | Description | +| - | - | +| Regex | The Regular Expression (in the Java Pattern syntax) to match against the Subject | + +**Return Type**: Boolean + +**Examples**: + +If the `filename` attribute has the value "a brand new filename.txt", then the +following Expressions will provide the following results: + +| Expression | Value | +| - | - | +| `${filename:matches('a.*txt')}` | true | +| `${filename:matches('brand')}` | false | +| `${filename:matches('.brand.')}` | true | http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/c99b3fa4/extensions/expression-language/Expression.cpp ---------------------------------------------------------------------- diff --git a/extensions/expression-language/Expression.cpp b/extensions/expression-language/Expression.cpp index de737ed..97bf340 100644 --- a/extensions/expression-language/Expression.cpp +++ b/extensions/expression-language/Expression.cpp @@ -152,6 +152,17 @@ std::string expr_replaceEmpty(const std::vector<std::string> &args) { return std::regex_replace(result, find, replace); } +std::string expr_matches(const std::vector<std::string> &args) { + const auto &subject = args[0]; + const std::regex expr = std::regex(args[1]); + + if (std::regex_match(subject.begin(), subject.end(), expr)) { + return "true"; + } else { + return "false"; + } +} + #endif // EXPRESSION_LANGUAGE_USE_REGEX std::string expr_binaryOp(const std::vector<std::string> &args, @@ -335,6 +346,8 @@ Expression make_dynamic_function(const std::string &function_name, return make_dynamic_function_incomplete<expr_replaceNull>(function_name, args, 1); } else if (function_name == "replaceEmpty") { return make_dynamic_function_incomplete<expr_replaceEmpty>(function_name, args, 1); + } else if (function_name == "matches") { + return make_dynamic_function_incomplete<expr_matches>(function_name, args, 1); #endif // EXPRESSION_LANGUAGE_USE_REGEX } else if (function_name == "plus") { return make_dynamic_function_incomplete<expr_plus>(function_name, args, 1); http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/c99b3fa4/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp ---------------------------------------------------------------------- diff --git a/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp b/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp index d6ab607..f0a9c7c 100644 --- a/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp +++ b/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp @@ -408,6 +408,30 @@ TEST_CASE("Replace Empty 3", "[expressionLanguageReplaceEmpty2]") { // NOLINT REQUIRE("abc" == expr({flow_file_a})); } +TEST_CASE("Matches", "[expressionLanguageMatches]") { // NOLINT + auto expr = expression::compile("${attr:matches('^(Ct|Bt|At):.*t$')}"); + + auto flow_file_a = std::make_shared<MockFlowFile>(); + flow_file_a->addAttribute("attr", "At:est"); + REQUIRE("true" == expr({flow_file_a})); +} + +TEST_CASE("Matches 2", "[expressionLanguageMatches2]") { // NOLINT + auto expr = expression::compile("${attr:matches('^(Ct|Bt|At):.*t$')}"); + + auto flow_file_a = std::make_shared<MockFlowFile>(); + flow_file_a->addAttribute("attr", "At:something"); + REQUIRE("false" == expr({flow_file_a})); +} + +TEST_CASE("Matches 3", "[expressionLanguageMatches3]") { // NOLINT + auto expr = expression::compile("${attr:matches('(Ct|Bt|At):.*t')}"); + + auto flow_file_a = std::make_shared<MockFlowFile>(); + flow_file_a->addAttribute("attr", " At:est"); + REQUIRE("false" == expr({flow_file_a})); +} + #endif // EXPRESSION_LANGUAGE_USE_REGEX TEST_CASE("Plus Integer", "[expressionLanguagePlusInteger]") { // NOLINT
