-
Notifications
You must be signed in to change notification settings - Fork 94
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
arpadboda
wants to merge
1
commit into
apache:master
Choose a base branch
from
arpadboda:MINIFICPP-985
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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_; | ||
} | ||
|
||
|
@@ -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(); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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) { | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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! | ||
|
@@ -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); | ||
|
@@ -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"), | ||
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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it OK for |
||
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.[1] https://github.com/apache/nifi/blob/master/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/processor/util/StandardValidators.java