Skip to content

Commit

Permalink
clnrs: Implement backwards compatible mode for Amount as bare u64
Browse files Browse the repository at this point in the history
Since we want to transition to raw `u64` values we need to accept both
for some time.
  • Loading branch information
cdecker authored and rustyrussell committed Jun 17, 2022
1 parent 3570e07 commit 8037b56
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,31 @@ impl<'de> Deserialize<'de> for Amount {
D: Deserializer<'de>,
{
use serde::de::Error;
let s: String = Deserialize::deserialize(deserializer)?;
let ss: &str = &s;
ss.try_into()
.map_err(|_e| Error::custom("could not parse amount"))

let any: serde_json::Value = Deserialize::deserialize(deserializer)?;

// Amount fields used to be a string with the unit "msat" or
// "sat" as a suffix. The great consolidation in PR #5306
// changed that to always be a `u64`, but for backwards
// compatibility we need to handle both cases.
let ires: Option<u64> = any.as_u64();
// TODO(cdecker): Remove string parsing support once the great msat purge is complete
let sres: Option<&str> = any.as_str();

match (ires, sres) {
(Some(i), _) => {
// Notice that this assumes the field is denominated in `msat`
Ok(Amount::from_msat(i))
}
(_, Some(s)) => s
.try_into()
.map_err(|_e| Error::custom("could not parse amount")),
(None, _) => {
// We reuse the integer parsing error as that's the
// default after the great msat purge of 2022.
Err(Error::custom("could not parse amount"))
}
}
}
}

Expand Down

0 comments on commit 8037b56

Please sign in to comment.