File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -1047,6 +1047,38 @@ switch (this) {{"#,
10471047 self . enter_class ( name) ;
10481048 writeln ! ( self . out, "const {}();" , self . quote_qualified_name( name) ) ?;
10491049
1050+ // if all variants are structs, output base class getters for properties that
1051+ // have the same name and type across every variant
1052+ let shared_fields = variants
1053+ . values ( )
1054+ . enumerate ( )
1055+ . fold ( vec ! [ ] , |shared_fields, enumerated_variant| {
1056+ if let VariantFormat :: Struct ( fields) = & enumerated_variant. 1 . value {
1057+ if enumerated_variant. 0 == 0 {
1058+ let mut cp = fields. to_vec ( ) ;
1059+ cp. sort_by_key ( |field| field. name . to_owned ( ) ) ;
1060+ cp
1061+ } else {
1062+ shared_fields. into_iter ( ) . filter ( |field| fields. contains ( field) ) . collect ( )
1063+ }
1064+ } else {
1065+ vec ! [ ]
1066+ }
1067+ } ) ;
1068+
1069+ if !shared_fields. is_empty ( ) {
1070+ writeln ! ( self . out) ?;
1071+ }
1072+
1073+ for field in & shared_fields {
1074+ writeln ! (
1075+ self . out,
1076+ "{} get {};" ,
1077+ self . quote_type( & field. value) ,
1078+ self . quote_field( & field. name. to_mixed_case( ) ) ,
1079+ ) ?;
1080+ }
1081+
10501082 if self . generator . config . serialization {
10511083 writeln ! ( self . out, "\n void serialize(BinarySerializer serializer);" ) ?;
10521084 write ! (
Original file line number Diff line number Diff line change @@ -119,6 +119,25 @@ pub struct Tree<T> {
119119#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
120120pub struct SimpleList ( Option < Box < SimpleList > > ) ;
121121
122+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
123+ pub enum ComplexEnum {
124+ A {
125+ id : String ,
126+ value : String ,
127+ a : String ,
128+ } ,
129+ B {
130+ id : String ,
131+ value : i32 ,
132+ b : String ,
133+ } ,
134+ C {
135+ id : String ,
136+ value : bool ,
137+ c : String ,
138+ } ,
139+ }
140+
122141#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
123142pub enum CStyleEnum {
124143 A ,
@@ -135,6 +154,7 @@ pub fn get_registry() -> Result<Registry> {
135154 tracer. trace_type :: < SerdeData > ( & samples) ?;
136155 tracer. trace_type :: < List < SerdeData > > ( & samples) ?;
137156 tracer. trace_type :: < CStyleEnum > ( & samples) ?;
157+ tracer. trace_type :: < ComplexEnum > ( & samples) ?;
138158 tracer. registry ( )
139159}
140160
Original file line number Diff line number Diff line change @@ -122,3 +122,27 @@ fn test_dart_code_compiles_class_enums_for_complex_enums() {
122122 assert ! ( generated_c_style. contains( "enum CStyleEnum {" ) ) ;
123123 assert ! ( generated_class_style. contains( "abstract class List_ {" ) ) ;
124124}
125+
126+ #[ test]
127+ fn test_dart_code_includes_getters_for_shared_properties_of_complex_enums ( ) {
128+ let source_path = tempdir ( ) . unwrap ( ) . path ( ) . join ( "dart_class_enum_shared_properties_project" ) ;
129+ print ! ( "{:?}" , source_path) ;
130+
131+ let config = CodeGeneratorConfig :: new ( "example" . to_string ( ) )
132+ . with_encodings ( vec ! [ Encoding :: Bcs , Encoding :: Bincode ] )
133+ // we enable native Dart enums to test that complex Rust enums will still produce Dart classes
134+ . with_c_style_enums ( true ) ;
135+
136+ generate_with_config ( source_path. clone ( ) , & config) ;
137+
138+ let generated_class_style =
139+ read_to_string ( & source_path. join ( "lib/src/example/complex_enum.dart" ) ) . unwrap ( ) ;
140+
141+ assert ! ( generated_class_style. contains( "String get id;\n " ) ) ;
142+ assert ! ( !generated_class_style. contains( "String get value;\n " ) ) ;
143+ assert ! ( !generated_class_style. contains( "int get value;\n " ) ) ;
144+ assert ! ( !generated_class_style. contains( "bool get value;\n " ) ) ;
145+ assert ! ( !generated_class_style. contains( "String get a;\n " ) ) ;
146+ assert ! ( !generated_class_style. contains( "String get b;\n " ) ) ;
147+ assert ! ( !generated_class_style. contains( "String get c;\n " ) ) ;
148+ }
You can’t perform that action at this time.
0 commit comments