@@ -4,6 +4,7 @@ import 'package:code_builder/code_builder.dart';
44import 'package:dart_style/dart_style.dart' ;
55import 'package:source_gen/source_gen.dart' ;
66import 'package:dartstruct/dartstruct.dart' ;
7+ import './extensions/extensions.dart' ;
78
89class DartStructGenerator extends GeneratorForAnnotation <Mapper > {
910 final _emitter = DartEmitter ();
@@ -32,7 +33,7 @@ class DartStructGenerator extends GeneratorForAnnotation<Mapper> {
3233 ..name = '${classElement .displayName }Impl'
3334 ..abstract = false
3435 ..extend = refer (classElement.displayName)
35- ..methods.addAll (_generateMethods ( classElement.methods));
36+ ..methods.addAll (classElement.methods. where ((method) => method.isAbstract). map (_generateMethod ));
3637
3738 final mapperImpl = classBuilder.build ();
3839
@@ -42,13 +43,78 @@ class DartStructGenerator extends GeneratorForAnnotation<Mapper> {
4243
4344 }
4445
45- List <Method > _generateMethods (List <MethodElement > methods) {
46- if (methods.any ((method) => method.isAbstract)) {
47- throw InvalidGenerationSourceError ('Not implemented yet' ,
48- todo: 'remove all methods'
46+ Method _generateMethod (MethodElement method) {
47+
48+ if (method.parameters.isEmpty) {
49+ throw InvalidGenerationSourceError ('Method must provide an argument' ,
50+ todo: 'add source parameter' ,
51+ element: method
52+ );
53+ }
54+
55+ if (method.parameters.length != 1 ) {
56+ throw InvalidGenerationSourceError ('Method must provide only one argument' ,
57+ todo: 'provide only one argument' ,
58+ element: method
59+ );
60+ }
61+
62+ final source = method.parameters.first;
63+
64+ if (source.isNamed) {
65+ throw InvalidGenerationSourceError ('Named parameters are not supported' ,
66+ todo: 'provide a positional argument' ,
67+ element: source
68+ );
69+ }
70+
71+ final sourceType = source.type;
72+ final returnType = method.returnType;
73+
74+ if (returnType.isPrimitive || sourceType.isPrimitive) {
75+ throw InvalidGenerationSourceError ('Primitive types are not supported' ,
76+ element: method
77+ );
78+ }
79+
80+ if (sourceType.isFuture || returnType.isFuture) {
81+ throw InvalidGenerationSourceError ('Future types are not supported' ,
82+ element: method
4983 );
5084 }
5185
52- return < Method > [];
86+ if (sourceType.isCollection || returnType.isCollection) {
87+ throw InvalidGenerationSourceError ('Collection types are not supported' ,
88+ element: method
89+ );
90+ }
91+
92+ if (sourceType.isDynamic || returnType.isDynamic) {
93+ throw InvalidGenerationSourceError ('Dynamic type is not supported' ,
94+ element: method
95+ );
96+ }
97+
98+ return Method ((builder) {
99+ builder
100+ ..annotations.add (CodeExpression (Code ('override' )))
101+ ..name = method.displayName
102+ ..requiredParameters.add (_generateSourceParameter (source))
103+ ..returns = refer (returnType.element.displayName)
104+ ..body = _generateMethodBody (method);
105+ });
106+ }
107+
108+ Parameter _generateSourceParameter (ParameterElement source) {
109+ return Parameter ((builder) {
110+ builder
111+ ..name = source.name
112+ ..type = refer (source.type.element.displayName);
113+ });
53114 }
115+
116+ Code _generateMethodBody (MethodElement methodElement) {
117+ return Code ('return null;' );
118+ }
119+
54120}
0 commit comments