martinzink commented on code in PR #2220:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2220#discussion_r3795183092


##########
minifi_rust/minifi_native/src/api/property.rs:
##########
@@ -16,102 +16,291 @@
 // 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<K2: ?Sized + PropertySchema>(&self) -> 
Property<K2> {

Review Comment:
   this is used for the wrapped the controller services, with this we can 
change the type of the property (creating an alternative one) during 
get_controller_service calls with this so one could simply write 
   ```
   pub(crate) const DUMMY_CONTROLLER_SERVICE: 
Property<Option<DummyControllerService>> = Property::new(
       "Dummy Controller Service",
       "Optional dummy controller service",
   );
   ```
   and the output type can be an Option<&DummyControllerService> via the 
ControllerServiceValue PropertySchema



-- 
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]

Reply via email to