-
Notifications
You must be signed in to change notification settings - Fork 366
Add proc macro package for automatic IndexList<T> implementation
#737
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc8d166
Add storage macro package
y-pakorn fbc3082
Add reexport macro to cw-storage-plus package
y-pakorn 6743898
Update notice and readme
y-pakorn 5a5b173
move struct position to cover code coverage
y-pakorn 3f29e20
remove macro crate features
y-pakorn 049bcb0
Apply suggestions from code review
y-pakorn 07f6cac
Add cfg(test) / mod test to storage-macro tests
43ad1e8
Rename index_list tests for clarity
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| [package] | ||
| name = "cw-storage-macro" | ||
| version = "0.13.4" | ||
| authors = ["yoisha <48324733+y-pakorn@users.noreply.github.com>"] | ||
| edition = "2018" | ||
| description = "Macro helpers for storage-plus" | ||
| license = "Apache-2.0" | ||
| repository = "https://github.com/CosmWasm/cw-plus" | ||
| homepage = "https://cosmwasm.com" | ||
| documentation = "https://docs.cosmwasm.com" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| syn = { version = "1.0.96", features = ["full"] } | ||
|
|
||
| [dev-dependencies] | ||
| cw-storage-plus = { version = "0.13.4", path = "../storage-plus" } | ||
| cosmwasm-std = { version = "1.0.0", default-features = false } | ||
| serde = { version = "1.0.103", default-features = false, features = ["derive"] } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| CW-Storage-Macro: Macro helpers for storage-plus | ||
| Copyright (C) 2022 Confio GmbH | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # CW-Storage-Plus: Macro helpers for storage-plus | ||
|
|
||
| Procedural macros helper for interacting with cw-storage-plus and cosmwasm-storage. | ||
|
|
||
| ## Current features | ||
|
|
||
| Auto generate an `IndexList` impl for your indexes struct. | ||
|
|
||
| ```rust | ||
| #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
| struct TestStruct { | ||
| id: u64, | ||
| id2: u32, | ||
| addr: Addr, | ||
| } | ||
|
|
||
| #[index_list(TestStruct)] // <- Add this line right here. | ||
| struct TestIndexes<'a> { | ||
| id: MultiIndex<'a, u32, TestStruct, u64>, | ||
| addr: UniqueIndex<'a, Addr, TestStruct>, | ||
| } | ||
| ``` |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| use proc_macro::TokenStream; | ||
| use syn::{ | ||
| Ident, | ||
| __private::{quote::quote, Span}, | ||
| parse_macro_input, ItemStruct, | ||
| }; | ||
|
|
||
| #[proc_macro_attribute] | ||
| pub fn index_list(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(item as ItemStruct); | ||
|
|
||
| let ty = Ident::new(&attr.to_string(), Span::call_site()); | ||
| let struct_ty = input.ident.clone(); | ||
|
|
||
| let names = input | ||
| .fields | ||
| .clone() | ||
| .into_iter() | ||
| .map(|e| { | ||
| let name = e.ident.unwrap(); | ||
| quote! { &self.#name } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let expanded = quote! { | ||
| #input | ||
|
|
||
| impl cw_storage_plus::IndexList<#ty> for #struct_ty<'_> { | ||
| fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn cw_storage_plus::Index<#ty>> + '_> { | ||
| let v: Vec<&dyn cw_storage_plus::Index<#ty>> = vec![#(#names),*]; | ||
| Box::new(v.into_iter()) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| TokenStream::from(expanded) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #[cfg(test)] | ||
| mod test { | ||
| use cosmwasm_std::{testing::MockStorage, Addr}; | ||
| use cw_storage_macro::index_list; | ||
| use cw_storage_plus::{IndexedMap, MultiIndex, UniqueIndex}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[test] | ||
| fn index_list_compiles() { | ||
| #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
| struct TestStruct { | ||
| id: u64, | ||
| id2: u32, | ||
| addr: Addr, | ||
| } | ||
|
|
||
| #[index_list(TestStruct)] | ||
| struct TestIndexes<'a> { | ||
| id: MultiIndex<'a, u32, TestStruct, u64>, | ||
| addr: UniqueIndex<'a, Addr, TestStruct>, | ||
| } | ||
|
|
||
| let _: IndexedMap<u64, TestStruct, TestIndexes> = IndexedMap::new( | ||
| "t", | ||
| TestIndexes { | ||
| id: MultiIndex::new(|t| t.id2, "t", "t_id2"), | ||
| addr: UniqueIndex::new(|t| t.addr.clone(), "t_addr"), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn index_list_works() { | ||
| #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
| struct TestStruct { | ||
| id: u64, | ||
| id2: u32, | ||
| addr: Addr, | ||
| } | ||
|
|
||
| #[index_list(TestStruct)] | ||
| struct TestIndexes<'a> { | ||
| id: MultiIndex<'a, u32, TestStruct, u64>, | ||
| addr: UniqueIndex<'a, Addr, TestStruct>, | ||
| } | ||
|
|
||
| let mut storage = MockStorage::new(); | ||
| let idm: IndexedMap<u64, TestStruct, TestIndexes> = IndexedMap::new( | ||
| "t", | ||
| TestIndexes { | ||
| id: MultiIndex::new(|t| t.id2, "t", "t_2"), | ||
| addr: UniqueIndex::new(|t| t.addr.clone(), "t_addr"), | ||
| }, | ||
| ); | ||
|
|
||
| idm.save( | ||
| &mut storage, | ||
| 0, | ||
| &TestStruct { | ||
| id: 0, | ||
| id2: 100, | ||
| addr: Addr::unchecked("1"), | ||
| }, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!( | ||
| idm.load(&storage, 0).unwrap(), | ||
| TestStruct { | ||
| id: 0, | ||
| id2: 100, | ||
| addr: Addr::unchecked("1"), | ||
| } | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.