git.delta.rocks / unique-network / refs/commits / b367b90fc85b

difftreelog

Merge pull request #434 from UniqueNetwork/doc/struct-versioning

Yaroslav Bolyukin2022-08-16parents: #83d3baa #03866db.patch.diff
in: master
Doc/struct versioning

2 files changed

addedcrates/struct-versioning/README.mddiffbeforeafterboth
--- /dev/null
+++ b/crates/struct-versioning/README.md
@@ -0,0 +1,62 @@
+# struct-versioning
+
+The crate contains procedural macros for versioning data structures.
+Macros [`versioned`] generate versioned variants of a struct.
+
+Example:
+```
+# use struct_versioning::versioned;
+#[versioned(version = 5, first_version = 2)]
+struct Example {}
+
+// versioned macro will generate suffixed versions of example struct,
+// starting from `Version{first_version or 1}` to `Version{version}` inclusive
+let _ver2 = ExampleVersion2 {};
+let _ver3 = ExampleVersion3 {};
+let _ver4 = ExampleVersion4 {};
+let _ver5 = ExampleVersion5 {};
+
+// last version will also be aliased with original struct name
+let _orig: Example = ExampleVersion5 {};
+
+#[versioned(version = 2, upper)]
+#[derive(PartialEq, Debug)]
+struct Upper {
+    #[version(..2)]
+    removed: u32,
+    #[version(2.., upper(10))]
+    added: u32,
+
+    #[version(..2)]
+    retyped: u32,
+    #[version(2.., upper(retyped as u64))]
+    retyped: u64,
+}
+
+// #[version] attribute on field allows to specify, in which versions of structs this field should present
+// versions here works as standard rust ranges, start is inclusive, end is exclusive
+let _up1 = UpperVersion1 {removed: 1, retyped: 0};
+let _up2 = UpperVersion2 {added: 1, retyped: 0};
+
+// and upper() allows to specify, which value should be assigned to this field in `From<OldVersion>` impl
+assert_eq!(
+    UpperVersion2::from(UpperVersion1 {removed: 0, retyped: 6}),
+    UpperVersion2 {added: 10, retyped: 6},
+);
+```
+
+In this case, the upgrade is described in `on_runtime_upgrade` using the `translate_values` substrate feature
+
+```ignore
+#[pallet::hooks]
+impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
+    fn on_runtime_upgrade() -> Weight {
+        if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {
+            <TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {
+                Some(<ItemDataVersion2>::from(v))
+            })
+        }
+        0
+    }
+}
+```
modifiedcrates/struct-versioning/src/lib.rsdiffbeforeafterboth
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
2// This file is part of Unique Network.
3
4// Unique Network is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Unique Network is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
16
17#![doc = include_str!("../README.md")]
18
1use proc_macro::TokenStream;19use proc_macro::TokenStream;
2use quote::format_ident;20use quote::format_ident;