Skip to content
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

Add an IndexedMap article (without MultiIndex) #69

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 +13,9 @@ 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 = "*"
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