From dc29a28b632f164588b066959605666365cad8b5 Mon Sep 17 00:00:00 2001 From: "Andrew I. Christianson" Date: Mon, 11 Jun 2018 09:28:53 -0400 Subject: [PATCH] MINIFICPP-535 Add support for nonexistent flow_file provided as param to an Expression --- extensions/expression-language/Expression.cpp | 29 ++++++++++++++----- .../ExpressionLanguageTests.cpp | 6 ++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/extensions/expression-language/Expression.cpp b/extensions/expression-language/Expression.cpp index 80c9440f7b..5681c0507a 100644 --- a/extensions/expression-language/Expression.cpp +++ b/extensions/expression-language/Expression.cpp @@ -64,7 +64,8 @@ Expression make_dynamic(const std::function &sub_exprs) -> Value { std::string result; - if (params.flow_file.lock()->getAttribute(attribute_id, result)) { + const auto cur_flow_file = params.flow_file.lock(); + if (cur_flow_file && cur_flow_file->getAttribute(attribute_id, result)) { return Value(result); } else { return Value(); @@ -1776,7 +1777,9 @@ Expression make_allAttributes(const std::string &function_name, const std::vecto attr_id = arg(params).asString(); std::string attr_val; - if (params.flow_file.lock()->getAttribute(attr_id, attr_val)) { + const auto cur_flow_file = params.flow_file.lock(); + + if (cur_flow_file && cur_flow_file->getAttribute(attr_id, attr_val)) { return Value(attr_val); } else { return Value(); @@ -1825,7 +1828,9 @@ Expression make_anyAttribute(const std::string &function_name, const std::vector attr_id = arg(params).asString(); std::string attr_val; - if (params.flow_file.lock()->getAttribute(attr_id, attr_val)) { + const auto cur_flow_file = params.flow_file.lock(); + + if (cur_flow_file && cur_flow_file->getAttribute(attr_id, attr_val)) { return Value(attr_val); } else { return Value(); @@ -1871,7 +1876,12 @@ Expression make_allMatchingAttributes(const std::string &function_name, const st for (const auto &arg : args) { const std::regex attr_regex = std::regex(arg(params).asString()); - auto attrs = params.flow_file.lock()->getAttributes(); + const auto cur_flow_file = params.flow_file.lock(); + std::map attrs; + + if (cur_flow_file) { + attrs = cur_flow_file->getAttributes(); + } for (const auto &attr : attrs) { if (std::regex_match(attr.first.begin(), attr.first.end(), attr_regex)) { @@ -1879,7 +1889,7 @@ Expression make_allMatchingAttributes(const std::string &function_name, const st const std::vector &sub_exprs) -> Value { std::string attr_val; - if (params.flow_file.lock()->getAttribute(attr.first, attr_val)) { + if (cur_flow_file && cur_flow_file->getAttribute(attr.first, attr_val)) { return Value(attr_val); } else { return Value(); @@ -1925,7 +1935,12 @@ Expression make_anyMatchingAttribute(const std::string &function_name, const std for (const auto &arg : args) { const std::regex attr_regex = std::regex(arg(params).asString()); - auto attrs = params.flow_file.lock()->getAttributes(); + const auto cur_flow_file = params.flow_file.lock(); + std::map attrs; + + if (cur_flow_file) { + attrs = cur_flow_file->getAttributes(); + } for (const auto &attr : attrs) { if (std::regex_match(attr.first.begin(), attr.first.end(), attr_regex)) { @@ -1933,7 +1948,7 @@ Expression make_anyMatchingAttribute(const std::string &function_name, const std const std::vector &sub_exprs) -> Value { std::string attr_val; - if (params.flow_file.lock()->getAttribute(attr.first, attr_val)) { + if (cur_flow_file && cur_flow_file->getAttribute(attr.first, attr_val)) { return Value(attr_val); } else { return Value(); diff --git a/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp b/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp index f6e143eab2..4099636868 100644 --- a/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp +++ b/libminifi/test/expression-language-tests/ExpressionLanguageTests.cpp @@ -54,6 +54,12 @@ TEST_CASE("Attribute expression", "[expressionLanguageTestAttributeExpression]") REQUIRE("text_before__attr_value_a__text_after" == expr({flow_file}).asString()); } +TEST_CASE("Attribute expression (Null)", "[expressionLanguageTestAttributeExpressionNull]") { // NOLINT + auto expr = expression::compile("text_before${attr_a}text_after"); + std::shared_ptr flow_file = nullptr; + REQUIRE("text_beforetext_after" == expr({flow_file}).asString()); +} + TEST_CASE("Multi-attribute expression", "[expressionLanguageTestMultiAttributeExpression]") { // NOLINT auto flow_file = std::make_shared(); flow_file->addAttribute("attr_a", "__attr_value_a__");