Skip to content

Commit

Permalink
Merge pull request #69 from CosmWasm/storage-plus-indexedmap
Browse files Browse the repository at this point in the history
Add an `IndexedMap` article (without `MultiIndex`)
  • Loading branch information
uint authored Jun 27, 2024
2 parents 059f901 + 0c5d0ad commit 2fe0897
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 7 deletions.
10 changes: 10 additions & 0 deletions docs-test-gen/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs-test-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ strum = { version = "0.26.2", features = ["derive"] }

[dev-dependencies]
cw2 = "*"
cw-storage-plus = { version = "*", features = ["macro"]}
cosmwasm-schema = "*"
cosmwasm-std = { version = "*", features = ["stargate", "staking", "cosmwasm_2_0"] }
sha2 = "0.10.8"
cosmos-sdk-proto = { version = "0.21.1", default-features = false } # Used in IBC code
cw-storage-plus = "*"
serde = "*"
cw-storey = "*"
84 changes: 80 additions & 4 deletions docs-test-gen/templates/storage.tpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,91 @@
#[allow(unused_imports)]
mod imports {
pub use cosmwasm_std::*;
pub use cosmwasm_schema::cw_serde;
pub use cosmwasm_std::*;
}

#[allow(unused_imports)]
use imports::*;

#[allow(dead_code, unused_variables)]
mod users {
use super::*;
use cw_storage_plus::{index_list, IndexedMap, UniqueIndex};

pub type Handle = String;

#[cw_serde]
pub struct User {
pub handle: String,
pub country: String,
}

pub struct ExampleUsers {
pub alice: User,
pub bob: User,
}

pub fn example_users() -> ExampleUsers {
ExampleUsers {
alice: User {
handle: "alice".to_string(),
country: "Wonderland".to_string(),
},
bob: User {
handle: "bob".to_string(),
country: "USA".to_string(),
},
}
}

#[index_list(User)]
pub struct UserIndexes<'a> {
pub handle_ix: UniqueIndex<'a, Handle, User, Addr>,
}
pub fn user_indexes(prefix: &'static str) -> UserIndexes<'static> {
UserIndexes {
handle_ix: UniqueIndex::new(|user| user.handle.clone(), prefix),
}
}
}
#[test]
fn doctest() {
#[allow(unused_mut)]
let mut storage = cosmwasm_std::testing::MockStorage::new();
{{code}}
#[allow(unused_variables, unused_mut)]
let mut storage = cosmwasm_std::testing::MockStorage::new();
let users = cw_storage_plus::IndexedMap::<Addr, _, _>::new("uu", users::user_indexes("uuh"));
let users_data = [
(
Addr::unchecked("aaa"),
users::User {
handle: "alice".to_string(),
country: "Wonderland".to_string(),
},
),
(
Addr::unchecked("bbb"),
users::User {
handle: "bob".to_string(),
country: "USA".to_string(),
},
),
(
Addr::unchecked("ccc"),
users::User {
handle: "carol".to_string(),
country: "UK".to_string(),
},
),
];
for (addr, user) in users_data {
users.save(&mut storage, addr, &user).unwrap();
}
#[rustfmt::skip]
{{code}}
}
2 changes: 1 addition & 1 deletion src/pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"core": "CosmWasm Core",
"ibc": "IBC",
"sylvia": "Sylvia",
"cw-storage-plus": "cw-storage-plus",
"cw-storage-plus": "StoragePlus",
"cw-multi-test": "MultiTest",
"how-to-doc": "How to doc",
"tags": {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/cw-storage-plus/containers/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"item": "Item",
"map": "Map",
"deque": "Deque"
"deque": "Deque",
"indexed-map": "IndexedMap"
}
Loading

0 comments on commit 2fe0897

Please sign in to comment.