martinzink commented on code in PR #1926: URL: https://github.com/apache/nifi-minifi-cpp/pull/1926#discussion_r2000618635
########## utils/include/utils/ParsingUtils.h: ########## @@ -0,0 +1,94 @@ +/** + * 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 "ParsingErrors.h" +#include "StringUtils.h" +#include "TimeUtil.h" +#include "Literals.h" +#include "fmt/format.h" + +namespace org::apache::nifi::minifi::parsing { +nonstd::expected<bool, std::error_code> parseBool(std::string_view input); + +template<std::integral T> +nonstd::expected<T, std::error_code> parseIntegralMinMax(std::string_view input, T minimum, T maximum); +template<std::integral T> +nonstd::expected<T, std::error_code> parseIntegral(std::string_view input); + +template<class TargetDuration> +nonstd::expected<TargetDuration, std::error_code> parseDurationMinMax(std::string_view input, TargetDuration minimum, TargetDuration maximum); +template<class TargetDuration = std::chrono::milliseconds> +nonstd::expected<TargetDuration, std::error_code> parseDuration(std::string_view input); + +nonstd::expected<uint64_t, std::error_code> parseDataSizeMinMax(std::string_view input, uint64_t minimum, uint64_t maximum); +nonstd::expected<uint64_t, std::error_code> parseDataSize(std::string_view input); + +nonstd::expected<uint32_t, std::error_code> parsePermissions(std::string_view input); + + +template<std::integral T> +nonstd::expected<T, std::error_code> parseIntegralMinMax(const std::string_view input, const T minimum, const T maximum) { + const auto trimmed_input = utils::string::trim(input); + T t{}; + const auto [ptr, ec] = std::from_chars(trimmed_input.data(), trimmed_input.data() + trimmed_input.size(), t); + if (ec != std::errc()) { + return nonstd::make_unexpected(core::ParsingErrorCode::GeneralParsingError); + } + if (ptr != trimmed_input.data() + trimmed_input.size()) { + return nonstd::make_unexpected(core::ParsingErrorCode::GeneralParsingError); + } + + if (t < minimum) { return nonstd::make_unexpected(core::ParsingErrorCode::SmallerThanMinimum); } + if (t > maximum) { return nonstd::make_unexpected(core::ParsingErrorCode::LargerThanMaximum); } + return t; +} + +template<std::integral T> +nonstd::expected<T, std::error_code> parseIntegral(const std::string_view input) { + return parseIntegralMinMax<T>(input, std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); +} + +template<class TargetDuration> Review Comment: Its already defaults to milliseconds https://github.com/apache/nifi-minifi-cpp/blob/725ad867d35025d1b6231c31e49cf3f29fadf3da/utils/include/utils/ParsingUtils.h#L37 , but when using with utils::andThen it cant infer it, but added some missing tests https://github.com/apache/nifi-minifi-cpp/pull/1926/commits/054c9e4622dda73ae8f074c4529eaf623213fbab -- 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]
