Skip to content

Commit 2ce242e

Browse files
authored
Fix Dart list equality (#36)
* Add Dart test for optional vec * Fix Dart bug where optional maps/lists weren't properly checked by == * Update registry yaml to match registry data
1 parent 4f2b294 commit 2ce242e

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

serde-generate/src/dart.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,15 @@ return obj;
817817

818818
self.out.indent();
819819
for field in fields.iter() {
820-
let stmt = match &field.value {
820+
// because the Dart functions of listEquals and mapEquals accept nullable
821+
// we only care about the data type and can discard the enclosing Format::Option
822+
let value = if let Format::Option(value) = &field.value {
823+
value
824+
} else {
825+
&field.value
826+
};
827+
828+
let stmt = match value {
821829
Format::Seq(_) => {
822830
format!(
823831
"listEquals({0}, other.{0})",

serde-generate/tests/dart_generation.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ fn test_dart_code_compiles() {
5555
.with_encodings(vec![Encoding::Bcs, Encoding::Bincode])
5656
.with_c_style_enums(true);
5757

58-
generate_with_config(source_path, &config);
58+
generate_with_config(source_path.clone(), &config);
59+
60+
let other_types =
61+
read_to_string(&source_path.join("lib/src/example/other_types.dart")).unwrap();
62+
63+
assert!(other_types.contains("listEquals(fOptSeq, other.fOptSeq)"));
5964
}
6065

6166
#[test]

serde-generate/tests/test_utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub struct OtherTypes {
8383
f_option: Option<Struct>,
8484
f_unit: (),
8585
f_seq: Vec<Struct>,
86+
f_opt_seq: Option<Vec<i32>>,
8687
f_tuple: (u8, u16),
8788
f_stringmap: BTreeMap<String, u32>,
8889
f_intset: BTreeMap<u64, ()>, // Avoiding BTreeSet because Serde treats them as sequences.
@@ -182,6 +183,7 @@ pub fn get_sample_values(has_canonical_maps: bool, has_floats: bool) -> Vec<Serd
182183
f_option: Some(Struct { x: 2, y: 3 }),
183184
f_unit: (),
184185
f_seq: vec![Struct { x: 1, y: 3 }],
186+
f_opt_seq: Some(vec![1]),
185187
f_tuple: (4, 5),
186188
f_stringmap: if has_canonical_maps {
187189
btreemap! {"foo".to_string() => 1, "bar".to_string() => 2}
@@ -201,6 +203,7 @@ pub fn get_sample_values(has_canonical_maps: bool, has_floats: bool) -> Vec<Serd
201203
f_option: None,
202204
f_unit: (),
203205
f_seq: Vec::new(),
206+
f_opt_seq: None,
204207
f_tuple: (4, 5),
205208
f_stringmap: BTreeMap::new(),
206209
f_intset: if has_canonical_maps {
@@ -217,6 +220,7 @@ pub fn get_sample_values(has_canonical_maps: bool, has_floats: bool) -> Vec<Serd
217220
f_option: None,
218221
f_unit: (),
219222
f_seq: Vec::new(),
223+
f_opt_seq: None,
220224
f_tuple: (4, 5),
221225
f_stringmap: BTreeMap::new(),
222226
f_intset: if has_canonical_maps {
@@ -675,6 +679,9 @@ OtherTypes:
675679
- f_seq:
676680
SEQ:
677681
TYPENAME: Struct
682+
- f_opt_seq:
683+
OPTION:
684+
SEQ: I32
678685
- f_tuple:
679686
TUPLE:
680687
- U8

0 commit comments

Comments
 (0)