Closed Nuttymoon closed 1 year ago
Rebased on main
@gyuho will you have a chance to take a look at the PR? Would be happy to implement any corrections if needed!
Hey @Nuttymoon, thanks for the contribution 🎉.
Could you provide a reproducible example(maybe on another branch) of how deserialization failed using ureq
? We shouldn't need that extra allocation deserializing into a String first.
Thanks in advance!
Hey @Nuttymoon, thanks for the contribution 🎉.
Could you provide a reproducible example(maybe on another branch) of how deserialization failed using
ureq
? We shouldn't need that extra allocation deserializing into a String first.Thanks in advance!
Hey @richardpringle ! Yes I guess I can create another branch here without the String
deserialization and one on my repo (that uses ureq
). I will ping you when I have done that!
Btw maybe my issue comes from the fact that I am using ureq
for deserializing to a struct that contains String
fields through serde
. You can take a look at the workaround I had to implement here:
And how I deserialize:
(The deserialization process is currently being refactored but you get the idea)
@Nuttymoon Thanks for the contribution! And apologies for the late response.
I cherry-picked your patch here https://github.com/ava-labs/avalanche-types-rs/pull/67.
And will cut a new cargo release, so you can try.
Just published https://crates.io/crates/avalanche-types/0.0.379.
Please try and let us know if you find any issue.
@Nuttymoon, I see the issue with deserialization now. If you want to open a new PR, you can solve it with the following code (or something similar)
impl<'de> Deserialize<'de> for Id {
fn deserialize<D>(deserializer: D) -> std::result::Result<Id, D::Error>
where
D: Deserializer<'de>,
{
struct StringOrStr;
impl<'de> Visitor<'de> for StringOrStr {
type Value = Cow<'de, str>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("borrowed or owned string")
}
fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Cow::Owned(v))
}
fn visit_borrowed_str<E>(self, v: &'de str) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Cow::Borrowed(v))
}
}
let s = deserializer.deserialize_any(StringOrStr)?;
Id::from_str(&s).map_err(serde::de::Error::custom)
}
}
The solution should also include a test using serde_json::from_value
for deserialization.
The more complex solution is that we do not want to perform any unnecessary allocations. Just let me know if you would rather I write the fix and accompanying test, but I figured I would give you an opportunity to make the contribution since you're the one that found the limitation.
Hey, @richardpringle Thanks for your answer! I will open a new PR based on the code above. To be honest I am still learning Rust so I could not have come up with that on my own :sweat_smile:
Description
This PR is the first effort to update
jsonrpc
structs to match the recent API updates. See #33 for more details.Note: I only focused on
ApiPrimaryValidator
andApiPrimaryDelegator
for now.Changes
ids
andids::node
: Allow to deserializeId
s fromString
. I had problems usingureq
with the previous implementation that only supported&str
.ApiPrimaryValidator
:staked
,reward_owner
validation_reward_owner
,delegation_reward_owner
,delegator_count
,delegator_weight
,signer
Option
for fields:connected
anduptime
that are not optional (based on the API docs)serde(rename_all = "camelCase")
to make struct more readableApiPrimaryDelegator
:weight
Notes
I used the platform.getCurrentValidators docs as a reference to update the fields.
Tests from
scripts/tests.unit.sh
are passing which make me believe that my changes on theId
s deserializers have no impact