szaszm commented on a change in pull request #940:
URL: https://github.com/apache/nifi-minifi-cpp/pull/940#discussion_r553477912
##########
File path: libminifi/src/utils/StringUtils.cpp
##########
@@ -46,8 +46,13 @@ std::string StringUtils::trim(const std::string& s) {
return trimRight(trimLeft(s));
}
-std::vector<std::string> StringUtils::split(const std::string &str, const
std::string &delimiter) {
+template<typename Fun>
+std::vector<std::string> split_transformed(const std::string& str, const
std::string& delimiter, Fun transformation) {
std::vector<std::string> result;
+ if (delimiter.empty()) {
+ std::transform(str.begin(), str.end(), std::back_inserter(result), []
(const char c) { return std::string{c}; });
Review comment:
This branch should apply `transformation` as well.
##########
File path: libminifi/include/utils/ProcessorConfigUtils.h
##########
@@ -0,0 +1,80 @@
+/**
+ * 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 <vector>
+#include <string>
+
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+std::string getRequiredPropertyOrThrow(const core::ProcessContext* context,
const std::string& property_name) {
+ std::string value;
+ if (!context->getProperty(property_name, value)) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property
missing or invalid");
+ }
+ return value;
+}
+
+std::vector<std::string> listFromCommaSeparatedProperty(const
core::ProcessContext* context, const std::string& property_name) {
+ std::string property_string;
+ context->getProperty(property_name, property_string);
+ return utils::StringUtils::splitAndTrim(property_string, ",");
+}
+
+std::vector<std::string> listFromRequiredCommaSeparatedProperty(const
core::ProcessContext* context, const std::string& property_name) {
+ return utils::StringUtils::splitAndTrim(getRequiredPropertyOrThrow(context,
property_name), ",");
+}
+
+bool parseBooleanPropertyOrThrow(core::ProcessContext* context, const
std::string& property_name) {
+ bool value;
+ std::string value_str = getRequiredPropertyOrThrow(context, property_name);
+ utils::optional<bool> maybe_value = utils::StringUtils::toBool(value_str);
+ if (!maybe_value) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property is
invalid: value is " + value_str);
Review comment:
Why are exceptions from these generic utilities categorized as
`PROCESS_SCHEDULE_EXCEPTION`? I think `GENERAL_EXCEPTION` is the closest.
##########
File path: libminifi/include/utils/ProcessorConfigUtils.h
##########
@@ -0,0 +1,80 @@
+/**
+ * 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 <vector>
+#include <string>
+
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+std::string getRequiredPropertyOrThrow(const core::ProcessContext* context,
const std::string& property_name) {
+ std::string value;
+ if (!context->getProperty(property_name, value)) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property
missing or invalid");
+ }
+ return value;
+}
+
+std::vector<std::string> listFromCommaSeparatedProperty(const
core::ProcessContext* context, const std::string& property_name) {
+ std::string property_string;
+ context->getProperty(property_name, property_string);
+ return utils::StringUtils::splitAndTrim(property_string, ",");
+}
+
+std::vector<std::string> listFromRequiredCommaSeparatedProperty(const
core::ProcessContext* context, const std::string& property_name) {
+ return utils::StringUtils::splitAndTrim(getRequiredPropertyOrThrow(context,
property_name), ",");
+}
+
+bool parseBooleanPropertyOrThrow(core::ProcessContext* context, const
std::string& property_name) {
+ bool value;
+ std::string value_str = getRequiredPropertyOrThrow(context, property_name);
+ utils::optional<bool> maybe_value = utils::StringUtils::toBool(value_str);
+ if (!maybe_value) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property is
invalid: value is " + value_str);
+ }
+ return maybe_value.value();
+}
+
+std::chrono::milliseconds parseTimePropertyMSOrThrow(core::ProcessContext*
context, const std::string& property_name) {
+ core::TimeUnit unit;
+ uint64_t time_value_ms;
+ if (!core::Property::StringToTime(getRequiredPropertyOrThrow(context,
property_name), time_value_ms, unit) ||
!core::Property::ConvertTimeUnitToMS(time_value_ms, unit, time_value_ms)) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property
missing or invalid");
Review comment:
If this `throw` statement is hit, then the property must be present,
because if it wasn't, then `getRequiredPropertyOrThrow` would throw instead.
I'd consider updating the message to reflect that.
##########
File path: libminifi/include/utils/ProcessorConfigUtils.h
##########
@@ -0,0 +1,80 @@
+/**
+ * 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 <vector>
+#include <string>
+
+#include "utils/StringUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+std::string getRequiredPropertyOrThrow(const core::ProcessContext* context,
const std::string& property_name) {
+ std::string value;
+ if (!context->getProperty(property_name, value)) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property
missing or invalid");
+ }
+ return value;
+}
+
+std::vector<std::string> listFromCommaSeparatedProperty(const
core::ProcessContext* context, const std::string& property_name) {
+ std::string property_string;
+ context->getProperty(property_name, property_string);
+ return utils::StringUtils::splitAndTrim(property_string, ",");
+}
+
+std::vector<std::string> listFromRequiredCommaSeparatedProperty(const
core::ProcessContext* context, const std::string& property_name) {
+ return utils::StringUtils::splitAndTrim(getRequiredPropertyOrThrow(context,
property_name), ",");
+}
+
+bool parseBooleanPropertyOrThrow(core::ProcessContext* context, const
std::string& property_name) {
+ bool value;
+ std::string value_str = getRequiredPropertyOrThrow(context, property_name);
+ utils::optional<bool> maybe_value = utils::StringUtils::toBool(value_str);
+ if (!maybe_value) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property is
invalid: value is " + value_str);
+ }
+ return maybe_value.value();
+}
+
+std::chrono::milliseconds parseTimePropertyMSOrThrow(core::ProcessContext*
context, const std::string& property_name) {
+ core::TimeUnit unit;
+ uint64_t time_value_ms;
+ if (!core::Property::StringToTime(getRequiredPropertyOrThrow(context,
property_name), time_value_ms, unit) ||
!core::Property::ConvertTimeUnitToMS(time_value_ms, unit, time_value_ms)) {
+ throw Exception(PROCESS_SCHEDULE_EXCEPTION, property_name + " property
missing or invalid");
+ }
+ return std::chrono::milliseconds(time_value_ms);
+}
+
+utils::optional<uint64_t> getOptionalUintProperty(core::ProcessContext*
context, const std::string& property_name) {
Review comment:
I suggest converting these functions to take `const
core::ProcessContext&` so that they can not be misused with null pointers and
they are const correct.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]