Skip to content

Commit 547193d

Browse files
tzakianTimothy Zakian
and
Timothy Zakian
authored
[json-rpc-types] Return variants in declaration order (#21686)
## Description A different way of returning variants in order ## Test plan Updated existing test that was added in the PR below this one. --------- Co-authored-by: Timothy Zakian <timothyzakian@mac.lan>
1 parent a998892 commit 547193d

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

crates/sui-json-rpc-types/src/sui_move.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub struct SuiMoveNormalizedEnum {
7979
pub abilities: SuiMoveAbilitySet,
8080
pub type_parameters: Vec<SuiMoveStructTypeParameter>,
8181
pub variants: BTreeMap<String, Vec<SuiMoveNormalizedField>>,
82+
#[serde(default)]
83+
pub variant_declaration_order: Option<Vec<String>>,
8284
}
8385

8486
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
@@ -244,6 +246,26 @@ impl<S: Hash + Eq + ToString> From<&NormalizedStruct<S>> for SuiMoveNormalizedSt
244246

245247
impl<S: Hash + Eq + ToString> From<&NormalizedEnum<S>> for SuiMoveNormalizedEnum {
246248
fn from(value: &NormalizedEnum<S>) -> Self {
249+
let variants = value
250+
.variants
251+
.values()
252+
.map(|variant| {
253+
(
254+
variant.name.to_string(),
255+
variant
256+
.fields
257+
.0
258+
.values()
259+
.map(|f| SuiMoveNormalizedField::from(&**f))
260+
.collect::<Vec<SuiMoveNormalizedField>>(),
261+
)
262+
})
263+
.collect::<Vec<(String, Vec<SuiMoveNormalizedField>)>>();
264+
let variant_declaration_order = variants
265+
.iter()
266+
.map(|(name, _)| name.clone())
267+
.collect::<Vec<String>>();
268+
let variants = variants.into_iter().collect();
247269
Self {
248270
abilities: value.abilities.into(),
249271
type_parameters: value
@@ -252,21 +274,8 @@ impl<S: Hash + Eq + ToString> From<&NormalizedEnum<S>> for SuiMoveNormalizedEnum
252274
.copied()
253275
.map(SuiMoveStructTypeParameter::from)
254276
.collect::<Vec<SuiMoveStructTypeParameter>>(),
255-
variants: value
256-
.variants
257-
.values()
258-
.map(|variant| {
259-
(
260-
variant.name.to_string(),
261-
variant
262-
.fields
263-
.0
264-
.values()
265-
.map(|f| SuiMoveNormalizedField::from(&**f))
266-
.collect::<Vec<SuiMoveNormalizedField>>(),
267-
)
268-
})
269-
.collect::<BTreeMap<String, Vec<SuiMoveNormalizedField>>>(),
277+
variants,
278+
variant_declaration_order: Some(variant_declaration_order),
270279
}
271280
}
272281
}

crates/sui-json-rpc-types/src/unit_tests/rpc_types_tests.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,29 +232,35 @@ fn test_move_type_serde() {
232232
},
233233
],
234234
),
235-
]
236-
.into_iter()
237-
.map(|(name, type_)| {
238-
(
239-
name.to_string(),
240-
type_
241-
.into_iter()
242-
.enumerate()
243-
.map(|(i, t)| SM::SuiMoveNormalizedField {
244-
name: format!("field{}", i),
245-
type_: t,
246-
})
247-
.collect(),
248-
)
249-
})
250-
.collect();
235+
];
236+
let variant_declaration_order = variants
237+
.iter()
238+
.map(|(name, _)| name.to_string())
239+
.collect::<Vec<_>>();
240+
let variants = variants
241+
.into_iter()
242+
.map(|(name, type_)| {
243+
(
244+
name.to_string(),
245+
type_
246+
.into_iter()
247+
.enumerate()
248+
.map(|(i, t)| SM::SuiMoveNormalizedField {
249+
name: format!("field{}", i),
250+
type_: t,
251+
})
252+
.collect(),
253+
)
254+
})
255+
.collect();
251256

252257
let e = SM::SuiMoveNormalizedEnum {
253258
abilities: SM::SuiMoveAbilitySet {
254259
abilities: vec![SM::SuiMoveAbility::Copy],
255260
},
256261
type_parameters: vec![],
257262
variants,
263+
variant_declaration_order: Some(variant_declaration_order),
258264
};
259265

260266
acc.push(serde_json::to_string(&e).unwrap());

crates/sui-json-rpc-types/src/unit_tests/snapshots/sui_json_rpc_types__rpc_types_tests__move_type_serde.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ expression: "acc.join(\"\\n\")"
1919
{"Reference":"U8"}
2020
{"MutableReference":{"Struct":{"address":"0000000000000000000000000000000000000000000000000000000000000002","module":"coin","name":"Coin","typeArguments":["Address"]}}}
2121
{"abilities":{"abilities":["Copy"]},"typeParameters":[{"constraints":{"abilities":["Drop"]},"isPhantom":false}],"fields":[{"name":"field1","type":"U8"},{"name":"field2","type":"U16"}]}
22-
{"abilities":{"abilities":["Copy"]},"typeParameters":[],"variants":{"a":[],"b":[{"name":"field0","type":"U16"}],"c":[{"name":"field0","type":"U32"},{"name":"field1","type":{"Struct":{"address":"0000000000000000000000000000000000000000000000000000000000000002","module":"coin","name":"Coin","typeArguments":["Address"]}}}]}}
22+
{"abilities":{"abilities":["Copy"]},"typeParameters":[],"variants":{"a":[],"b":[{"name":"field0","type":"U16"}],"c":[{"name":"field0","type":"U32"},{"name":"field1","type":{"Struct":{"address":"0000000000000000000000000000000000000000000000000000000000000002","module":"coin","name":"Coin","typeArguments":["Address"]}}}]},"variantDeclarationOrder":["b","a","c"]}

crates/sui-open-rpc/spec/openrpc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9040,6 +9040,16 @@
90409040
"$ref": "#/components/schemas/SuiMoveStructTypeParameter"
90419041
}
90429042
},
9043+
"variantDeclarationOrder": {
9044+
"default": null,
9045+
"type": [
9046+
"array",
9047+
"null"
9048+
],
9049+
"items": {
9050+
"type": "string"
9051+
}
9052+
},
90439053
"variants": {
90449054
"type": "object",
90459055
"additionalProperties": {

0 commit comments

Comments
 (0)