recipes
recipes copied to clipboard
storage::maps with values wrapped in option
I've noticed that it's common for maps to initialize values as wrapped in an Option so that there is explicit handling of calls to storage in which the value does not exist. The calls to get the storage value often look like
if let Some(value) = <StorageValue<T>>::get(&key) {
// some logic to use the value in the option
} else {
// some logic to handle when the option is `None`
}
This could also be expressed as a match statement,
let v = <StorageValue<T>>::get(&key);
match v {
Some(value) => {}, // some logic to use the value in the option
None = {}, //some logic to use the value in the option
}
Or the most simple,
// inside a runtime method that returns a `support::dispatch::Result`
let v = <StorageValue<T>>::get(&key).ok_or("error message for None on call to storage")?;