You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/06/16 12:34:13 UTC

[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #797: MINIFICPP-1231 - General property validation + use them in MergeContent.

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



##########
File path: libminifi/include/utils/PropertyErrors.h
##########
@@ -0,0 +1,115 @@
+/**
+ *
+ * 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 {
+
+class ValueException: public Exception{
+ private:
+  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
+
+  friend class ParseException;
+  friend class ConversionException;
+  friend class InvalidValueException;
+};

Review comment:
       This approach seems to violate the Open-closed principle. I don't mind it, as I'm not a big fan of its "open" part, but I'm interested why did you choose this approach over `protected` constructors.

##########
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) noexcept : validator_(std::move(other.validator_)) {}

Review comment:
       Instead of taking over the `validator_` but leaving the `validation_result_` in place, why don't we take over both and reset `other.validation_result_` to the default `RECOMPUTE`?

##########
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) {

Review comment:
       Self-assignment should be handled in both copy and move assignment.

##########
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) noexcept : 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 {

Review comment:
       I'd recommend an `explicit` conversion operator instead of an implicit one, if really needed. Explicit conversion happens inside an `if` statement.

##########
File path: extensions/coap/controllerservice/CoapConnector.cpp
##########
@@ -39,7 +39,7 @@ static core::Property RemoteServer;
 static core::Property Port;
 static core::Property MaxQueueSize;
 
-core::Property CoapConnectorService::RemoteServer(core::PropertyBuilder::createProperty("Remote Server")->withDescription("Remote CoAP server")->isRequired(true)->build());
+core::Property CoapConnectorService::RemoteServer(core::PropertyBuilder::createProperty("Remote Server")->withDescription("Remote CoAP server")->isRequired(false)->build());

Review comment:
       Why is this change? I don't think it makes much sense to set up coap without a server address.

##########
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) noexcept : 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_;
+  }
+
+  const std::shared_ptr<PropertyValidator>& operator*() const {
+    return validator_;
+  }
+
+  void setValidationResult(bool success) const {
+    validation_result_ = success ? Result::SUCCESS : Result::FAILURE;
+  }
+
+  void clearValidationResult() const {
+    validation_result_ = Result::RECOMPUTE;
+  }

Review comment:
       These are not logically `const` and `validation_result_` should not be `mutable`.

##########
File path: libminifi/include/core/state/Value.h
##########
@@ -86,35 +87,57 @@ class Value {
   }
 
   virtual bool getValue(uint32_t &ref) {
-    const auto negative = string_value.find_first_of('-') != std::string::npos;
-     if (negative) {
-       return false;
-     }
-    ref = std::stoul(string_value);
+    try {
+      uint32_t value;
+      utils::ValueParser(string_value).parseUInt32(value).parseEnd();
+      ref = value;
+    } catch(const utils::ParseException&) {
+      return false;
+    }

Review comment:
       To me, the changes look like they are not more general, just moving the logic elsewhere, with an increase of required boilerplate. Not sure if this is an improvement, but maybe I'm missing a point.

##########
File path: libminifi/include/utils/ValueUtils.h
##########
@@ -0,0 +1,193 @@
+/**

Review comment:
       I suggest renaming the file to `ValueParser.h` to reflect the class name. -Utils is usually used for potentially related, but largely independent set of utilities. This is a stateful parser with its own invariants

##########
File path: libminifi/src/core/ConfigurableComponent.cpp
##########
@@ -103,11 +105,12 @@ bool ConfigurableComponent::updateProperty(const std::string &name, const std::s
 
   if (it != properties_.end()) {
     Property orig_property = it->second;
-    Property new_property = orig_property;
+    Property& new_property = it->second;
+    utils::ScopeGuard onExit([&] {

Review comment:
       Now with `gsl::finally`, we have `finally` in C++. :slightly_smiling_face: 

##########
File path: libminifi/include/core/PropertyValue.h
##########
@@ -202,14 +196,50 @@ 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:
+  template<typename T>
+  T convertImpl(const char* const type_name) const {
+    if (!isValueUsable()) {
+      throw utils::InvalidValueException("Cannot convert invalid value");
+    }
+    T res;
+    if (value_->convertValue(res)) {
+      return res;
+    }
+    throw utils::ConversionException(std::string("Invalid conversion to ") + type_name + " for " + value_->getStringValue());
+  }
+
+  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(adebreceni): as soon as c++17 comes jump to a RAII implementation
+    // as we will need std::uncaught_exceptions()
+    try {
+      return std::forward<Fn>(functor)();
+    } catch(const utils::ValueException&) {
+      type_id = std::type_index(typeid(std::string));
+      value_ = minifi::state::response::createValue(ref);
+      throw;
+    }
   }
 
  protected:
   std::type_index type_id;
-  std::shared_ptr<PropertyValidator> validator_;
+  CachedValueValidator validator_;

Review comment:
       :+1: 
   I hope this API breakage is not significant enough to prevent merging before the next release. @arpadboda may be able to provide more insight.




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