martinzink commented on code in PR #1926:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1926#discussion_r1995256748


##########
extension-utils/src/utils/ProcessorConfigUtils.cpp:
##########
@@ -16,37 +16,93 @@
  */
 #include "utils/ProcessorConfigUtils.h"
 
-#include <vector>
 #include <string>
-#include <string_view>
 
-#include "range/v3/algorithm/contains.hpp"
-#include "utils/StringUtils.h"
+#include "utils/expected.h"
 
 namespace org::apache::nifi::minifi::utils {
 
-std::vector<std::string> listFromCommaSeparatedProperty(const 
core::ProcessContext& context, std::string_view property_name) {
-  std::string property_string;
-  context.getProperty(property_name, property_string);
-  return utils::string::splitAndTrim(property_string, ",");
+std::string parseProperty(const core::ProcessContext& ctx, const 
core::PropertyReference& property, const core::FlowFile* flow_file) {
+  return ctx.getProperty(property, flow_file) | 
utils::expect(fmt::format("Expected valid value from {}::{}", 
ctx.getProcessor().getName(), property.name));
 }
 
-std::vector<std::string> listFromRequiredCommaSeparatedProperty(const 
core::ProcessContext& context, std::string_view property_name) {
-  return utils::string::splitAndTrim(getRequiredPropertyOrThrow(context, 
property_name), ",");
+std::optional<std::string> parseOptionalProperty(const core::ProcessContext& 
ctx, const core::PropertyReference& property, const core::FlowFile* flow_file) {
+  if (const auto property_str = ctx.getProperty(property, flow_file)) {
+    return *property_str;
+  }
+  return std::nullopt;
+}
+
+std::optional<bool> parseOptionalBoolProperty(const core::ProcessContext& ctx, 
const core::PropertyReference& property, const core::FlowFile* flow_file) {
+  if (const auto property_str = ctx.getProperty(property, flow_file)) {
+    return parsing::parseBool(*property_str) | 
utils::expect(fmt::format("Expected parsable bool from {}::{}", 
ctx.getProcessor().getName(), property.name));
+  }
+  return std::nullopt;
+}
+
+bool parseBoolProperty(const core::ProcessContext& ctx, const 
core::PropertyReference& property, const core::FlowFile* flow_file) {
+  return ctx.getProperty(property, flow_file) | 
utils::andThen(parsing::parseBool) | utils::expect(fmt::format("Expected 
parsable bool from {}::{}", ctx.getProcessor().getName(), property.name));
+}
+
+std::optional<uint64_t> parseOptionalU64Property(const core::ProcessContext& 
ctx, const core::PropertyReference& property, const core::FlowFile* flow_file) {
+  if (const auto property_str = ctx.getProperty(property, flow_file)) {
+    if (property_str->empty()) {
+        return std::nullopt;
+    }
+    return parsing::parseIntegral<uint64_t>(*property_str) | 
utils::expect(fmt::format("Expected parsable uint64_t from {}::{}", 
ctx.getProcessor().getName(), property.name));
+  }
+
+  return std::nullopt;
+}
+
+uint64_t parseU64Property(const core::ProcessContext& ctx, const 
core::PropertyReference& property, const core::FlowFile* flow_file) {
+  return ctx.getProperty(property, flow_file) | 
utils::andThen(parsing::parseIntegral<uint64_t>) | 
utils::expect(fmt::format("Expected parsable uint64_t from {}::{}", 
ctx.getProcessor().getName(), property.name));
+}
+
+std::optional<int64_t> parseOptionalI64Property(const core::ProcessContext& 
ctx, const core::PropertyReference& property, const core::FlowFile* flow_file) {
+  if (const auto property_str = ctx.getProperty(property, flow_file)) {
+    if (property_str->empty()) {
+      return std::nullopt;
+    }
+    return parsing::parseIntegral<int64_t>(*property_str) | 
utils::expect(fmt::format("Expected parsable int64_t from {}::{}", 
ctx.getProcessor().getName(), property.name));
+  }
+
+  return std::nullopt;
 }
 
-bool parseBooleanPropertyOrThrow(const core::ProcessContext& context, 
std::string_view property_name) {
-  const std::string value_str = getRequiredPropertyOrThrow(context, 
property_name);
-  const auto maybe_value = utils::string::toBool(value_str);
-  if (!maybe_value) {
-    throw std::runtime_error(std::string(property_name) + " property is 
invalid: value is " + value_str);
+int64_t parseI64Property(const core::ProcessContext& ctx, const 
core::PropertyReference& property, const core::FlowFile* flow_file) {
+  return ctx.getProperty(property, flow_file) | 
utils::andThen(parsing::parseIntegral<int64_t>) | 
utils::expect(fmt::format("Expected parsable int64_t from {}::{}", 
ctx.getProcessor().getName(), property.name));
+}
+
+std::optional<std::chrono::milliseconds> parseOptionalMsProperty(const 
core::ProcessContext& ctx, const core::PropertyReference& property, const 
core::FlowFile* flow_file) {
+  if (const auto property_str = ctx.getProperty(property, flow_file)) {
+    if (property_str->empty()) {
+      return std::nullopt;
+    }
+    return parsing::parseDuration(*property_str) | 
utils::expect(fmt::format("Expected parsable duration from {}::{}", 
ctx.getProcessor().getName(), property.name));
   }
-  return maybe_value.value();
+
+  return std::nullopt;
 }
 
-std::chrono::milliseconds parseTimePropertyMSOrThrow(const 
core::ProcessContext& context, std::string_view property_name) {
-  const auto time_property = 
getRequiredPropertyOrThrow<core::TimePeriodValue>(context, property_name);
-  return time_property.getMilliseconds();
+std::chrono::milliseconds parseMsProperty(const core::ProcessContext& ctx, 
const core::PropertyReference& property, const core::FlowFile* flow_file) {

Review Comment:
   good idea, changed it in 
https://github.com/apache/nifi-minifi-cpp/pull/1926/commits/b08ea19f3a40f49e041ea5d96c8b8115abdea7ee



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