File tree 3 files changed +83
-0
lines changed 3 files changed +83
-0
lines changed Original file line number Diff line number Diff line change @@ -1047,6 +1047,42 @@ switch (this) {{"#,
1047
1047
self . enter_class ( name) ;
1048
1048
writeln ! ( self . out, "const {}();" , self . quote_qualified_name( name) ) ?;
1049
1049
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 =
1053
+ variants
1054
+ . values ( )
1055
+ . enumerate ( )
1056
+ . fold ( vec ! [ ] , |shared_fields, enumerated_variant| {
1057
+ if let VariantFormat :: Struct ( fields) = & enumerated_variant. 1 . value {
1058
+ if enumerated_variant. 0 == 0 {
1059
+ let mut cp = fields. to_vec ( ) ;
1060
+ cp. sort_by_key ( |field| field. name . to_owned ( ) ) ;
1061
+ cp
1062
+ } else {
1063
+ shared_fields
1064
+ . into_iter ( )
1065
+ . filter ( |field| fields. contains ( field) )
1066
+ . collect ( )
1067
+ }
1068
+ } else {
1069
+ vec ! [ ]
1070
+ }
1071
+ } ) ;
1072
+
1073
+ if !shared_fields. is_empty ( ) {
1074
+ writeln ! ( self . out) ?;
1075
+ }
1076
+
1077
+ for field in & shared_fields {
1078
+ writeln ! (
1079
+ self . out,
1080
+ "{} get {};" ,
1081
+ self . quote_type( & field. value) ,
1082
+ self . quote_field( & field. name. to_mixed_case( ) ) ,
1083
+ ) ?;
1084
+ }
1085
+
1050
1086
if self . generator . config . serialization {
1051
1087
writeln ! ( self . out, "\n void serialize(BinarySerializer serializer);" ) ?;
1052
1088
write ! (
Original file line number Diff line number Diff line change @@ -119,6 +119,25 @@ pub struct Tree<T> {
119
119
#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
120
120
pub struct SimpleList ( Option < Box < SimpleList > > ) ;
121
121
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
+
122
141
#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
123
142
pub enum CStyleEnum {
124
143
A ,
@@ -135,6 +154,7 @@ pub fn get_registry() -> Result<Registry> {
135
154
tracer. trace_type :: < SerdeData > ( & samples) ?;
136
155
tracer. trace_type :: < List < SerdeData > > ( & samples) ?;
137
156
tracer. trace_type :: < CStyleEnum > ( & samples) ?;
157
+ tracer. trace_type :: < ComplexEnum > ( & samples) ?;
138
158
tracer. registry ( )
139
159
}
140
160
Original file line number Diff line number Diff line change @@ -122,3 +122,30 @@ fn test_dart_code_compiles_class_enums_for_complex_enums() {
122
122
assert ! ( generated_c_style. contains( "enum CStyleEnum {" ) ) ;
123
123
assert ! ( generated_class_style. contains( "abstract class List_ {" ) ) ;
124
124
}
125
+
126
+ #[ test]
127
+ fn test_dart_code_includes_getters_for_shared_properties_of_complex_enums ( ) {
128
+ let source_path = tempdir ( )
129
+ . unwrap ( )
130
+ . path ( )
131
+ . join ( "dart_class_enum_shared_properties_project" ) ;
132
+ print ! ( "{:?}" , source_path) ;
133
+
134
+ let config = CodeGeneratorConfig :: new ( "example" . to_string ( ) )
135
+ . with_encodings ( vec ! [ Encoding :: Bcs , Encoding :: Bincode ] )
136
+ // we enable native Dart enums to test that complex Rust enums will still produce Dart classes
137
+ . with_c_style_enums ( true ) ;
138
+
139
+ generate_with_config ( source_path. clone ( ) , & config) ;
140
+
141
+ let generated_class_style =
142
+ read_to_string ( & source_path. join ( "lib/src/example/complex_enum.dart" ) ) . unwrap ( ) ;
143
+
144
+ assert ! ( generated_class_style. contains( "String get id;\n " ) ) ;
145
+ assert ! ( !generated_class_style. contains( "String get value;\n " ) ) ;
146
+ assert ! ( !generated_class_style. contains( "int get value;\n " ) ) ;
147
+ assert ! ( !generated_class_style. contains( "bool get value;\n " ) ) ;
148
+ assert ! ( !generated_class_style. contains( "String get a;\n " ) ) ;
149
+ assert ! ( !generated_class_style. contains( "String get b;\n " ) ) ;
150
+ assert ! ( !generated_class_style. contains( "String get c;\n " ) ) ;
151
+ }
You can’t perform that action at this time.
0 commit comments