szaszm commented on code in PR #1692:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1692#discussion_r1452196886


##########
extensions/standard-processors/utils/JoltUtils.h:
##########
@@ -0,0 +1,205 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <string_view>
+#include <vector>
+#include <functional>
+#include <map>
+#include <unordered_map>
+#include <memory>
+#include <compare>
+
+#include "logging/Logger.h"
+#include "utils/gsl.h"
+#include "rapidjson/document.h"
+#include "utils/expected.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::utils::jolt {
+
+class Spec {
+ public:
+  using It = std::string_view::const_iterator;
+
+  struct Context {
+   public:
+    const Context* parent{nullptr};
+
+    std::string path() const {
+      std::string res;
+      if (parent) {
+        res = parent->path();
+      }
+      res.append("/").append(matches.at(0));
+      return res;
+    }
+
+    const Context* find(size_t idx) const {
+      if (idx == 0) return this;
+      if (parent) return parent->find(idx - 1);
+      return nullptr;
+    }
+
+    ::gsl::final_action<std::function<void()>> 
log(std::function<void(std::shared_ptr<core::logging::Logger>)> on_enter, 
std::function<void(std::shared_ptr<core::logging::Logger>)> on_exit) const {

Review Comment:
   Since this is all in the header anyway, this could be done without 
`std::function`, just using the function objects themselves directly, by making 
this a template.



##########
extensions/standard-processors/processors/JoltTransformJSON.h:
##########
@@ -0,0 +1,91 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+
+#include "core/Processor.h"
+#include "core/ProcessSession.h"
+#include "core/PropertyDefinition.h"
+#include "core/PropertyDefinitionBuilder.h"
+#include "utils/Enum.h"
+#include "utils/Searcher.h"
+#include "../utils/JoltUtils.h"
+
+namespace org::apache::nifi::minifi::processors::jolt_transform_json {
+enum class JoltTransform {
+  Shift
+};
+}  // namespace org::apache::nifi::minifi::processors::jolt_transform_json
+
+namespace org::apache::nifi::minifi::processors {
+
+class JoltTransformJSON : public core::Processor {
+ public:
+  explicit JoltTransformJSON(std::string_view name, const utils::Identifier& 
uuid = {})
+      : Processor(name, uuid) {}
+
+
+  EXTENSIONAPI static constexpr const char* Description = "Applies a list of 
Jolt specifications to the flowfile JSON payload. A new FlowFile is created "
+      "with transformed content and is routed to the 'success' relationship. 
If the JSON transform "
+      "fails, the original FlowFile is routed to the 'failure' relationship.";
+
+  EXTENSIONAPI static constexpr auto JoltTransform = 
core::PropertyDefinitionBuilder<magic_enum::enum_count<jolt_transform_json::JoltTransform>()>::createProperty("Jolt
 Transformation DSL")
+      .withDescription("Specifies the Jolt Transformation that should be used 
with the provided specification.")
+      
.withDefaultValue(magic_enum::enum_name(jolt_transform_json::JoltTransform::Shift))
+      
.withAllowedValues(magic_enum::enum_names<jolt_transform_json::JoltTransform>())
+      .isRequired(true)
+      .build();
+
+  EXTENSIONAPI static constexpr auto JoltSpecification = 
core::PropertyDefinitionBuilder<>::createProperty("Jolt Specification")
+      .withDescription("Jolt Specification for transformation of JSON data. 
The value for this property may be the text of a Jolt specification "
+          "or the path to a file containing a Jolt specification. 'Jolt 
Specification' must be set, or "
+          "the value is ignored if the Jolt Sort Transformation is selected.")
+      .supportsExpressionLanguage(true)

Review Comment:
   I don't think EL would work, since the spec member is populated in 
onSchedule, without flow file context. 



##########
extensions/standard-processors/utils/JoltUtils.h:
##########
@@ -0,0 +1,205 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <string_view>
+#include <vector>
+#include <functional>
+#include <map>
+#include <unordered_map>
+#include <memory>
+#include <compare>
+
+#include "logging/Logger.h"
+#include "utils/gsl.h"
+#include "rapidjson/document.h"
+#include "utils/expected.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::utils::jolt {
+
+class Spec {
+ public:
+  using It = std::string_view::const_iterator;
+
+  struct Context {
+   public:
+    const Context* parent{nullptr};
+
+    std::string path() const {
+      std::string res;
+      if (parent) {
+        res = parent->path();
+      }
+      res.append("/").append(matches.at(0));
+      return res;
+    }
+
+    const Context* find(size_t idx) const {
+      if (idx == 0) return this;
+      if (parent) return parent->find(idx - 1);
+      return nullptr;
+    }
+
+    ::gsl::final_action<std::function<void()>> 
log(std::function<void(std::shared_ptr<core::logging::Logger>)> on_enter, 
std::function<void(std::shared_ptr<core::logging::Logger>)> on_exit) const {
+      if (logger) {
+        on_enter(logger);
+        return gsl::finally<std::function<void()>>([on_exit, logger = logger] {
+          on_exit(logger);
+        });
+      }
+      if (parent) {
+        return parent->log(on_enter, on_exit);
+      }
+      return gsl::finally<std::function<void()>>([]{});
+    }
+
+    Context extend(std::vector<std::string_view> sub_matches, const 
rapidjson::Value* sub_node) const {
+      return {.parent = this, .matches = std::move(sub_matches), .node = 
sub_node, .match_count = 0, .logger = logger};
+    }
+
+    std::vector<std::string_view> matches;
+    const rapidjson::Value* node{nullptr};
+    size_t match_count{0};
+    std::shared_ptr<core::logging::Logger> logger;
+  };
+
+  class Template {
+   public:
+    Template(std::vector<std::string> frags, std::vector<std::pair<size_t, 
size_t>> refs) : fragments(std::move(frags)), references(std::move(refs)) {
+      gsl_Expects(fragments.size() == references.size() + 1);  // implies that 
fragments is non-empty
+      full = fragments.front();
+      for (size_t idx = 0; idx < references.size(); ++idx) {
+        full
+            .append("&(")
+            .append(std::to_string(references[idx].first))
+            .append(",")
+            .append(std::to_string(references[idx].second))
+            .append(")")
+            .append(fragments[idx + 1]);
+      }
+    }
+
+    // checks if the string is definitely a template (i.e. has an unescaped 
'&' char)
+    static bool check(std::string_view str);
+    static nonstd::expected<Template, std::string> parse(std::string_view str) 
{
+      if (auto res = parse(str.begin(), str.end())) {
+        if (res->second != str.end()) {
+          return nonstd::make_unexpected("Failed to fully parse template");
+        }
+        return {std::move(res->first)};
+      } else {
+        return nonstd::make_unexpected(std::move(res.error()));
+      }
+    }
+    static nonstd::expected<std::pair<Template, It>, std::string> parse(It 
begin, It end);
+
+    std::string eval(const Context& ctx) const;
+
+    auto operator<=>(const Template& other) const {
+      return full <=> other.full;
+    }
+
+    auto operator==(const Template& other) const {
+      return full == other.full;
+    }

Review Comment:
   Wouldn't this be automatically created from `operator<=>`?



-- 
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