Copilot commented on code in PR #2047:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2047#discussion_r2433085459


##########
libminifi/src/core/ProcessContextImpl.cpp:
##########
@@ -15,8 +15,9 @@
  * limitations under the License.
  */
 
-#include "core/ProcessContext.h"
+#include "core/ProcessContextImpl.h"
 #include "core/Processor.h"
+#include "utils/PropertyErrors.h"

Review Comment:
   gsl_Assert is used in this file (e.g., in getDynamicProperties) but the gsl 
header is not explicitly included, relying on transitive includes. Add the 
appropriate include to make the dependency explicit: #include 
\"minifi-cpp/utils/gsl.h\".
   ```suggestion
   #include "utils/PropertyErrors.h"
   #include "minifi-cpp/utils/gsl.h"
   ```



##########
libminifi/src/core/ProcessContextImpl.cpp:
##########
@@ -74,28 +75,72 @@ bool 
ProcessContextImpl::hasNonEmptyProperty(std::string_view name) const {
 
 std::vector<std::string> ProcessContextImpl::getDynamicPropertyKeys() const { 
return processor_.getDynamicPropertyKeys(); }
 
-std::map<std::string, std::string> 
ProcessContextImpl::getDynamicProperties(const FlowFile*) const { return 
processor_.getDynamicProperties(); }
+std::map<std::string, std::string> 
ProcessContextImpl::getDynamicProperties(const FlowFile* flow_file) const {
+  std::lock_guard<std::mutex> lock(mutex_);
+  auto dynamic_props = processor_.getDynamicProperties();
+  const expression::Parameters params{this, flow_file};
+  for (auto& [dynamic_property_name, dynamic_property_value]: dynamic_props) {
+    auto cached_dyn_expr_it = 
cached_dynamic_expressions_.find(dynamic_property_name);
+    if (cached_dyn_expr_it == cached_dynamic_expressions_.end()) {
+      auto expression = expression::compile(dynamic_property_value);
+      const auto [it, success] = 
cached_dynamic_expressions_.emplace(dynamic_property_name, expression);
+      gsl_Assert(success && "getDynamicProperties: no element with the key 
existed, yet insertion failed");
+      cached_dyn_expr_it = it;
+    }
+    auto& expression = cached_dyn_expr_it->second;
+    dynamic_property_value = expression(params).asString();
+  }
+  return dynamic_props;
+}
 
 bool ProcessContextImpl::isAutoTerminated(Relationship relationship) const { 
return processor_.isAutoTerminated(relationship); }
 
 uint8_t ProcessContextImpl::getMaxConcurrentTasks() const { return 
processor_.getMaxConcurrentTasks(); }
 
 void ProcessContextImpl::yield() { processor_.yield(); }
 
-nonstd::expected<std::string, std::error_code> 
ProcessContextImpl::getProperty(const std::string_view name, const FlowFile* 
const) const {
-  return getProcessor().getProperty(name);
+nonstd::expected<std::string, std::error_code> 
ProcessContextImpl::getProperty(const std::string_view name, const FlowFile* 
flow_file) const {
+  std::lock_guard<std::mutex> lock(mutex_);
+  const auto property = getProcessorInfo().getSupportedProperty(name);
+  if (!property) {
+    return nonstd::make_unexpected(PropertyErrorCode::NotSupportedProperty);
+  }
+
+  if (!property->supportsExpressionLanguage()) {
+    return getProcessor().getProperty(name);
+  }
+  if (!cached_expressions_.contains(name)) {
+    auto expression_str = getProcessor().getProperty(name);
+    if (!expression_str) { return expression_str; }
+    cached_expressions_.emplace(std::string{name}, 
expression::compile(*expression_str));
+  }
+  expression::Parameters p(this, flow_file);
+  auto result = cached_expressions_[std::string{name}](p).asString();
+  if (!property->getValidator().validate(result)) {
+    return nonstd::make_unexpected(PropertyErrorCode::ValidationFailed);
+  }
+  return result;
 }
 
 nonstd::expected<void, std::error_code> ProcessContextImpl::setProperty(const 
std::string_view name, std::string value) {
+  std::lock_guard<std::mutex> lock(mutex_);
+  cached_expressions_.erase(std::string{name});
   return getProcessor().setProperty(name, std::move(value));
 }
 
 nonstd::expected<void, std::error_code> 
ProcessContextImpl::clearProperty(const std::string_view name) {

Review Comment:
   clearProperty does not invalidate the compiled expression cache. If a 
property that supports EL is cleared, subsequent getProperty calls may still 
return a value evaluated from the stale cached Expression. Erase the cached 
entry under the same mutex before delegating to the processor.
   ```suggestion
   nonstd::expected<void, std::error_code> 
ProcessContextImpl::clearProperty(const std::string_view name) {
     std::lock_guard<std::mutex> lock(mutex_);
     cached_expressions_.erase(std::string{name});
   ```



##########
libminifi/CMakeLists.txt:
##########
@@ -32,15 +32,10 @@ set_cpp_version()
 
 include_directories(include)
 
-if(WIN32)
-    include_directories(opsys/win)
-    set(SOCKET_SOURCES "src/io/win/*.cpp")
-else()
-    include_directories(opsys/posix)
-    set(SOCKET_SOURCES "src/io/posix/*.cpp")
-endif()
+include(ExpressionLanguage)
 
 set(TLS_SOURCES "src/utils/tls/*.cpp" "src/io/tls/*.cpp")
+set(EL_GENERATED_SOURCES "${EL_GENERATED_INCLUDE_DIR}/*.cpp")

Review Comment:
   EL_GENERATED_SOURCES is defined but not used. Either remove this variable or 
use it (e.g., append to SOURCES) to avoid dead code in the build scripts.
   ```suggestion
   set(EL_GENERATED_SOURCES "${EL_GENERATED_INCLUDE_DIR}/*.cpp")
   list(APPEND SOURCES ${EL_GENERATED_SOURCES})
   ```



##########
extensions/standard-processors/tests/unit/ExpressionLanguageInDynamicPropertiesTests.cpp:
##########
@@ -0,0 +1,95 @@
+/**
+ *
+ * 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.
+ */
+#include <ExtractText.h>
+#include <GetFile.h>
+#include <PutFile.h>
+#include <UpdateAttribute.h>
+#include <LogAttribute.h>
+#include "unit/TestBase.h"
+#include "unit/Catch.h"
+#include "unit/ProvenanceTestHelper.h"
+
+namespace org::apache::nifi::minifi::test {
+
+TEST_CASE("GetFile PutFile dynamic attribute", 
"[expressionLanguageTestGetFilePutFileDynamicAttribute]") {
+  TestController testController;
+
+  LogTestController::getInstance().setTrace<TestPlan>();
+  LogTestController::getInstance().setTrace<minifi::processors::PutFile>();
+  LogTestController::getInstance().setTrace<minifi::processors::ExtractText>();
+  LogTestController::getInstance().setTrace<minifi::processors::GetFile>();
+  LogTestController::getInstance().setTrace<minifi::processors::PutFile>();
+  
LogTestController::getInstance().setTrace<minifi::processors::LogAttribute>();
+  
LogTestController::getInstance().setTrace<minifi::processors::UpdateAttribute>();
+
+  auto conf = std::make_shared<minifi::ConfigureImpl>();
+
+  conf->set("nifi.my.own.property", "custom_value");
+
+  auto plan = testController.createPlan(conf);
+  auto repo = std::make_shared<TestRepository>();

Review Comment:
   The local variable 'repo' is never used and can be removed to avoid an 
unused-variable warning.
   ```suggestion
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to