@@ -3,9 +3,10 @@ import 'dart:io';
33
44import 'package:collection/collection.dart' ;
55import 'package:onix_flutter_bricks/domain/entity/data_component/data_component.dart' ;
6+ import 'package:onix_flutter_bricks/domain/entity/data_component/json_class_variable.dart' ;
67import 'package:onix_flutter_bricks/domain/repository/data_component_repository.dart' ;
78import 'package:onix_flutter_bricks/domain/service/base/base_generation_service.dart' ;
8- import 'package:onix_flutter_bricks/domain/service/base/class_builder/freezed_class_builder .dart' ;
9+ import 'package:onix_flutter_bricks/domain/service/base/class_builder/json_class_builder .dart' ;
910import 'package:onix_flutter_bricks/domain/service/base/params/base_generation_params.dart' ;
1011import 'package:onix_flutter_bricks/domain/service/file_generator_service/data_component_generators/params/data_component_params.dart' ;
1112import 'package:onix_flutter_bricks/util/type_matcher.dart' ;
@@ -29,16 +30,25 @@ class RequestGenerator implements BaseGenerationService<bool> {
2930 params.dataComponent,
3031 );
3132
33+ final properties = _getProperties (params.dataComponent);
34+
35+ final variableDeclarations = JsonClassBuilder .variablesFromJsonVariable (
36+ properties,
37+ );
3238 final constructorProperties =
33- _getConstructorProperties (params.dataComponent);
39+ JsonClassBuilder .constructorPropertiesFromJsonVariable (
40+ properties,
41+ );
3442
35- final freezedClass = FreezedClassBuilder (
43+ final jsonClass = JsonClassBuilder (
3644 className: name,
3745 classNameSuffix: 'request' ,
3846 )
47+ ..withToJson = true
3948 ..imports = imports
49+ ..variableDeclarations = variableDeclarations
4050 ..baseConstructorProperties = constructorProperties;
41- final fileContent = freezedClass .build ();
51+ final fileContent = jsonClass .build ();
4252
4353 final path = await Directory (
4454 '${params .projectPath }/${params .projectName }/lib/data/model/remote/${sourceName .isNotEmpty ? '${sourceName .snakeCase }/' : '' }${name .snakeCase }' )
@@ -76,12 +86,17 @@ class RequestGenerator implements BaseGenerationService<bool> {
7686 return imports;
7787 }
7888
79- Iterable <String > _getConstructorProperties (DataComponent dataComponent) {
80- final constructorProperties = dataComponent.properties.map (
89+ Iterable <JsonClassVariable > _getProperties (
90+ DataComponent dataComponent,
91+ ) {
92+ final properties = dataComponent.properties.map (
8193 (e) {
8294 String type = e.type;
83- if (! TypeMatcher .isStandardType (TypeMatcher .getDartType (type)) &&
84- ! type.contains ('dynamic' )) {
95+ final isDynamic = type.contains ('dynamic' );
96+ final isStandardType = TypeMatcher .isStandardType (
97+ TypeMatcher .getDartType (type),
98+ );
99+ if (! isStandardType && ! isDynamic) {
85100 final import = dataComponent.imports.firstWhereOrNull (
86101 (element) => element.pascalCase == type.pascalCase);
87102 final isEnum =
@@ -96,12 +111,16 @@ class RequestGenerator implements BaseGenerationService<bool> {
96111 if (e.isList) {
97112 type = 'List<${TypeMatcher .getDartType (type )}>' ;
98113 }
99- final prefix =
100- e.nullable ? '@JsonKey(includeIfNull: false)' : 'required' ;
114+
101115 final dartType = TypeMatcher .getDartType (type);
102- return '$prefix $dartType ${e .nullable ? '?' : '' } ${e .name },' ;
116+
117+ return JsonClassVariable (
118+ dartType: dartType,
119+ name: e.name,
120+ nullable: e.nullable,
121+ );
103122 },
104123 );
105- return constructorProperties ;
124+ return properties ;
106125 }
107126}
0 commit comments