Skip to content

Commit 469da94

Browse files
committed
fix
1 parent 1c5f1f7 commit 469da94

File tree

1 file changed

+50
-17
lines changed
  • packages/rs-dpp/src/data_contract/serialized_version/v1

1 file changed

+50
-17
lines changed

packages/rs-dpp/src/data_contract/serialized_version/v1/mod.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::data_contract::document_type::accessors::DocumentTypeV0Getters;
44

55
use crate::block::epoch::EpochIndex;
66
use crate::data_contract::associated_token::token_configuration::TokenConfiguration;
7+
use crate::data_contract::group::v0::GroupV0;
78
use crate::data_contract::group::Group;
89
use crate::data_contract::v0::DataContractV0;
910
use crate::data_contract::v1::DataContractV1;
@@ -15,7 +16,6 @@ use crate::prelude::BlockHeight;
1516
use bincode::{Decode, Encode};
1617
use platform_value::{Identifier, Value};
1718
use serde::{Deserialize, Serialize};
18-
use serde_json::Value as JsonValue;
1919
use std::collections::BTreeMap;
2020

2121
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
@@ -70,29 +70,62 @@ pub struct DataContractInSerializationFormatV1 {
7070
pub description: Option<String>,
7171
}
7272

73+
/// Deserialize `groups` map with stringified u16 keys and enum-wrapped `Group` values.
74+
///
75+
/// Accepts:
76+
/// ```json
77+
/// {
78+
/// "0": {
79+
/// "V0": {
80+
/// "members": { "...": 1 },
81+
/// "required_power": 3
82+
/// }
83+
/// }
84+
/// }
85+
/// ```
7386
fn deserialize_u16_group_map<'de, D>(
7487
deserializer: D,
7588
) -> Result<BTreeMap<GroupContractPosition, Group>, D::Error>
7689
where
7790
D: serde::Deserializer<'de>,
7891
{
79-
let raw: Value = Value::deserialize(deserializer)?;
80-
81-
let json: JsonValue = raw
82-
.try_to_validating_json()
83-
.map_err(|e| serde::de::Error::custom(format!("groups: {}", e)))?;
84-
85-
let by_string: BTreeMap<String, Group> =
86-
serde_json::from_value(json).map_err(serde::de::Error::custom)?;
92+
let map: BTreeMap<String, Value> = BTreeMap::deserialize(deserializer)?;
93+
94+
let mut out = BTreeMap::new();
95+
96+
for (key_str, group_value) in map {
97+
// Parse key to GroupContractPosition
98+
let key = key_str.parse::<GroupContractPosition>().map_err(|e| {
99+
serde::de::Error::custom(format!("invalid group key '{}': {}", key_str, e))
100+
})?;
101+
102+
// Extract the V0 variant manually
103+
let group_obj = group_value.into_btree_string_map().map_err(|e| {
104+
serde::de::Error::custom(format!("invalid group structure at '{}': {}", key_str, e))
105+
})?;
106+
107+
let v0_payload = group_obj.get("V0").ok_or_else(|| {
108+
serde::de::Error::custom(format!("missing 'V0' variant under group '{}'", key_str))
109+
})?;
110+
111+
// Deserialize the GroupV0 struct and wrap in Group::V0
112+
let serde_value: serde_json::Value = v0_payload.clone().try_into().map_err(|e| {
113+
serde::de::Error::custom(format!(
114+
"failed to convert platform_value::Value to serde_json::Value at '{}': {}",
115+
key_str, e
116+
))
117+
})?;
118+
let group_v0: GroupV0 = GroupV0::deserialize(&serde_value).map_err(|e| {
119+
serde::de::Error::custom(format!(
120+
"failed to deserialize GroupV0 at '{}': {}",
121+
key_str, e
122+
))
123+
})?;
124+
125+
out.insert(key, Group::V0(group_v0));
126+
}
87127

88-
by_string
89-
.into_iter()
90-
.map(|(k, v)| {
91-
k.parse::<GroupContractPosition>()
92-
.map(|pos| (pos, v))
93-
.map_err(|e| serde::de::Error::custom(format!("invalid group key '{}': {}", k, e)))
94-
})
95-
.collect()
128+
Ok(out)
96129
}
97130

98131
fn deserialize_u16_token_configuration_map<'de, D>(

0 commit comments

Comments
 (0)