@@ -6,6 +6,8 @@ import 'package:dartstruct_generator/src/name_provider.dart';
6
6
import 'package:source_gen/source_gen.dart' ;
7
7
import 'package:dartstruct/dartstruct.dart' ;
8
8
import './extensions/extensions.dart' ;
9
+ import 'models/input_source.dart' ;
10
+ import 'models/output_source.dart' ;
9
11
10
12
class DartStructGenerator extends GeneratorForAnnotation <Mapper > {
11
13
final _emitter = DartEmitter ();
@@ -131,17 +133,59 @@ class DartStructGenerator extends GeneratorForAnnotation<Mapper> {
131
133
132
134
Code _generateMethodBody (MethodElement methodElement, NameProvider nameProvider) {
133
135
134
- print (nameProvider) ;
136
+ final firstParameter = methodElement.parameters.first ;
135
137
136
- final targetType = methodElement.returnType ;
138
+ final inputSource = InputSource (firstParameter.type, firstParameter.displayName) ;
137
139
138
- final targetName = nameProvider.provideVariableName (targetType );
140
+ final outputSource = OutputSource (methodElement.returnType, nameProvider.provideVariableName (methodElement.returnType) );
139
141
140
- return Block ((builder) {
141
- builder
142
- ..addExpression (refer (targetType.element.displayName).newInstance ([]).assignFinal (targetName))
143
- ..addExpression (refer (targetName).returned);
144
- });
142
+ final setters = (outputSource.type.element as ClassElement ).fields.where ((field) => field.setter != null );
143
+
144
+ final blockBuilder = BlockBuilder ()..addExpression (_instantiateOutputOrNull (inputSource, outputSource));
145
+
146
+ for (final setter in setters) {
147
+
148
+ final mapperExpression = _getMapperExpression (setter, inputSource);
149
+
150
+ if (mapperExpression != null ) {
151
+ final assignmentExpression = refer (outputSource.name).nullSafeProperty (setter.displayName).assign (mapperExpression);
152
+ blockBuilder.addExpression (assignmentExpression);
153
+ }
154
+
155
+ }
156
+
157
+ blockBuilder.addExpression (refer (outputSource.name).returned);
158
+
159
+ return blockBuilder.build ();
160
+
161
+ }
162
+
163
+ Expression _getMapperExpression (FieldElement outputField, InputSource inputSource) {
164
+
165
+ final classElement = inputSource.type.element as ClassElement ;
166
+ final fieldName = outputField.displayName;
167
+
168
+ final inputFieldElement = classElement.fields.firstWhere ((field) => field.displayName == fieldName && field.getter != null , orElse: () => null );
169
+
170
+
171
+ if (inputFieldElement? .type == outputField.type) {
172
+ return refer (inputSource.name).nullSafeProperty (fieldName);
173
+ }
174
+
175
+ return null ;
176
+
177
+ }
178
+
179
+ /// generate expression `final {output} = {input} == null ? null : new {output}()`
180
+ Expression _instantiateOutputOrNull (InputSource input, OutputSource output) {
181
+
182
+ final nullValue = refer ('null' );
183
+ final inputValue = refer (input.name);
184
+ final newInstanceExpression = refer (output.type.element.displayName).newInstance ([]);
185
+
186
+ return inputValue
187
+ .equalTo (nullValue)
188
+ .conditional (nullValue, newInstanceExpression).assignFinal (output.name);
145
189
}
146
190
147
191
}
0 commit comments