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



##########
File path: libminifi/include/utils/ValueUtils.h
##########
@@ -0,0 +1,203 @@
+/**
+ *
+ * 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 NIFI_MINIFI_CPP_VALUEUTILS_H
+#define NIFI_MINIFI_CPP_VALUEUTILS_H
+
+#include <exception>
+#include <string>
+#include <cstring>
+#include <vector>
+#include <cstdlib>
+#include "PropertyErrors.h"
+#include <type_traits>
+#include <limits>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+class ValueParser {
+ private:
+  template< class... >
+  using void_t = void;
+
+  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:
+  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");
+    try {
+      char *end;
+      long result{std::strtol(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      if (result < (std::numeric_limits<int>::min)() || result > 
(std::numeric_limits<int>::max)()) {
+        throw ParseException("Cannot convert long to int");
+      }
+      out = {static_cast<int>(result)};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse int");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<long, Out>::value, "Expected 
lossless conversion from long");
+    try {
+      char *end;
+      long result{std::strtol(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      out = {result};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse long");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseLongLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<long long, Out>::value, 
"Expected lossless conversion from long long");
+    try {
+      char *end;
+      long long result{std::strtoll(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      out = {result};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse long long");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseUInt32(Out& out) {
+    static_assert(is_non_narrowing_convertible<uint32_t, Out>::value, 
"Expected lossless conversion from uint32_t");
+    try {
+      parseSpace();
+      if (offset < str.length() && str[offset] == '-') {
+        throw ParseException("Not an unsigned long");
+      }
+      char *end;
+      unsigned long result{std::strtoul(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      if (result > (std::numeric_limits<uint32_t>::max)()) {
+        throw ParseException("Cannot convert unsigned long to uint32_t");
+      }
+      out = {static_cast<uint32_t>(result)};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse unsigned long");
+    }
+  }
+
+  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");
+    try {
+      parseSpace();
+      if (offset < str.length() && str[offset] == '-') {
+        throw ParseException("Not an unsigned long");
+      }
+      char *end;
+      unsigned long long result{std::strtoull(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      out = {result};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse unsigned long long");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseBool(Out& out){
+    const char* options[] = {"false", "true"};
+    const bool values[] = {false, true};
+    auto index = parseAny(options);
+    if(index == -1)throw ParseException("Couldn't parse bool");
+    out = values[index];
+    return *this;
+  }
+
+  int parseAny(const std::vector<std::string> &options) {
+    parseSpace();
+    for (std::size_t optionIdx = 0; optionIdx < options.size(); ++optionIdx) {
+      const auto &option = options[optionIdx];
+      if (offset + option.length() <= str.length()) {
+        if (std::equal(option.begin(), option.end(), str.begin() + offset)) {
+          offset += option.length();
+          return optionIdx;
+        }
+      }
+    }
+    return -1;
+  }
+
+  template<std::size_t N>
+  int parseAny(const char* (&options)[N]) {

Review comment:
       done

##########
File path: libminifi/include/utils/ValueUtils.h
##########
@@ -0,0 +1,203 @@
+/**
+ *
+ * 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 NIFI_MINIFI_CPP_VALUEUTILS_H
+#define NIFI_MINIFI_CPP_VALUEUTILS_H
+
+#include <exception>
+#include <string>
+#include <cstring>
+#include <vector>
+#include <cstdlib>
+#include "PropertyErrors.h"
+#include <type_traits>
+#include <limits>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+class ValueParser {
+ private:
+  template< class... >
+  using void_t = void;
+
+  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:
+  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");
+    try {
+      char *end;
+      long result{std::strtol(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      if (result < (std::numeric_limits<int>::min)() || result > 
(std::numeric_limits<int>::max)()) {
+        throw ParseException("Cannot convert long to int");
+      }
+      out = {static_cast<int>(result)};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse int");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<long, Out>::value, "Expected 
lossless conversion from long");
+    try {
+      char *end;
+      long result{std::strtol(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      out = {result};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse long");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseLongLong(Out& out) {
+    static_assert(is_non_narrowing_convertible<long long, Out>::value, 
"Expected lossless conversion from long long");
+    try {
+      char *end;
+      long long result{std::strtoll(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      out = {result};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse long long");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseUInt32(Out& out) {
+    static_assert(is_non_narrowing_convertible<uint32_t, Out>::value, 
"Expected lossless conversion from uint32_t");
+    try {
+      parseSpace();
+      if (offset < str.length() && str[offset] == '-') {
+        throw ParseException("Not an unsigned long");
+      }
+      char *end;
+      unsigned long result{std::strtoul(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      if (result > (std::numeric_limits<uint32_t>::max)()) {
+        throw ParseException("Cannot convert unsigned long to uint32_t");
+      }
+      out = {static_cast<uint32_t>(result)};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse unsigned long");
+    }
+  }
+
+  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");
+    try {
+      parseSpace();
+      if (offset < str.length() && str[offset] == '-') {
+        throw ParseException("Not an unsigned long");
+      }
+      char *end;
+      unsigned long long result{std::strtoull(str.c_str() + offset, &end, 10)};
+      offset = end - str.c_str();
+      out = {result};
+      return *this;
+    }catch(...){
+      throw ParseException("Could not parse unsigned long long");
+    }
+  }
+
+  template<typename Out>
+  ValueParser& parseBool(Out& out){
+    const char* options[] = {"false", "true"};
+    const bool values[] = {false, true};
+    auto index = parseAny(options);
+    if(index == -1)throw ParseException("Couldn't parse bool");
+    out = values[index];
+    return *this;
+  }
+
+  int parseAny(const std::vector<std::string> &options) {
+    parseSpace();
+    for (std::size_t optionIdx = 0; optionIdx < options.size(); ++optionIdx) {
+      const auto &option = options[optionIdx];
+      if (offset + option.length() <= str.length()) {
+        if (std::equal(option.begin(), option.end(), str.begin() + offset)) {
+          offset += option.length();
+          return optionIdx;
+        }
+      }
+    }
+    return -1;
+  }
+
+  template<std::size_t N>
+  int parseAny(const char* (&options)[N]) {
+    parseSpace();
+    for (std::size_t optionIdx = 0; optionIdx < N; ++optionIdx) {
+      const auto &option = options[optionIdx];
+      auto len = std::strlen(option);
+      if (offset + len <= str.length()) {
+        if (std::equal(option, option + len, str.begin() + offset)) {
+          offset += len;
+          return optionIdx;
+        }
+      }
+    }
+    return -1;
+  }
+
+  void parseEnd(){
+    parseSpace();
+    if(offset < str.length()){
+      throw ParseException("Expected to parse till the end");
+    }
+  }
+
+ private:
+  void parseSpace() {
+    while (offset < str.length() && std::isspace(str[offset]))++offset;

Review comment:
       done

##########
File path: libminifi/include/utils/PropertyErrors.h
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * 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 NIFI_MINIFI_CPP_PROPERTYERRORS_H
+#define NIFI_MINIFI_CPP_PROPERTYERRORS_H
+
+#include "Exception.h"
+
+namespace org{
+namespace apache{
+namespace nifi{
+namespace minifi{
+namespace core{
+class PropertyValue;
+class ConfigurableComponent;
+class Property;
+}

Review comment:
       done

##########
File path: libminifi/include/core/PropertyValue.h
##########
@@ -199,15 +214,40 @@ class PropertyValue : public state::response::ValueNode {
   auto operator=(const std::string &ref) -> typename std::enable_if<
   std::is_same<T, DataSizeValue >::value ||
   std::is_same<T, TimePeriodValue >::value,PropertyValue&>::type {
-    value_ = std::make_shared<T>(ref);
-    type_id = value_->getTypeIndex();
-    return *this;
+    validator_.clearValidationResult();
+    return WithAssignmentGuard(ref, [&] () -> PropertyValue& {
+      value_ = std::make_shared<T>(ref);
+      type_id = value_->getTypeIndex();
+      return *this;
+    });
+  }
+
+ private:
+
+  bool isValueUsable() const {
+    if (!value_) return false;
+    if (validator_.isValid() == CachedValueValidator::Result::FAILURE) return 
false;
+    if (validator_.isValid() == CachedValueValidator::Result::SUCCESS) return 
true;
+    return validate("__unknown__").valid();
+  }
+
+  template<typename Fn>
+  auto WithAssignmentGuard(const std::string& ref, Fn&& functor) -> 
decltype(std::forward<Fn>(functor)()) {
+    // TODO: as soon as c++17 comes jump to a RAII implementation

Review comment:
       done

##########
File path: libminifi/include/utils/ValueUtils.h
##########
@@ -0,0 +1,203 @@
+/**
+ *
+ * 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 NIFI_MINIFI_CPP_VALUEUTILS_H
+#define NIFI_MINIFI_CPP_VALUEUTILS_H
+
+#include <exception>
+#include <string>
+#include <cstring>
+#include <vector>
+#include <cstdlib>
+#include "PropertyErrors.h"
+#include <type_traits>
+#include <limits>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+class ValueParser {
+ private:
+  template< class... >
+  using void_t = void;

Review comment:
       done

##########
File path: libminifi/include/utils/PropertyErrors.h
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * 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 NIFI_MINIFI_CPP_PROPERTYERRORS_H
+#define NIFI_MINIFI_CPP_PROPERTYERRORS_H
+
+#include "Exception.h"
+
+namespace org{
+namespace apache{
+namespace nifi{
+namespace minifi{

Review comment:
       done

##########
File path: libminifi/include/core/PropertyValue.h
##########
@@ -138,26 +150,28 @@ class PropertyValue : public state::response::ValueNode {
    */
   template<typename T>
   auto operator=(const T ref) -> typename std::enable_if<std::is_same<T, 
std::string>::value,PropertyValue&>::type {

Review comment:
       I would not touch this now, as it is part of the API, after 0.8.0 we 
should revisit this, as well as the whole codebase in general

##########
File path: libminifi/include/core/CachedValueValidator.h
##########
@@ -0,0 +1,103 @@
+/**
+ *
+ * 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.
+ */
+
+#ifndef NIFI_MINIFI_CPP_CACHEDVALUEVALIDATOR_H
+#define NIFI_MINIFI_CPP_CACHEDVALUEVALIDATOR_H
+
+#include "PropertyValidation.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace core {
+
+class CachedValueValidator{
+ public:
+  enum class Result {
+    FAILURE,
+    SUCCESS,
+    RECOMPUTE
+  };
+
+  CachedValueValidator() = default;
+  CachedValueValidator(const CachedValueValidator& other) : 
validator_(other.validator_) {}
+  CachedValueValidator(CachedValueValidator&& other) : 
validator_(std::move(other.validator_)) {}
+  CachedValueValidator& operator=(const CachedValueValidator& other) {
+    validator_ = other.validator_;
+    validation_result_ = Result::RECOMPUTE;
+    return *this;
+  }
+  CachedValueValidator& operator=(CachedValueValidator&& other) {
+    validator_ = std::move(other.validator_);
+    validation_result_ = Result::RECOMPUTE;
+    return *this;
+  }
+
+  CachedValueValidator(const std::shared_ptr<PropertyValidator>& other) : 
validator_(other) {}
+  CachedValueValidator(std::shared_ptr<PropertyValidator>&& other) : 
validator_(std::move(other)) {}
+  CachedValueValidator& operator=(const std::shared_ptr<PropertyValidator>& 
new_validator) {
+    validator_ = new_validator;
+    validation_result_ = Result::RECOMPUTE;
+    return *this;
+  }
+  CachedValueValidator& operator=(std::shared_ptr<PropertyValidator>&& 
new_validator) {
+    validator_ = std::move(new_validator);
+    validation_result_ = Result::RECOMPUTE;
+    return *this;
+  }
+
+  const std::shared_ptr<PropertyValidator>& operator->() const {
+    return validator_;
+  }
+
+  operator bool() const {
+    return (bool)validator_;

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:
us...@infra.apache.org


Reply via email to