arpadboda commented on a change in pull request #797:
URL: https://github.com/apache/nifi-minifi-cpp/pull/797#discussion_r443498165
##########
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:
I can definitely live with this :)
Property validation has always been lagging behind, seems to be properly
done finally.
----------------------------------------------------------------
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]