Skip to content

Commit

Permalink
use types implemented with JsonSchema trait
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjiao committed Aug 28, 2023
1 parent 0dce022 commit 190e29c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion rpc/api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub trait StateApi {

/// Return the TableInfo according to queried AccountAddress
#[rpc(name = "state.get_table_info")]
fn get_table_info(&self, address: AccountAddress) -> FutureResult<TableInfoView>;
fn get_table_info(&self, address: AccountAddress) -> FutureResult<Option<TableInfoView>>;

/// Return the TableItem value and provide a State Proof at `state_root`
#[rpc(name = "state.get_with_table_item_proof")]
Expand Down
36 changes: 17 additions & 19 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,15 +1903,14 @@ impl From<BlockInfo> for BlockInfoView {
#[serde(rename = "table_item")]
pub struct TableItemView {
handle: TableHandle,
#[schemars(with = "String")]
key: Vec<u8>,
key: StrView<Vec<u8>>,
}

impl From<TableItem> for TableItemView {
fn from(table_item: TableItem) -> Self {
Self {
handle: table_item.handle,
key: table_item.key,
key: table_item.key.into(),
}
}
}
Expand All @@ -1930,35 +1929,34 @@ impl From<StateKey> for StateKeyView {
StateKey::AccessPath(access_path) => Self::AccessPath(access_path),
StateKey::TableItem(table_item) => Self::TableItem(TableItemView {
handle: table_item.handle,
key: table_item.key,
key: table_item.key.into(),
}),
}
}
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename = "table_info")]
pub struct TableInfoView {
#[schemars(with = "String")]
#[serde(rename = "table_info")]
inner: Option<TableInfo>,
key_type: TypeTagView,
value_type: TypeTagView,
}

impl TableInfoView {
pub fn new(inner: Option<TableInfo>) -> Self {
Self { inner }
impl From<TableInfo> for TableInfoView {
fn from(value: TableInfo) -> Self {
Self {
key_type: value.key_type.into(),
value_type: value.value_type.into(),
}
}
}

impl From<TableInfoView> for Option<TableInfo> {
impl From<TableInfoView> for TableInfo {
fn from(value: TableInfoView) -> Self {
value.inner
}
}

impl TryInto<TableInfo> for TableInfoView {
type Error = String;
fn try_into(self) -> Result<TableInfo, Self::Error> {
self.inner.ok_or_else(|| "Null".to_string())
Self {
key_type: value.key_type.0,
value_type: value.value_type.0,
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,10 @@ impl RpcClient {
.map_err(map_err)
}

pub fn state_get_table_info(&self, address: AccountAddress) -> anyhow::Result<TableInfoView> {
pub fn state_get_table_info(
&self,
address: AccountAddress,
) -> anyhow::Result<Option<TableInfoView>> {
self.call_rpc_blocking(|inner| inner.state_client.get_table_info(address))
.map_err(map_err)
}
Expand Down
4 changes: 3 additions & 1 deletion rpc/client/src/remote_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ impl<'a> ChainStateReader for RemoteStateReader<'a> {
.map(Into::into)
}
fn get_table_info(&self, address: AccountAddress) -> Result<Option<TableInfo>> {
self.client.state_get_table_info(address).map(Into::into)
self.client
.state_get_table_info(address)
.map(|v| v.map(Into::into))
}
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/server/src/module/state_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ where
Box::pin(fut)
}

fn get_table_info(&self, address: AccountAddress) -> FutureResult<TableInfoView> {
fn get_table_info(&self, address: AccountAddress) -> FutureResult<Option<TableInfoView>> {
let fut = self
.service
.clone()
.get_table_info(address)
.map_ok(TableInfoView::new)
.map_ok(|v| v.map(Into::into))
.map_err(map_err);
Box::pin(fut)
}
Expand Down

0 comments on commit 190e29c

Please sign in to comment.