Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json conversion error #156

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bin/json2model
Binary file not shown.
22 changes: 22 additions & 0 deletions bin/test.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
22 changes: 22 additions & 0 deletions bin/test2.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
22 changes: 22 additions & 0 deletions bin/test3.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
10 changes: 10 additions & 0 deletions lib/commands/impl/args_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 35 additions & 12 deletions lib/commands/impl/generate/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,44 @@ class GenerateModelCommand extends Command {
String get commandName => 'model';
@override
Future<void> 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';

Expand Down Expand Up @@ -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 =
Expand All @@ -78,9 +101,9 @@ class GenerateModelCommand extends Command {
return true;
}

Future<String> get _jsonRawData async {
if (withArgument.isNotEmpty) {
return await File(withArgument).readAsString();
Future<String> _jsonRawData(String file) async {
if (file.isNotEmpty) {
return await File(file).readAsString();
} else {
try {
var result = await get(Uri.parse(fromArgument));
Expand Down
23 changes: 23 additions & 0 deletions lib/common/providers/test2_provider.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic>) return Test2.fromJson(map);
if (map is List) return map.map((item) => Test2.fromJson(item)).toList();
};
httpClient.baseUrl = 'YOUR-API-URL';
}

Future<Test2?> getTest2(int id) async {
final response = await get('test2/$id');
return response.body;
}

Future<Response<Test2>> postTest2(Test2 test2) async =>
await post('test2', test2);
Future<Response> deleteTest2(int id) async => await delete('test2/$id');
}
23 changes: 23 additions & 0 deletions lib/common/providers/test3_provider.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic>) return Test3.fromJson(map);
if (map is List) return map.map((item) => Test3.fromJson(item)).toList();
};
httpClient.baseUrl = 'YOUR-API-URL';
}

Future<Test3?> getTest3(int id) async {
final response = await get('test3/$id');
return response.body;
}

Future<Response<Test3>> postTest3(Test3 test3) async =>
await post('test3', test3);
Future<Response> deleteTest3(int id) async => await delete('test3/$id');
}
22 changes: 22 additions & 0 deletions lib/common/providers/test_provider.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic>) return Test.fromJson(map);
if (map is List) return map.map((item) => Test.fromJson(item)).toList();
};
httpClient.baseUrl = 'YOUR-API-URL';
}

Future<Test?> getTest(int id) async {
final response = await get('test/$id');
return response.body;
}

Future<Response<Test>> postTest(Test test) async => await post('test', test);
Future<Response> deleteTest(int id) async => await delete('test/$id');
}
95 changes: 95 additions & 0 deletions lib/common/test2_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class test2 {
int? code;
String? msg;
List<Data>? data;

test2({this.code, this.msg, this.data});

test2.fromJson(Map<String, dynamic> json) {
code = json['code'];
msg = json['msg'];
if (json['data'] != null) {
data = <Data>[];
json['data'].forEach((v) {
data?.add(Data.fromJson(v));
});
}
}

Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
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<String, dynamic> 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<String, dynamic> toJson() {
final data = <String, dynamic>{};
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;
}
}
Loading