adamdebreceni commented on a change in pull request #797:
URL: https://github.com/apache/nifi-minifi-cpp/pull/797#discussion_r446101347



##########
File path: libminifi/test/unit/PropertyValidationTests.cpp
##########
@@ -0,0 +1,238 @@
+/**
+ *
+ * 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 "../TestBase.h"
+#include "core/ConfigurableComponent.h"
+#include "utils/PropertyErrors.h"
+#include "core/PropertyValidation.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace core {
+
+using namespace utils::internal;
+/**
+ * This Tests checks a deprecated behavior that should be removed
+ * in the next major release.
+ */
+TEST_CASE("Some default values get coerced to typed variants") {
+  auto prop = Property("prop", "d", "true");
+  REQUIRE_THROWS_AS(prop.setValue("banana"), ConversionException);
+
+  const std::string SPACE = " ";
+  auto prop2 = Property("prop", "d", SPACE + "true");
+  prop2.setValue("banana");
+}
+
+TEST_CASE("Converting invalid PropertyValue") {
+  auto prop = PropertyBuilder::createProperty("prop")
+    ->withDefaultValue<int>(0)
+    ->build();

Review comment:
       done

##########
File path: libminifi/include/utils/ValueParser.h
##########
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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/licenseas/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.
+ */
+
+#ifndef LIBMINIFI_INCLUDE_UTILS_VALUEPARSER_H_
+#define LIBMINIFI_INCLUDE_UTILS_VALUEPARSER_H_
+
+#include <exception>
+#include <string>
+#include <cstring>
+#include <vector>
+#include <cstdlib>
+#include <type_traits>
+#include <limits>
+
+#include "PropertyErrors.h"
+#include "GeneralUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+namespace internal {
+
+class ValueParser {
+ private:
+  template<typename From, typename To, typename = void>
+  struct is_non_narrowing_convertible : std::false_type {
+    static_assert(std::is_integral<From>::value && 
std::is_integral<To>::value, "Checks only integral values");
+  };
+
+  template<typename From, typename To>
+  struct is_non_narrowing_convertible<From, To, 
void_t<decltype(To{std::declval<From>()})>> : std::true_type {
+    static_assert(std::is_integral<From>::value && 
std::is_integral<To>::value, "Checks only integral values");
+  };
+
+ public:
+  explicit ValueParser(const std::string& str, std::size_t offset = 0) : 
str(str), offset(offset) {}
+
+  template<typename Out>
+  ValueParser& parseInt(Out& out) {
+    static_assert(is_non_narrowing_convertible<int, Out>::value, "Expected 
lossless conversion from int");
+    long result;  // NOLINT
+    auto len = safeCallConverter(std::strtol, result);
+    if (len == 0) {
+      throw ParseException("Couldn't parse int");
+    }
+    if (result < (std::numeric_limits<int>::min)() || result > 
(std::numeric_limits<int>::max)()) {
+      throw ParseException("Cannot convert long to int");
+    }
+    offset += len;
+    out = {static_cast<int>(result)};
+    return *this;
+  }
+
+  template<typename Out>
+  ValueParser& parseLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<long, Out>::value, "Expected 
lossless conversion from long");  // NOLINT
+    long result;  // NOLINT
+    auto len = safeCallConverter(std::strtol, result);
+    if (len == 0) {
+      throw ParseException("Couldn't parse long");
+    }
+    offset += len;
+    out = {result};
+    return *this;
+  }
+
+  template<typename Out>
+  ValueParser& parseLongLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<long long, Out>::value, 
"Expected lossless conversion from long long");  // NOLINT
+    long long result;  // NOLINT
+    auto len = safeCallConverter(std::strtoll, result);
+    if (len == 0) {
+      throw ParseException("Couldn't parse long long");
+    }
+    offset += len;
+    out = {result};
+    return *this;
+  }
+
+  template<typename Out>
+  ValueParser& parseUInt32(Out& out) {
+    static_assert(is_non_narrowing_convertible<uint32_t, Out>::value, 
"Expected lossless conversion from uint32_t");
+    skipWhitespace();
+    if (offset < str.length() && str[offset] == '-') {
+      throw ParseException("Not an unsigned long");
+    }
+    unsigned long result;  // NOLINT
+    auto len = safeCallConverter(std::strtoul, result);
+    if (len == 0) {
+      throw ParseException("Couldn't parse uint32_t");
+    }
+    if (result > (std::numeric_limits<uint32_t>::max)()) {
+      throw ParseException("Cannot convert unsigned long to uint32_t");
+    }
+    offset += len;
+    out = {static_cast<uint32_t>(result)};
+    return *this;
+  }
+
+  template<typename Out>
+  ValueParser& parseUnsignedLongLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<unsigned long long, 
Out>::value, "Expected lossless conversion from unsigned long long");  // NOLINT
+    skipWhitespace();
+    if (offset < str.length() && str[offset] == '-') {
+      throw ParseException("Not an unsigned long");
+    }
+    unsigned long long result;  // NOLINT
+    auto len = safeCallConverter(std::strtoull, result);
+    if (len == 0) {
+      throw ParseException("Couldn't parse unsigned long long");
+    }
+    offset += len;
+    out = {result};
+    return *this;
+  }
+
+  template<typename Out>
+  ValueParser& parseBool(Out& out) {

Review comment:
       done

##########
File path: libminifi/include/utils/PropertyErrors.h
##########
@@ -0,0 +1,101 @@
+/**
+ *
+ * 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/licenseas/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.
+ */
+
+#ifndef LIBMINIFI_INCLUDE_UTILS_PROPERTYERRORS_H_
+#define LIBMINIFI_INCLUDE_UTILS_PROPERTYERRORS_H_
+
+#include <string>
+
+#include "Exception.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+
+namespace core {
+
+class PropertyValue;
+class ConfigurableComponent;
+class Property;
+
+} /* namespace core */
+
+namespace utils {
+namespace internal {
+
+class ValueException : public Exception {
+ protected:
+  explicit ValueException(const std::string& err) : 
Exception(ExceptionType::GENERAL_EXCEPTION, err) {}
+  explicit ValueException(const char* err) : 
Exception(ExceptionType::GENERAL_EXCEPTION, err) {}
+
+  // base class already has a virtual destructor
+};
+
+class PropertyException : public Exception {
+ protected:
+  explicit PropertyException(const std::string& err) : 
Exception(ExceptionType::GENERAL_EXCEPTION, err) {}
+  explicit PropertyException(const char* err) : 
Exception(ExceptionType::GENERAL_EXCEPTION, err) {}
+
+  // base class already has a virtual destructor
+};
+
+/**
+ * Thrown during converting from and to Value
+ */
+class ConversionException : public ValueException {
+ public:
+  explicit ConversionException(const std::string& err) : ValueException(err) {}
+  explicit ConversionException(const char* err) : ValueException(err) {}

Review comment:
       done




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


Reply via email to