szaszm commented on code in PR #1926:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1926#discussion_r1979413517
##########
utils/include/utils/expected.h:
##########
@@ -176,11 +177,26 @@ auto operator|(Expected&& object,
transform_error_wrapper<F> f) {
static_assert(valid_unexpected_type<transformed_error_type>, "transformError
expects a function returning a valid unexpected type");
using transformed_expected_type = nonstd::expected<value_type,
transformed_error_type>;
if (object.has_value()) {
- return transformed_expected_type{std::forward<Expected>(object)};
+ return transformed_expected_type{std::forward<Expected>(object).value()};
Review Comment:
Is there any reason for this change? It seems to make no difference from
what I can see, copy/moving the whole object that has the value vs making a new
one with the value.
##########
utils/include/utils/detail/MonadicOperationWrappers.h:
##########
@@ -49,6 +49,17 @@ template<typename T>
struct transform_error_wrapper {
T function;
};
+
+struct to_optional_wrapper{};
+
+template<typename E>
+struct to_expected_wrapper {
+ E error;
+};
+
+struct expect_wrapper {
+ std::string reason;
Review Comment:
This could be a string_view or const char*, it's only ever accessed in the
same expression as far as I'm aware.
##########
utils/src/utils/ParsingUtils.cpp:
##########
@@ -0,0 +1,122 @@
+/**
+ *
+ * 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 "utils/ParsingUtils.h"
+
+namespace org::apache::nifi::minifi::parsing {
+
+nonstd::expected<bool, std::error_code> parseBool(const std::string_view
input) {
+ if (utils::string::equalsIgnoreCase(input, "true")) { return true; }
+ if (utils::string::equalsIgnoreCase(input, "false")) { return false; }
+
+ return nonstd::make_unexpected(core::ParsingErrorCode::GeneralParsingError);
+}
+
+namespace {
+uint64_t getUnitMultiplier(const std::string_view unit_str) {
+ static std::map<std::string, int64_t, std::less<>> unit_map{
+ {"B", 1},
+ {"K", 1_KB},
+ {"M", 1_MB},
+ {"G", 1_GB},
+ {"T", 1_TB},
+ {"P", 1_PB},
+ {"KB", 1_KiB},
+ {"MB", 1_MiB},
+ {"GB", 1_GiB},
+ {"TB", 1_TiB},
+ {"PB", 1_PiB},
+ {"KiB", 1_KiB},
+ {"MiB", 1_MiB},
+ {"GiB", 1_GiB},
+ {"TiB", 1_TiB},
+ {"PiB", 1_PiB},
+ };
+ const auto unit_multiplier = unit_map.find(unit_str);
+ if (unit_multiplier != unit_map.end()) { return unit_multiplier->second; }
+
+
+ return 1;
+}
+}
+
+nonstd::expected<uint64_t, std::error_code> parseDataSizeMinMax(const
std::string_view input, const uint64_t minimum, const uint64_t maximum) {
+ const auto trimmed_input = utils::string::trim(input);
+ const auto split_pos = trimmed_input.find_first_not_of("0123456789");
+
+ if (split_pos == std::string_view::npos) {
+ return parseIntegral<uint64_t>(trimmed_input);
+ }
+
+ const auto num_str = trimmed_input.substr(0, split_pos);
+ const std::string unit_str =
utils::string::toUpper(std::string{trimmed_input.substr(split_pos,
trimmed_input.size() - split_pos)});
+
+ nonstd::expected<uint64_t, std::error_code> num_part =
parseIntegral<uint64_t>(num_str);
+ if (!num_part) { return nonstd::make_unexpected(num_part.error()); }
+
+ uint64_t result = *num_part *
getUnitMultiplier(utils::string::trim(unit_str));
+ if (result < minimum) { return
nonstd::make_unexpected(core::ParsingErrorCode::SmallerThanMinimum); }
+ if (result > maximum) { return
nonstd::make_unexpected(core::ParsingErrorCode::LargerThanMaximum); }
+
+ return result;
+}
+
+nonstd::expected<uint64_t, std::error_code> parseDataSize(const
std::string_view input) {
+ return parseDataSizeMinMax(input, std::numeric_limits<uint64_t>::min(),
std::numeric_limits<uint64_t>::max());
+}
+
+nonstd::expected<uint32_t, std::error_code> parsePermissions(const
std::string_view input) {
Review Comment:
let's move it there
--
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]