Skip to content

Commit

Permalink
feat: enable expand_as_any deserialize a pair of type_url and value
Browse files Browse the repository at this point in the history
Signed-off-by: 170210 <j170210@icloud.com>
  • Loading branch information
170210 committed Feb 29, 2024
1 parent 3b57fee commit 5854ce4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions packages/finschia-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ prost = { version = "0.12.3", default-features = false, features = [
] }
prost-types = { version = "0.12.3", default-features = false }
schemars = "0.8.8"
serde_json = "1.0.40"

# for query
serde = { version = "1.0", default-features = false, features = ["derive"] }
Expand Down
35 changes: 25 additions & 10 deletions packages/finschia-std/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,18 @@ macro_rules! expand_as_any {
}
};

let m = match value {
serde_cw_value::Value::Map(ref m) => m,
_ => return Err(serde::de::Error::custom("data must have map structure")),
};

// must be map er else error
let type_url = if let serde_cw_value::Value::Map(m) = value.clone() {
m.get(&serde_cw_value::Value::String("@type".to_string()))
.map(|t| match t.to_owned() {
serde_cw_value::Value::String(s) => Ok(s),
_ => Err(serde::de::Error::custom("type_url must be String")),
})
.transpose()
} else {
Err(serde::de::Error::custom("data must have map structure"))
}?;
let type_url = m.get(&serde_cw_value::Value::String("@type".to_string()))
.map(|t| match t.to_owned() {
serde_cw_value::Value::String(s) => Ok(s),
_ => Err(serde::de::Error::custom("type_url must be String")),
})
.transpose()?;

match type_url {
// @type found
Expand Down Expand Up @@ -268,6 +269,20 @@ macro_rules! expand_as_any {
});
}
)*

let new_type_url = m.get(&serde_cw_value::Value::String("type_url".to_string()))
.and_then(|t| match t {
serde_cw_value::Value::String(s) => Some(s.clone()),
_ => None,
});
let new_value = serde_json::to_vec(&m).ok();

if let (Some(type_url), Some(value)) = (new_type_url, new_value) {
return Ok(Any {
type_url,
value,
});
}
}
};

Expand Down

0 comments on commit 5854ce4

Please sign in to comment.