diff --git a/bin/json2model b/bin/json2model new file mode 100755 index 00000000..c228769f Binary files /dev/null and b/bin/json2model differ diff --git a/bin/test.json b/bin/test.json new file mode 100644 index 00000000..860a2d88 --- /dev/null +++ b/bin/test.json @@ -0,0 +1,22 @@ +{ + "code": 0, + "msg": "操作成功", + "data": [ + { + "id": null, + "remark": null, + "createdBy": null, + "createdAt": null, + "updatedBy": null, + "updatedAt": null, + "delFlag": null, + "belongCustomerId": null, + "appCode": null, + "agreementName": "用户协议", + "functionCode": "USER_AGREEMENT", + "richTextId": null, + "richTextContent": "html", + "updateBy": null + } + ] +} \ No newline at end of file diff --git a/bin/test2.json b/bin/test2.json new file mode 100644 index 00000000..860a2d88 --- /dev/null +++ b/bin/test2.json @@ -0,0 +1,22 @@ +{ + "code": 0, + "msg": "操作成功", + "data": [ + { + "id": null, + "remark": null, + "createdBy": null, + "createdAt": null, + "updatedBy": null, + "updatedAt": null, + "delFlag": null, + "belongCustomerId": null, + "appCode": null, + "agreementName": "用户协议", + "functionCode": "USER_AGREEMENT", + "richTextId": null, + "richTextContent": "html", + "updateBy": null + } + ] +} \ No newline at end of file diff --git a/bin/test3.json b/bin/test3.json new file mode 100644 index 00000000..860a2d88 --- /dev/null +++ b/bin/test3.json @@ -0,0 +1,22 @@ +{ + "code": 0, + "msg": "操作成功", + "data": [ + { + "id": null, + "remark": null, + "createdBy": null, + "createdAt": null, + "updatedBy": null, + "updatedAt": null, + "delFlag": null, + "belongCustomerId": null, + "appCode": null, + "agreementName": "用户协议", + "functionCode": "USER_AGREEMENT", + "richTextId": null, + "richTextContent": "html", + "updateBy": null + } + ] +} \ No newline at end of file diff --git a/lib/commands/impl/args_mixin.dart b/lib/commands/impl/args_mixin.dart index 5aa15730..c9778d4c 100644 --- a/lib/commands/impl/args_mixin.dart +++ b/lib/commands/impl/args_mixin.dart @@ -50,6 +50,16 @@ mixin ArgsMixin { return _getArg('with'); } + /// model suffix + String get onSuffix { + return _getArg('suffix'); + } + + /// model recursive Directory + String get onRecursive { + return _getArg('recursive'); + } + /// return parameter `from` /// /// example run diff --git a/lib/commands/impl/generate/model/model.dart b/lib/commands/impl/generate/model/model.dart index 5e46b19d..5a70f94b 100644 --- a/lib/commands/impl/generate/model/model.dart +++ b/lib/commands/impl/generate/model/model.dart @@ -21,22 +21,44 @@ class GenerateModelCommand extends Command { String get commandName => 'model'; @override Future execute() async { - var name = p.basenameWithoutExtension(withArgument).pascalCase; - if (withArgument.isEmpty) { - final dialog = CLI_Dialog(questions: [ - [LocaleKeys.ask_model_name.tr, 'name'] - ]); - var result = dialog.ask()['name'] as String; - name = result.pascalCase; + var recursiveDir = onRecursive; + + var suffix = onSuffix; + + if (recursiveDir.isNotEmpty) { + var current = Directory(recursiveDir); + final list = current.listSync(recursive: false, followLinks: false); + + for (var element in list) { + var ext = p.extension(element.path); + if (ext == ".json") { + var r_name = p.basenameWithoutExtension(element.path).pascalCase; + start(r_name, suffix, await _jsonRawData(element.path)); + } + } + } else { + var name = p.basenameWithoutExtension(withArgument); + + if (withArgument.isEmpty) { + final dialog = CLI_Dialog(questions: [ + [LocaleKeys.ask_model_name.tr, 'name'] + ]); + var result = dialog.ask()['name'] as String; + name = result.pascalCase; + } + + start(name, suffix, await _jsonRawData(withArgument)); } + } + void start(String name, String suffix, String jsonRawData) async { FileModel _fileModel; final classGenerator = ModelGenerator( - name, containsArg('--private'), containsArg('--withCopy')); + name + suffix, containsArg('--private'), containsArg('--withCopy')); _fileModel = Structure.model(name, 'model', false, on: onCommand); - var dartCode = classGenerator.generateDartClasses(await _jsonRawData); + var dartCode = classGenerator.generateDartClasses(jsonRawData); var modelPath = '${_fileModel.path}_model.dart'; @@ -68,6 +90,7 @@ class GenerateModelCommand extends Command { @override bool validate() { + if (onRecursive.isNotEmpty) return true; if ((withArgument.isEmpty || p.extension(withArgument) != '.json') && fromArgument.isEmpty) { var codeSample = @@ -78,9 +101,9 @@ class GenerateModelCommand extends Command { return true; } - Future get _jsonRawData async { - if (withArgument.isNotEmpty) { - return await File(withArgument).readAsString(); + Future _jsonRawData(String file) async { + if (file.isNotEmpty) { + return await File(file).readAsString(); } else { try { var result = await get(Uri.parse(fromArgument)); diff --git a/lib/common/providers/test2_provider.dart b/lib/common/providers/test2_provider.dart new file mode 100644 index 00000000..69976a63 --- /dev/null +++ b/lib/common/providers/test2_provider.dart @@ -0,0 +1,23 @@ +import 'package:get/get.dart'; + +import '../test2_model.dart'; + +class Test2Provider extends GetConnect { + @override + void onInit() { + httpClient.defaultDecoder = (map) { + if (map is Map) return Test2.fromJson(map); + if (map is List) return map.map((item) => Test2.fromJson(item)).toList(); + }; + httpClient.baseUrl = 'YOUR-API-URL'; + } + + Future getTest2(int id) async { + final response = await get('test2/$id'); + return response.body; + } + + Future> postTest2(Test2 test2) async => + await post('test2', test2); + Future deleteTest2(int id) async => await delete('test2/$id'); +} diff --git a/lib/common/providers/test3_provider.dart b/lib/common/providers/test3_provider.dart new file mode 100644 index 00000000..0ebbe7e5 --- /dev/null +++ b/lib/common/providers/test3_provider.dart @@ -0,0 +1,23 @@ +import 'package:get/get.dart'; + +import '../test3_model.dart'; + +class Test3Provider extends GetConnect { + @override + void onInit() { + httpClient.defaultDecoder = (map) { + if (map is Map) return Test3.fromJson(map); + if (map is List) return map.map((item) => Test3.fromJson(item)).toList(); + }; + httpClient.baseUrl = 'YOUR-API-URL'; + } + + Future getTest3(int id) async { + final response = await get('test3/$id'); + return response.body; + } + + Future> postTest3(Test3 test3) async => + await post('test3', test3); + Future deleteTest3(int id) async => await delete('test3/$id'); +} diff --git a/lib/common/providers/test_provider.dart b/lib/common/providers/test_provider.dart new file mode 100644 index 00000000..becc15f8 --- /dev/null +++ b/lib/common/providers/test_provider.dart @@ -0,0 +1,22 @@ +import 'package:get/get.dart'; + +import '../test_model.dart'; + +class TestProvider extends GetConnect { + @override + void onInit() { + httpClient.defaultDecoder = (map) { + if (map is Map) return Test.fromJson(map); + if (map is List) return map.map((item) => Test.fromJson(item)).toList(); + }; + httpClient.baseUrl = 'YOUR-API-URL'; + } + + Future getTest(int id) async { + final response = await get('test/$id'); + return response.body; + } + + Future> postTest(Test test) async => await post('test', test); + Future deleteTest(int id) async => await delete('test/$id'); +} diff --git a/lib/common/test2_model.dart b/lib/common/test2_model.dart new file mode 100644 index 00000000..0574ea9d --- /dev/null +++ b/lib/common/test2_model.dart @@ -0,0 +1,95 @@ +class test2 { + int? code; + String? msg; + List? data; + + test2({this.code, this.msg, this.data}); + + test2.fromJson(Map json) { + code = json['code']; + msg = json['msg']; + if (json['data'] != null) { + data = []; + json['data'].forEach((v) { + data?.add(Data.fromJson(v)); + }); + } + } + + Map toJson() { + final data = {}; + data['code'] = code; + data['msg'] = msg; + data['data'] = this.data?.map((v) => v.toJson()).toList(); + return data; + } +} + +class Data { + dynamic id; + dynamic remark; + dynamic createdBy; + dynamic createdAt; + dynamic updatedBy; + dynamic updatedAt; + dynamic delFlag; + dynamic belongCustomerId; + dynamic appCode; + String? agreementName; + String? functionCode; + dynamic richTextId; + String? richTextContent; + dynamic updateBy; + + Data( + {this.id, + this.remark, + this.createdBy, + this.createdAt, + this.updatedBy, + this.updatedAt, + this.delFlag, + this.belongCustomerId, + this.appCode, + this.agreementName, + this.functionCode, + this.richTextId, + this.richTextContent, + this.updateBy}); + + Data.fromJson(Map json) { + id = json['id']; + remark = json['remark']; + createdBy = json['createdBy']; + createdAt = json['createdAt']; + updatedBy = json['updatedBy']; + updatedAt = json['updatedAt']; + delFlag = json['delFlag']; + belongCustomerId = json['belongCustomerId']; + appCode = json['appCode']; + agreementName = json['agreementName']; + functionCode = json['functionCode']; + richTextId = json['richTextId']; + richTextContent = json['richTextContent']; + updateBy = json['updateBy']; + } + + Map toJson() { + final data = {}; + data['id'] = id; + data['remark'] = remark; + data['createdBy'] = createdBy; + data['createdAt'] = createdAt; + data['updatedBy'] = updatedBy; + data['updatedAt'] = updatedAt; + data['delFlag'] = delFlag; + data['belongCustomerId'] = belongCustomerId; + data['appCode'] = appCode; + data['agreementName'] = agreementName; + data['functionCode'] = functionCode; + data['richTextId'] = richTextId; + data['richTextContent'] = richTextContent; + data['updateBy'] = updateBy; + return data; + } +} diff --git a/lib/common/test3_model.dart b/lib/common/test3_model.dart new file mode 100644 index 00000000..f845e89d --- /dev/null +++ b/lib/common/test3_model.dart @@ -0,0 +1,95 @@ +class test3 { + int? code; + String? msg; + List? data; + + test3({this.code, this.msg, this.data}); + + test3.fromJson(Map json) { + code = json['code']; + msg = json['msg']; + if (json['data'] != null) { + data = []; + json['data'].forEach((v) { + data?.add(Data.fromJson(v)); + }); + } + } + + Map toJson() { + final data = {}; + data['code'] = code; + data['msg'] = msg; + data['data'] = this.data?.map((v) => v.toJson()).toList(); + return data; + } +} + +class Data { + dynamic id; + dynamic remark; + dynamic createdBy; + dynamic createdAt; + dynamic updatedBy; + dynamic updatedAt; + dynamic delFlag; + dynamic belongCustomerId; + dynamic appCode; + String? agreementName; + String? functionCode; + dynamic richTextId; + String? richTextContent; + dynamic updateBy; + + Data( + {this.id, + this.remark, + this.createdBy, + this.createdAt, + this.updatedBy, + this.updatedAt, + this.delFlag, + this.belongCustomerId, + this.appCode, + this.agreementName, + this.functionCode, + this.richTextId, + this.richTextContent, + this.updateBy}); + + Data.fromJson(Map json) { + id = json['id']; + remark = json['remark']; + createdBy = json['createdBy']; + createdAt = json['createdAt']; + updatedBy = json['updatedBy']; + updatedAt = json['updatedAt']; + delFlag = json['delFlag']; + belongCustomerId = json['belongCustomerId']; + appCode = json['appCode']; + agreementName = json['agreementName']; + functionCode = json['functionCode']; + richTextId = json['richTextId']; + richTextContent = json['richTextContent']; + updateBy = json['updateBy']; + } + + Map toJson() { + final data = {}; + data['id'] = id; + data['remark'] = remark; + data['createdBy'] = createdBy; + data['createdAt'] = createdAt; + data['updatedBy'] = updatedBy; + data['updatedAt'] = updatedAt; + data['delFlag'] = delFlag; + data['belongCustomerId'] = belongCustomerId; + data['appCode'] = appCode; + data['agreementName'] = agreementName; + data['functionCode'] = functionCode; + data['richTextId'] = richTextId; + data['richTextContent'] = richTextContent; + data['updateBy'] = updateBy; + return data; + } +} diff --git a/lib/common/test_model.dart b/lib/common/test_model.dart new file mode 100644 index 00000000..1088c6fa --- /dev/null +++ b/lib/common/test_model.dart @@ -0,0 +1,95 @@ +class test { + int? code; + String? msg; + List? data; + + test({this.code, this.msg, this.data}); + + test.fromJson(Map json) { + code = json['code']; + msg = json['msg']; + if (json['data'] != null) { + data = []; + json['data'].forEach((v) { + data?.add(Data.fromJson(v)); + }); + } + } + + Map toJson() { + final data = {}; + data['code'] = code; + data['msg'] = msg; + data['data'] = this.data?.map((v) => v.toJson()).toList(); + return data; + } +} + +class Data { + dynamic id; + dynamic remark; + dynamic createdBy; + dynamic createdAt; + dynamic updatedBy; + dynamic updatedAt; + dynamic delFlag; + dynamic belongCustomerId; + dynamic appCode; + String? agreementName; + String? functionCode; + dynamic richTextId; + String? richTextContent; + dynamic updateBy; + + Data( + {this.id, + this.remark, + this.createdBy, + this.createdAt, + this.updatedBy, + this.updatedAt, + this.delFlag, + this.belongCustomerId, + this.appCode, + this.agreementName, + this.functionCode, + this.richTextId, + this.richTextContent, + this.updateBy}); + + Data.fromJson(Map json) { + id = json['id']; + remark = json['remark']; + createdBy = json['createdBy']; + createdAt = json['createdAt']; + updatedBy = json['updatedBy']; + updatedAt = json['updatedAt']; + delFlag = json['delFlag']; + belongCustomerId = json['belongCustomerId']; + appCode = json['appCode']; + agreementName = json['agreementName']; + functionCode = json['functionCode']; + richTextId = json['richTextId']; + richTextContent = json['richTextContent']; + updateBy = json['updateBy']; + } + + Map toJson() { + final data = {}; + data['id'] = id; + data['remark'] = remark; + data['createdBy'] = createdBy; + data['createdAt'] = createdAt; + data['updatedBy'] = updatedBy; + data['updatedAt'] = updatedAt; + data['delFlag'] = delFlag; + data['belongCustomerId'] = belongCustomerId; + data['appCode'] = appCode; + data['agreementName'] = agreementName; + data['functionCode'] = functionCode; + data['richTextId'] = richTextId; + data['richTextContent'] = richTextContent; + data['updateBy'] = updateBy; + return data; + } +} diff --git a/lib/common/utils/json_serialize/sintaxe.dart b/lib/common/utils/json_serialize/sintaxe.dart index 0876ca3f..47b102db 100644 --- a/lib/common/utils/json_serialize/sintaxe.dart +++ b/lib/common/utils/json_serialize/sintaxe.dart @@ -152,9 +152,7 @@ class TypeDefinition { return "data['$key'] = $thisKey;"; } else if (name == 'List') { // class list - return """if ($thisKey != null) { - data['$key'] = $thisKey${PubspecUtils.nullSafeSupport ? '?' : ''}.map((v) => ${_buildToJsonClass('v', nullSafe: false)}).toList(); - }"""; + return "data['$key'] = this.$thisKey${PubspecUtils.nullSafeSupport ? '?' : ''}.map((v) => ${_buildToJsonClass('v', nullSafe: false)}).toList();"; } else { // class return """if ($thisKey != null) { diff --git a/test.json b/test.json new file mode 100644 index 00000000..860a2d88 --- /dev/null +++ b/test.json @@ -0,0 +1,22 @@ +{ + "code": 0, + "msg": "操作成功", + "data": [ + { + "id": null, + "remark": null, + "createdBy": null, + "createdAt": null, + "updatedBy": null, + "updatedAt": null, + "delFlag": null, + "belongCustomerId": null, + "appCode": null, + "agreementName": "用户协议", + "functionCode": "USER_AGREEMENT", + "richTextId": null, + "richTextContent": "html", + "updateBy": null + } + ] +} \ No newline at end of file