-
Notifications
You must be signed in to change notification settings - Fork 250
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
Investigate how to reduce boilerplate that needs to be added almost to every struct #1142
Comments
I have chatted with @PolyProgrammist and we arrived to the following design: #[near]
struct Contract {
...
}
#[near(serializers = [borsh, json])]
struct Post {
author: AccountId,
text: String,
}
#[near]
impl Contract {
fn get_posts(&self) -> Vec<Post> { ... }
}
|
Just for the context, here is how I see status-message contract to be updated after these improvements land: Before: use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::store::LookupMap;
use near_sdk::{env, near_bindgen, AccountId, BorshStorageKey};
#[derive(BorshSerialize, BorshStorageKey)]
#[borsh(crate = "near_sdk::borsh")]
enum StorageKey {
StatusMessageRecords,
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
pub struct StatusMessage {
records: LookupMap<AccountId, String>,
}
impl Default for StatusMessage {
fn default() -> Self {
Self {
records: LookupMap::new(StorageKey::StatusMessageRecords),
}
}
}
#[near_bindgen]
impl StatusMessage {
pub fn set_status(&mut self, message: String) {
let account_id = env::predecessor_account_id();
self.records.insert(account_id, message);
}
pub fn get_status(&self, account_id: AccountId) -> Option<&String> {
self.records.get(&account_id)
}
} After: use near_sdk::{near, AccountId, NearStorageKey};
use near_sdk::store::LookupMap;
#[derive(NearStorageKey)]
enum StorageKey {
StatusMessageRecords,
}
#[near]
pub struct StatusMessage {
records: LookupMap<AccountId, String>,
}
#[near]
impl StatusMessage {
#[init]
pub fn new() -> Self {
Self {
records: LookupMap::new(StorageKey::StatusMessageRecords),
}
}
pub fn set_status(&mut self, message: String) {
let account_id = near_sdk::env::predecessor_account_id();
self.records.insert(account_id, message);
}
pub fn get_status(&self, account_id: AccountId) -> Option<&String> {
self.records.get(&account_id)
}
} In addition to the already discussed
|
Discussed with Vlad that we don't add NearStorageKey and Default trait for now |
The reasons are below. Default: if we do that for impl, there may be multiple definition, because there may be several impl sections. However it can't be done for structure as well, because we don't know information about the #[init] method and others As for NearStorageKey, for the following structure: pub enum StorageKey {
TokensPerOwner { account_hash: Vec<u8> },
} We would generate the following code: const _: () = {
#[allow(non_camel_case_types)]
type StorageKey_KEKEKKE = StorageKey;
{
#[derive(::near_sdk::borsh::BorshSerialize, ::near_sdk::BorshStorageKey)]
#[borsh(crate = "near_sdk::borsh")]
pub enum StorageKeyJ {
TokensPerOwner { account_hash: Vec<u8> },
}
#[automatically_derived]
impl ::near_sdk::__private::BorshIntoStorageKey for StorageKey_KEKEKKE {
}
impl From<StorageKey_KEKEKKE> for StorageKeyJ {
fn from(value: StorageKey_KEKEKKE) -> Self {
// value
match value {
StorageKey::TokensPerOwner { account_hash } => StorageKeyJ::TokensPerOwner { account_hash: account_hash.clone() }
}
}
}
impl BorshSerialize for StorageKey_KEKEKKE {
fn serialize<W: ::near_sdk::borsh::io::Write>(&self, writer: &mut W) -> Result<(), std::io::Error> {
let x: StorageKeyJ = StorageKeyJ::from(self);
let _ = BorshSerialize::serialize(&x, writer);
Ok(())
}
}
};
}; But there is an error:
|
…opment reducing the boilerplate! (#1142)
Moving the discussion from near/borsh-rs#277 since it is bigger than borsh. serde and schemars also contribute a lot to this ugly boilerplate:
The text was updated successfully, but these errors were encountered: