-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #737 from y-pakorn/feat/storage-macro
Add proc macro package for automatic `IndexList<T>` implementation
- Loading branch information
Showing
8 changed files
with
202 additions
and
12 deletions.
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 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 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 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 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 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 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 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