-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
270 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
swagger_parser/lib/src/generator/templates/dart_dart_mappable_dto_template.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'package:collection/collection.dart'; | ||
|
||
import '../../../swagger_parser.dart'; | ||
import '../../utils/case_utils.dart'; | ||
import '../../utils/type_utils.dart'; | ||
import '../../utils/utils.dart'; | ||
import '../models/json_serializer.dart'; | ||
import '../models/universal_type.dart'; | ||
import 'dart_import_dto_template.dart'; | ||
|
||
String dartDartMappableDtoTemplate( | ||
UniversalComponentClass dataClass, { | ||
required bool markFileAsGenerated, | ||
}) { | ||
final className = dataClass.name.toPascal; | ||
return ''' | ||
${generatedFileComment(markFileAsGenerated: markFileAsGenerated)} | ||
${dartImportDtoTemplate(JsonSerializer.dart_mappable)} | ||
${dartImports(imports: dataClass.imports)} | ||
part '${dataClass.name.toSnake}.mapper.dart'; | ||
${descriptionComment(dataClass.description)}@MappableClass() | ||
class $className with ${className}Mappable { | ||
${indentation(2)}const $className(${getParameters(dataClass)}); | ||
${getFields(dataClass)} | ||
} | ||
'''; | ||
} | ||
|
||
String getParameters(UniversalComponentClass dataClass) { | ||
if (dataClass.parameters.isNotEmpty) { | ||
return '{\n${_parametersToString(dataClass.parameters)}\n${indentation(2)}}'; | ||
} else { | ||
return ''; | ||
} | ||
} | ||
|
||
String getFields(UniversalComponentClass dataClass) { | ||
if (dataClass.parameters.isNotEmpty) { | ||
return '${_fieldsToString(dataClass.parameters)}\n'; | ||
} else { | ||
return ''; | ||
} | ||
} | ||
|
||
String _fieldsToString(List<UniversalType> parameters) { | ||
final sortedByRequired = | ||
List<UniversalType>.from(parameters.sorted((a, b) => a.compareTo(b))); | ||
return sortedByRequired | ||
.mapIndexed( | ||
(i, e) => | ||
'${indentation(2)}final ${e.toSuitableType(ProgrammingLanguage.dart)} ${e.name};', | ||
) | ||
.join('\n'); | ||
} | ||
|
||
String _parametersToString(List<UniversalType> parameters) { | ||
final sortedByRequired = | ||
List<UniversalType>.from(parameters.sorted((a, b) => a.compareTo(b))); | ||
return sortedByRequired | ||
.mapIndexed( | ||
(i, e) => | ||
'${indentation(4)}${_required(e)}this.${e.name}${getDefaultValue(e)},', | ||
) | ||
.join('\n'); | ||
} | ||
|
||
String getDefaultValue(UniversalType t) { | ||
if (t.defaultValue == null) { | ||
return ''; | ||
} | ||
return ' = ${_defaultValue(t)}'; | ||
} | ||
|
||
/// return required if isRequired | ||
String _required(UniversalType t) => | ||
t.isRequired && t.defaultValue == null ? 'required ' : ''; | ||
|
||
/// return defaultValue if have | ||
String _defaultValue(UniversalType t) => | ||
'${t.enumType != null ? '${t.type}.${protectDefaultEnum(t.defaultValue)?.toCamel}' : protectDefaultValue(t.defaultValue, type: t.type)}'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
swagger_parser/lib/src/generator/templates/dart_import_dto_template.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import '../models/json_serializer.dart'; | ||
|
||
String dartImportDtoTemplate(JsonSerializer jsonSerializer) { | ||
switch (jsonSerializer) { | ||
case JsonSerializer.freezed: | ||
return "import 'package:freezed_annotation/freezed_annotation.dart';"; | ||
case JsonSerializer.json_serializable: | ||
return "import 'package:json_annotation/json_annotation.dart';"; | ||
case JsonSerializer.dart_mappable: | ||
return "import 'package:dart_mappable/dart_mappable.dart';"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters