Browse Source

Merge pull request #1079 from iorveth/schema_refactoring

Schema refactoring
Bedeho Mender 4 years ago
parent
commit
f7dd96f523

+ 2 - 2
runtime-modules/content-directory/src/mock.rs

@@ -973,12 +973,12 @@ impl<T: Trait> PropertyType<T> {
     }
 
     pub fn single_text(text_max_len: TextMaxLength) -> PropertyType<Runtime> {
-        let text_type = SingleValuePropertyType(Type::<Runtime>::Text(text_max_len));
+        let text_type = Type::<Runtime>::Text(text_max_len);
         PropertyType::<Runtime>::Single(text_type)
     }
 
     pub fn single_text_hash(text_hash_max_len: HashedTextMaxLength) -> PropertyType<Runtime> {
-        let text_type = SingleValuePropertyType(Type::<Runtime>::Hash(text_hash_max_len));
+        let text_type = Type::<Runtime>::Hash(text_hash_max_len);
         PropertyType::<Runtime>::Single(text_type)
     }
 

+ 30 - 58
runtime-modules/content-directory/src/schema.rs

@@ -64,6 +64,27 @@ impl<T: Trait> Default for Type<T> {
     }
 }
 
+impl<T: Trait> Type<T> {
+    /// Ensure `Type` specific `TextMaxLengthConstraint` or `HashedTextMaxLengthConstraint` satisfied
+    pub fn ensure_property_type_size_is_valid(&self) -> Result<(), Error<T>> {
+        if let Type::Text(text_max_len) = self {
+            ensure!(
+                *text_max_len <= T::TextMaxLengthConstraint::get(),
+                Error::<T>::TextPropertyTooLong
+            );
+        }
+
+        if let Type::Hash(hashed_text_max_len) = self {
+            ensure!(
+                *hashed_text_max_len <= T::HashedTextMaxLengthConstraint::get(),
+                Error::<T>::HashedTextPropertyTooLong
+            );
+        }
+
+        Ok(())
+    }
+}
+
 /// Vector property type representation
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)]
@@ -93,18 +114,9 @@ impl<T: Trait> VecPropertyType<T> {
 
     /// Ensure `Type` specific `TextMaxLengthConstraint` & `VecMaxLengthConstraint` satisfied
     fn ensure_property_type_size_is_valid(&self) -> Result<(), Error<T>> {
-        if let Type::Text(text_max_len) = self.vec_type {
-            ensure!(
-                text_max_len <= T::TextMaxLengthConstraint::get(),
-                Error::<T>::TextPropertyTooLong
-            );
-        }
-        if let Type::Hash(hash_text_max_len) = self.vec_type {
-            ensure!(
-                hash_text_max_len <= T::HashedTextMaxLengthConstraint::get(),
-                Error::<T>::HashedTextPropertyTooLong
-            );
-        }
+        // Ensure Type specific TextMaxLengthConstraint or HashedTextMaxLengthConstraint satisfied
+        self.vec_type.ensure_property_type_size_is_valid()?;
+
         ensure!(
             self.max_length <= T::VecMaxLengthConstraint::get(),
             Error::<T>::VecPropertyTooLong
@@ -121,57 +133,17 @@ impl<T: Trait> VecPropertyType<T> {
     }
 }
 
-/// `Type` enum wrapper
-#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
-#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)]
-pub struct SingleValuePropertyType<T: Trait>(pub Type<T>);
-
-impl<T: Trait> Default for SingleValuePropertyType<T> {
-    fn default() -> Self {
-        Self(Type::default())
-    }
-}
-
-impl<T: Trait> SingleValuePropertyType<T> {
-    /// Ensure `Type` specific `TextMaxLengthConstraint` or `HashedTextMaxLengthConstraint` satisfied
-    fn ensure_property_type_size_is_valid(&self) -> Result<(), Error<T>> {
-        if let Type::Text(text_max_len) = self.0 {
-            ensure!(
-                text_max_len <= T::TextMaxLengthConstraint::get(),
-                Error::<T>::TextPropertyTooLong
-            );
-        }
-
-        if let Type::Hash(hashed_text_max_len) = self.0 {
-            ensure!(
-                hashed_text_max_len <= T::HashedTextMaxLengthConstraint::get(),
-                Error::<T>::HashedTextPropertyTooLong
-            );
-        }
-
-        Ok(())
-    }
-}
-
-impl<T: Trait> Deref for SingleValuePropertyType<T> {
-    type Target = Type<T>;
-
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-
-/// Enum, representing either `SingleValuePropertyType` or `VecPropertyType`
+/// Enum, representing either `Type` or `VecPropertyType`
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)]
 pub enum PropertyType<T: Trait> {
-    Single(SingleValuePropertyType<T>),
+    Single(Type<T>),
     Vector(VecPropertyType<T>),
 }
 
 impl<T: Trait> Default for PropertyType<T> {
     fn default() -> Self {
-        Self::Single(SingleValuePropertyType::default())
+        Self::Single(Type::default())
     }
 }
 
@@ -184,9 +156,9 @@ impl<T: Trait> PropertyType<T> {
         }
     }
 
-    fn as_vec_type(&self) -> Option<&VecPropertyType<T>> {
-        if let PropertyType::Vector(single_value_property_type) = self {
-            Some(single_value_property_type)
+    pub fn as_vec_type(&self) -> Option<&VecPropertyType<T>> {
+        if let PropertyType::Vector(vec_value_property_type) = self {
+            Some(vec_value_property_type)
         } else {
             None
         }

+ 1 - 2
runtime-modules/content-directory/src/tests/transaction.rs

@@ -7,8 +7,7 @@ fn transaction_success() {
         assert_ok!(create_simple_class(LEAD_ORIGIN, ClassType::Valid));
 
         // Create single reference property
-        let property_type_reference =
-            SingleValuePropertyType(Type::Reference(FIRST_CLASS_ID, true));
+        let property_type_reference = Type::Reference(FIRST_CLASS_ID, true);
         let property = Property::<Runtime>::with_name_and_type(
             PropertyNameLengthConstraint::get().max() as usize,
             PropertyType::Single(property_type_reference),