Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: MINIFICPP-985 - Implement listvalidators #618

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 65 additions & 25 deletions libminifi/include/core/PropertyValidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include "core/state/Value.h"
#include "TypedValues.h"
#include "utils/StringUtils.h"
#include "utils/RegexUtils.h"
#include <limits>
#include <memory>
#include <set>

namespace org {
namespace apache {
Expand Down Expand Up @@ -88,11 +90,10 @@ class PropertyValidator {
PropertyValidator(const std::string &name)
: name_(name) {
}
virtual ~PropertyValidator() {

}
virtual ~PropertyValidator() {}

std::string getName() const {
virtual std::string getName() const {
return name_;
}

Expand Down Expand Up @@ -124,8 +125,7 @@ class AlwaysValid : public PropertyValidator {
PropertyValidator(name) {

}
virtual ~AlwaysValid() {
}

ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return ValidationResult::Builder::createBuilder().withSubject(subject).withInput(input->getStringValue()).isValid(always_valid_).build();
}
Expand All @@ -141,9 +141,6 @@ class BooleanValidator : public PropertyValidator {
BooleanValidator(const std::string &name)
: PropertyValidator(name) {
}
virtual ~BooleanValidator() {

}

ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return PropertyValidator::_validate_internal<minifi::state::response::BoolValue>(subject, input);
Expand All @@ -162,8 +159,6 @@ class IntegerValidator : public PropertyValidator {
IntegerValidator(const std::string &name)
: PropertyValidator(name) {
}
virtual ~IntegerValidator() {
}

ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return PropertyValidator::_validate_internal<minifi::state::response::IntValue>(subject, input);
Expand All @@ -187,9 +182,7 @@ class LongValidator : public PropertyValidator {
min_(min),
max_(max) {
}
virtual ~LongValidator() {

}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
auto in64 = std::dynamic_pointer_cast<minifi::state::response::Int64Value>(input);
if (in64) {
Expand Down Expand Up @@ -221,9 +214,7 @@ class UnsignedLongValidator : public PropertyValidator {
explicit UnsignedLongValidator(const std::string &name)
: PropertyValidator(name) {
}
virtual ~UnsignedLongValidator() {

}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return PropertyValidator::_validate_internal<minifi::state::response::UInt64Value>(subject, input);
}
Expand All @@ -249,9 +240,7 @@ class DataSizeValidator : public PropertyValidator {
DataSizeValidator(const std::string &name)
: PropertyValidator(name) {
}
virtual ~DataSizeValidator() {

}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return PropertyValidator::_validate_internal<core::DataSizeValue>(subject, input);
}
Expand All @@ -270,9 +259,6 @@ class PortValidator : public LongValidator {
PortValidator(const std::string &name)
: LongValidator(name, 1, 65535) {
}
virtual ~PortValidator() {

}
};

//Use only for specifying listen ports, where 0 means a randomly chosen one!
Expand All @@ -281,18 +267,12 @@ class ListenPortValidator : public LongValidator {
ListenPortValidator(const std::string &name)
: LongValidator(name, 0, 65535) {
}
virtual ~ListenPortValidator() {

}
};

class TimePeriodValidator : public PropertyValidator {
public:
TimePeriodValidator(const std::string &name)
: PropertyValidator(name) {
}
virtual ~TimePeriodValidator() {

}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return PropertyValidator::_validate_internal<core::TimePeriodValue>(subject, input);
Expand All @@ -308,6 +288,66 @@ class TimePeriodValidator : public PropertyValidator {
}
};

class RegexValidator : public PropertyValidator {
public:
explicit RegexValidator(const std::string& pattern)
: PropertyValidator("REGULAR_EXPRESSION_VALIDATOR"){
regex_ = utils::Regex(pattern);
}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return validate(subject, input->getStringValue());
}

ValidationResult validate(const std::string &subject, const std::string &input) const {
bool result = regex_.match(input);
return ValidationResult::Builder::createBuilder().withSubject(subject).withInput(input).isValid(result).build();
}
private:
mutable utils::Regex regex_;
};

template <typename T, typename std::enable_if<std::is_base_of<PropertyValidator, T>::value>::type* = nullptr>
class ListValidator : public PropertyValidator{
public:
explicit ListValidator(T val)
: PropertyValidator("LIST_VALIDATOR"),
Copy link
Contributor Author

@arpadboda arpadboda Jul 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name should somehow contain the underlying validator's name to work properly with C2.
This should be adjusted to the way Java MiNiFi or NiFi works.

That's why I made getName() virtual.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name pattern of non-list validators in nifi seem to be _VALIDATOR, and the pattern of list validators seem to be _LIST_VALIDATOR. [1]
I suggest cutting the _VALIDATOR suffix from the inner validator, replace it with _LIST_VALIDATOR suffix and pass that to the base class ctor. I'd also avoid making getName() virtual, since all the derived classes can already specify their desired names through the base class ctor parameter, so making it virtual would be a second customization point for the very same thing.

std::string replace_last(std::string str, const std::string& search, const std::string& replacement) {
  str.replace(str.find_last_of(search), search.size(), replacement);
  return str;
}

template<typename T>
ListValidator<T>::ListValidator(T inner_validator)
    :PropertyValidator{ replace_last(inner_validator.getName(), "_VALIDATOR", "_LIST_VALIDATOR") },
[...]

[1] https://github.com/apache/nifi/blob/master/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/processor/util/StandardValidators.java

inner_validator_(std::move(val)){
}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return validate(subject, input->getStringValue());
}

ValidationResult validate(const std::string &subject, const std::string &input) const {
const auto& tokens = utils::StringUtils::split(input, ",");
for(const auto& token: tokens) {
if(!inner_validator_.validate(subject, token).valid()){
return ValidationResult::Builder::createBuilder().withSubject(subject).withInput(input).isValid(false).build();
}
}
return ValidationResult::Builder::createBuilder().withSubject(subject).withInput(input).isValid(true).build();
}

private:
T inner_validator_;
};

class MultiChoiceValidator : PropertyValidator {
public:
explicit MultiChoiceValidator (const std::set<std::string>& choices)
: PropertyValidator("LIST_VALIDATOR"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK for MultiChoiceValidator to have the same name as ListValidator<T>? I couldn't find validator names referenced in minifi-c2, so I don't know whether this causes problems with C2.

val_(RegexValidator("(" + utils::StringUtils::join("|", choices) + ")")) {
}
ValidationResult validate(const std::string &subject, const std::shared_ptr<minifi::state::response::Value> &input) const {
return validate(subject, input->getStringValue());
}

ValidationResult validate(const std::string &subject, const std::string &input) const {
return val_.validate(subject, input);
}
private:
ListValidator<RegexValidator> val_;
};

// STATIC DEFINITIONS

class StandardValidators {
Expand Down
22 changes: 22 additions & 0 deletions libminifi/test/unit/PropertyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include "utils/StringUtils.h"
#include "core/Property.h"
#include "core/PropertyValidation.h"
#include "../TestBase.h"

TEST_CASE("Test Boolean Conversion", "[testboolConversion]") {
Expand Down Expand Up @@ -173,3 +174,24 @@ TEST_CASE("Test DateTime Conversion", "[testDateTime]") {

REQUIRE(false == org::apache::nifi::minifi::core::Property::StringToDateTime("foobar", timestamp));
}

TEST_CASE("Regex validator tests", "[TestRegexValidator]") {
core::RegexValidator rv("dog");
REQUIRE(rv.validate("", "mad dog").valid());
REQUIRE(rv.validate("", "dogfooded test").valid());
REQUIRE_FALSE(rv.validate("", "d_o_g").valid());
REQUIRE_THROWS(core::RegexValidator("(unclosed]"));
}

TEST_CASE("List validator tests", "[TestListValidator]") {
core::ListValidator<core::IntegerValidator> listintvalidator(core::IntegerValidator("IV"));
REQUIRE(listintvalidator.validate("", "1,42,8").valid());
REQUIRE_FALSE(listintvalidator.validate("","4,three").valid());
REQUIRE_FALSE(listintvalidator.validate("", "cats, dogs").valid());
}

TEST_CASE("Multichoice validator tests", "[TestMultichoiceValidator]") {
core::MultiChoiceValidator mv({"cat", "dog", "pig"});
REQUIRE(mv.validate("", "pig,cat").valid());
REQUIRE_FALSE(mv.validate("", "rat,dog").valid());
}