martinzink commented on code in PR #2220:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2220#discussion_r3863699154
##########
minifi_rust/minifi_native/src/api/property.rs:
##########
@@ -16,102 +16,303 @@
// under the License.
use crate::StandardPropertyValidator::{
- BoolValidator, DataSizeValidator, TimePeriodValidator, U64Validator,
+ BoolValidator, DataSizeValidator, NonBlankValidator, TimePeriodValidator,
U64Validator,
};
use crate::{
ComponentIdentifier, ControllerServiceDefinition, EnableControllerService,
MinifiError,
};
+use minifi_native::StandardPropertyValidator::{F64Validator, I64Validator};
+use std::marker::PhantomData;
use std::str::FromStr;
use std::time::Duration;
#[derive(Debug, Eq, PartialEq)]
pub enum StandardPropertyValidator {
- AlwaysValidValidator,
NonBlankValidator,
TimePeriodValidator,
BoolValidator,
I64Validator,
U64Validator,
DataSizeValidator,
PortValidator,
+ F64Validator,
}
-#[derive(Debug)]
-pub struct Property {
+#[derive(Debug, PartialEq)]
+pub enum PropertyConstraints {
+ Validator(StandardPropertyValidator),
+ AllowedValues(&'static [&'static str]),
+ ControllerService(&'static str),
+}
+
+pub struct PropertyDefinition {
pub name: &'static str,
pub description: &'static str,
pub is_required: bool,
pub is_sensitive: bool,
pub supports_expr_lang: bool,
pub default_value: Option<&'static str>,
- pub validator: StandardPropertyValidator,
- pub allowed_values: &'static [&'static str],
- pub allowed_type: Option<&'static str>,
+ pub constraints: Option<PropertyConstraints>,
}
-pub trait GetProperty {
- fn get_property(&self, property: &Property) -> Result<Option<String>,
MinifiError>;
- fn get_bool_property(&self, property: &Property) -> Result<Option<bool>,
MinifiError> {
- if property.validator != BoolValidator {
- return Err(MinifiError::validation_err(format!(
- "to use get_bool_property {:?} must have BoolValidator",
- property
- )));
- }
+#[macro_export]
+macro_rules! property_definitions {
+ ($($property:expr),* $(,)?) => {
+ &[$($property.definition()),*]
+ };
+}
+
+pub struct Property<P: ?Sized + PropertySchema> {
+ pub(crate) name: &'static str,
+ pub(crate) description: &'static str,
+ pub(crate) is_sensitive: bool,
+ pub(crate) supports_expr_lang: bool,
+ pub(crate) default_value: Option<&'static str>,
+ pub(crate) marker: PhantomData<P>,
+}
- if let Some(property_val) = self.get_property(property)? {
- Ok(Some(bool::from_str(&property_val)?))
- } else {
- Ok(None)
+impl<P: ?Sized + PropertySchema> Property<P> {
+ pub const fn new(name: &'static str, description: &'static str) -> Self {
+ Property {
+ name,
+ description,
+ is_sensitive: false,
+ supports_expr_lang: false,
+ default_value: None,
+ marker: PhantomData,
}
}
- fn get_duration_property(&self, property: &Property) ->
Result<Option<Duration>, MinifiError> {
- if property.validator != TimePeriodValidator {
- return Err(MinifiError::validation_err(format!(
- "to use get_duration_property {:?} must have
TimePeriodValidator",
- property
- )));
+ pub const fn sensitive(mut self) -> Self {
+ self.is_sensitive = true;
+ self
+ }
+
+ pub const fn supports_expression_language(mut self) -> Self {
+ self.supports_expr_lang = true;
+ self
+ }
+
+ pub const fn with_default(mut self, default_value: &'static str) -> Self {
+ self.default_value = Some(default_value);
+ self
+ }
+
+ pub const fn name(&self) -> &'static str {
+ self.name
+ }
+
+ pub const fn definition(&self) -> PropertyDefinition {
+ PropertyDefinition {
+ name: self.name,
+ description: self.description,
+ is_required: P::IS_REQUIRED,
+ is_sensitive: self.is_sensitive,
+ supports_expr_lang: self.supports_expr_lang,
+ default_value: self.default_value,
+ constraints: P::CONSTRAINT,
}
+ }
- if let Some(property_val) = self.get_property(property)? {
- Ok(Some(humantime::parse_duration(property_val.as_str())?))
- } else {
- Ok(None)
+ pub(crate) const fn with_marker<P2: ?Sized + PropertySchema>(&self) ->
Property<P2> {
+ Property {
+ name: self.name,
+ description: self.description,
+ is_sensitive: self.is_sensitive,
+ supports_expr_lang: self.supports_expr_lang,
+ default_value: self.default_value,
+ marker: PhantomData,
}
}
+}
+
+/// Trait required to register Property with the agent
+/// These values will be translated to fill out the
+/// validator, allowed_value, allowed_types, is_required on the agent side
+pub trait PropertySchema {
+ const CONSTRAINT: Option<PropertyConstraints>;
+ const IS_REQUIRED: bool;
+}
+
+/// The requiredness of the property is enforced via this Option impl
+/// If the property is required it should be registered as Property<T>
+/// If the property is not required it should be registered as
Property<Option<T>
+impl<T: PropertySchema> PropertySchema for Option<T> {
+ const CONSTRAINT: Option<PropertyConstraints> = T::CONSTRAINT;
+ const IS_REQUIRED: bool = false;
+}
- fn get_size_property(&self, property: &Property) -> Result<Option<u64>,
MinifiError> {
- if property.validator != DataSizeValidator {
- return Err(MinifiError::validation_err(format!(
- "to use get_size_property {:?} must have DataSizeValidator",
- property
- )));
+/// Trait required to register property as Property<T> or Property<Option<T>
Review Comment:
https://github.com/apache/nifi-minifi-cpp/pull/2220/changes/288e205a9fef79af4737904a69f88f0110ee41b6
##########
minifi_rust/minifi_native/src/api/property.rs:
##########
@@ -16,102 +16,303 @@
// under the License.
use crate::StandardPropertyValidator::{
- BoolValidator, DataSizeValidator, TimePeriodValidator, U64Validator,
+ BoolValidator, DataSizeValidator, NonBlankValidator, TimePeriodValidator,
U64Validator,
};
use crate::{
ComponentIdentifier, ControllerServiceDefinition, EnableControllerService,
MinifiError,
};
+use minifi_native::StandardPropertyValidator::{F64Validator, I64Validator};
+use std::marker::PhantomData;
use std::str::FromStr;
use std::time::Duration;
#[derive(Debug, Eq, PartialEq)]
pub enum StandardPropertyValidator {
- AlwaysValidValidator,
NonBlankValidator,
TimePeriodValidator,
BoolValidator,
I64Validator,
U64Validator,
DataSizeValidator,
PortValidator,
+ F64Validator,
}
-#[derive(Debug)]
-pub struct Property {
+#[derive(Debug, PartialEq)]
+pub enum PropertyConstraints {
+ Validator(StandardPropertyValidator),
+ AllowedValues(&'static [&'static str]),
+ ControllerService(&'static str),
+}
+
+pub struct PropertyDefinition {
pub name: &'static str,
pub description: &'static str,
pub is_required: bool,
pub is_sensitive: bool,
pub supports_expr_lang: bool,
pub default_value: Option<&'static str>,
- pub validator: StandardPropertyValidator,
- pub allowed_values: &'static [&'static str],
- pub allowed_type: Option<&'static str>,
+ pub constraints: Option<PropertyConstraints>,
}
-pub trait GetProperty {
- fn get_property(&self, property: &Property) -> Result<Option<String>,
MinifiError>;
- fn get_bool_property(&self, property: &Property) -> Result<Option<bool>,
MinifiError> {
- if property.validator != BoolValidator {
- return Err(MinifiError::validation_err(format!(
- "to use get_bool_property {:?} must have BoolValidator",
- property
- )));
- }
+#[macro_export]
+macro_rules! property_definitions {
+ ($($property:expr),* $(,)?) => {
+ &[$($property.definition()),*]
+ };
+}
+
+pub struct Property<P: ?Sized + PropertySchema> {
+ pub(crate) name: &'static str,
+ pub(crate) description: &'static str,
+ pub(crate) is_sensitive: bool,
+ pub(crate) supports_expr_lang: bool,
+ pub(crate) default_value: Option<&'static str>,
+ pub(crate) marker: PhantomData<P>,
+}
- if let Some(property_val) = self.get_property(property)? {
- Ok(Some(bool::from_str(&property_val)?))
- } else {
- Ok(None)
+impl<P: ?Sized + PropertySchema> Property<P> {
+ pub const fn new(name: &'static str, description: &'static str) -> Self {
+ Property {
+ name,
+ description,
+ is_sensitive: false,
+ supports_expr_lang: false,
+ default_value: None,
+ marker: PhantomData,
}
}
- fn get_duration_property(&self, property: &Property) ->
Result<Option<Duration>, MinifiError> {
- if property.validator != TimePeriodValidator {
- return Err(MinifiError::validation_err(format!(
- "to use get_duration_property {:?} must have
TimePeriodValidator",
- property
- )));
+ pub const fn sensitive(mut self) -> Self {
+ self.is_sensitive = true;
+ self
+ }
+
+ pub const fn supports_expression_language(mut self) -> Self {
+ self.supports_expr_lang = true;
+ self
+ }
+
+ pub const fn with_default(mut self, default_value: &'static str) -> Self {
+ self.default_value = Some(default_value);
+ self
+ }
+
+ pub const fn name(&self) -> &'static str {
+ self.name
+ }
+
+ pub const fn definition(&self) -> PropertyDefinition {
+ PropertyDefinition {
+ name: self.name,
+ description: self.description,
+ is_required: P::IS_REQUIRED,
+ is_sensitive: self.is_sensitive,
+ supports_expr_lang: self.supports_expr_lang,
+ default_value: self.default_value,
+ constraints: P::CONSTRAINT,
}
+ }
- if let Some(property_val) = self.get_property(property)? {
- Ok(Some(humantime::parse_duration(property_val.as_str())?))
- } else {
- Ok(None)
+ pub(crate) const fn with_marker<P2: ?Sized + PropertySchema>(&self) ->
Property<P2> {
+ Property {
+ name: self.name,
+ description: self.description,
+ is_sensitive: self.is_sensitive,
+ supports_expr_lang: self.supports_expr_lang,
+ default_value: self.default_value,
+ marker: PhantomData,
}
}
+}
+
+/// Trait required to register Property with the agent
+/// These values will be translated to fill out the
+/// validator, allowed_value, allowed_types, is_required on the agent side
+pub trait PropertySchema {
+ const CONSTRAINT: Option<PropertyConstraints>;
+ const IS_REQUIRED: bool;
+}
+
+/// The requiredness of the property is enforced via this Option impl
+/// If the property is required it should be registered as Property<T>
+/// If the property is not required it should be registered as
Property<Option<T>
Review Comment:
https://github.com/apache/nifi-minifi-cpp/pull/2220/changes/288e205a9fef79af4737904a69f88f0110ee41b6
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]