Skip to content

Commit

Permalink
fix(sdk): option not serialized properly (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Oct 28, 2024
1 parent a0b2427 commit 4f2bb21
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/dapi-grpc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,37 @@ where
impl<T: Mockable> Mockable for Option<T> {
#[cfg(feature = "mocks")]
fn mock_serialize(&self) -> Option<Vec<u8>> {
self.as_ref().and_then(|value| value.mock_serialize())
let data = match self {
None => vec![0],
Some(value) => {
let mut data = vec![1]; // we return None if value does not support serialization
let mut serialized = value.mock_serialize()?;
data.append(&mut serialized);

data
}
};

Some(data)
}

#[cfg(feature = "mocks")]
fn mock_deserialize(data: &[u8]) -> Option<Self> {
T::mock_deserialize(data).map(Some)
if data.is_empty() {
panic!("empty data");
}

match data[0] {
0 => Some(None),
// Mind double Some - first says mock_deserialize is implemented, second is deserialized value
1 => Some(Some(
T::mock_deserialize(&data[1..]).expect("unable to deserialize Option<T>"),
)),
_ => panic!(
"unsupported first byte for Option<T>::mock_deserialize: {:x}",
data[0]
),
}
}
}

Expand Down

0 comments on commit 4f2bb21

Please sign in to comment.