diff --git a/Test/EmptyResp.dart b/Test/EmptyResp.dart index b35f5ff..46c038a 100644 --- a/Test/EmptyResp.dart +++ b/Test/EmptyResp.dart @@ -1,15 +1,16 @@ import 'dart:convert' show json; class EmptyResp { - Qwe qwe; + Qwe? qwe; EmptyResp.fromParams({this.qwe}); - factory EmptyResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? EmptyResp.fromJson(json.decode(jsonStr)) - : EmptyResp.fromJson(jsonStr); + factory EmptyResp(Object jsonStr) => jsonStr is String + ? EmptyResp.fromJson(json.decode(jsonStr)) + : EmptyResp.fromJson(jsonStr); + + static EmptyResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : EmptyResp(jsonStr); EmptyResp.fromJson(jsonRes) { qwe = jsonRes['qwe'] == null ? null : Qwe.fromJson(jsonRes['qwe']); @@ -19,12 +20,14 @@ class EmptyResp { String toString() { return '{"qwe": $qwe}'; } + + String toJSON() => this.toString(); } class Qwe { - List asd; - List qaz; - List zxc; + List? asd; + List? qaz; + List? zxc; Qwe.fromParams({this.asd, this.qaz, this.zxc}); @@ -32,19 +35,19 @@ class Qwe { asd = jsonRes['asd'] == null ? null : []; for (var asdItem in asd == null ? [] : jsonRes['asd']) { - asd.add(asdItem); + asd!.add(asdItem); } qaz = jsonRes['qaz'] == null ? null : []; for (var qazItem in qaz == null ? [] : jsonRes['qaz']) { - qaz.add(qazItem); + qaz!.add(qazItem); } zxc = jsonRes['zxc'] == null ? null : []; for (var zxcItem in zxc == null ? [] : jsonRes['zxc']) { - zxc.add(zxcItem); + zxc!.add(zxcItem); } } @@ -52,4 +55,6 @@ class Qwe { String toString() { return '{"asd": $asd, "qaz": $qaz, "zxc": $zxc}'; } + + String toJSON() => this.toString(); } diff --git a/Test/IgnoreMapResp.dart b/Test/IgnoreMapResp.dart index 34f1c46..b2c8975 100644 --- a/Test/IgnoreMapResp.dart +++ b/Test/IgnoreMapResp.dart @@ -1,15 +1,16 @@ import 'dart:convert' show json; class IgnoreMapResp { - Data data; + Data? data; IgnoreMapResp.fromParams({this.data}); - factory IgnoreMapResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? IgnoreMapResp.fromJson(json.decode(jsonStr)) - : IgnoreMapResp.fromJson(jsonStr); + factory IgnoreMapResp(Object jsonStr) => jsonStr is String + ? IgnoreMapResp.fromJson(json.decode(jsonStr)) + : IgnoreMapResp.fromJson(jsonStr); + + static IgnoreMapResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : IgnoreMapResp(jsonStr); IgnoreMapResp.fromJson(jsonRes) { data = jsonRes['data'] == null ? null : Data.fromJson(jsonRes['data']); @@ -19,16 +20,18 @@ class IgnoreMapResp { String toString() { return '{"data": $data}'; } + + String toJSON() => this.toString(); } class Data { - int wc; - String author; - String content; - String digest; - String title; - Map date; - Extra extra; + int? wc; + String? author; + String? content; + String? digest; + String? title; + Map? date; + Extra? extra; Data.fromParams( {this.wc, @@ -53,12 +56,14 @@ class Data { String toString() { return '{"wc": $wc, "author": ${author != null ? '${json.encode(author)}' : 'null'}, "content": ${content != null ? '${json.encode(content)}' : 'null'}, "digest": ${digest != null ? '${json.encode(digest)}' : 'null'}, "title": ${title != null ? '${json.encode(title)}' : 'null'}, "date": ${date != null ? '${json.encode(date)}' : 'null'}, "extra": $extra}'; } + + String toJSON() => this.toString(); } class Extra { - int a; - int b; - int c; + int? a; + int? b; + int? c; Extra.fromParams({this.a, this.b, this.c}); @@ -72,4 +77,6 @@ class Extra { String toString() { return '{"a": $a, "b": $b, "c": $c}'; } + + String toJSON() => this.toString(); } diff --git a/Test/ListTopResp.dart b/Test/ListTopResp.dart index 3c65f94..f35ca02 100644 --- a/Test/ListTopResp.dart +++ b/Test/ListTopResp.dart @@ -1,21 +1,22 @@ import 'dart:convert' show json; class ListTopResp { - List list; + List? list; ListTopResp.fromParams({this.list}); - factory ListTopResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? ListTopResp.fromJson(json.decode(jsonStr)) - : ListTopResp.fromJson(jsonStr); + factory ListTopResp(Object jsonStr) => jsonStr is String + ? ListTopResp.fromJson(json.decode(jsonStr)) + : ListTopResp.fromJson(jsonStr); + + static ListTopResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : ListTopResp(jsonStr); ListTopResp.fromJson(jsonRes) { list = jsonRes == null ? null : []; for (var listItem in list == null ? [] : jsonRes) { - list.add(listItem == null ? null : Resp.fromJson(listItem)); + list!.add(listItem == null ? null : Resp.fromJson(listItem)); } } @@ -23,11 +24,13 @@ class ListTopResp { String toString() { return '{"json_list": $list}'; } + + String toJSON() => this.toString(); } class Resp { - String a; - String b; + String? a; + String? b; Resp.fromParams({this.a, this.b}); @@ -40,4 +43,6 @@ class Resp { String toString() { return '{"a": ${a != null ? '${json.encode(a)}' : 'null'}, "b": ${b != null ? '${json.encode(b)}' : 'null'}}'; } + + String toJSON() => this.toString(); } diff --git a/Test/ListWithStringResp.dart b/Test/ListWithStringResp.dart index e5e5b44..94f08bf 100644 --- a/Test/ListWithStringResp.dart +++ b/Test/ListWithStringResp.dart @@ -1,39 +1,40 @@ import 'dart:convert' show json; class ListWithStringResp { - List asd; - List> qaz; - List zxc; + List? asd; + List?>? qaz; + List? zxc; ListWithStringResp.fromParams({this.asd, this.qaz, this.zxc}); - factory ListWithStringResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? ListWithStringResp.fromJson(json.decode(jsonStr)) - : ListWithStringResp.fromJson(jsonStr); + factory ListWithStringResp(Object jsonStr) => jsonStr is String + ? ListWithStringResp.fromJson(json.decode(jsonStr)) + : ListWithStringResp.fromJson(jsonStr); + + static ListWithStringResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : ListWithStringResp(jsonStr); ListWithStringResp.fromJson(jsonRes) { asd = jsonRes['asd'] == null ? null : []; for (var asdItem in asd == null ? [] : jsonRes['asd']) { - asd.add(asdItem); + asd!.add(asdItem); } qaz = jsonRes['qaz'] == null ? null : []; for (var qazItem in qaz == null ? [] : jsonRes['qaz']) { - List qazChild = qazItem == null ? null : []; + List? qazChild = qazItem == null ? null : []; for (var qazItemItem in qazChild == null ? [] : qazItem) { - qazChild.add(qazItemItem); + qazChild!.add(qazItemItem); } - qaz.add(qazChild); + qaz!.add(qazChild); } zxc = jsonRes['zxc'] == null ? null : []; for (var zxcItem in zxc == null ? [] : jsonRes['zxc']) { - zxc.add(zxcItem == null ? null : Qwe.fromJson(zxcItem)); + zxc!.add(zxcItem == null ? null : Qwe.fromJson(zxcItem)); } } @@ -41,10 +42,12 @@ class ListWithStringResp { String toString() { return '{"asd": ${asd != null ? '${json.encode(asd)}' : 'null'}, "qaz": ${qaz != null ? '${json.encode(qaz)}' : 'null'}, "zxc": $zxc}'; } + + String toJSON() => this.toString(); } class Qwe { - String qwe; + String? qwe; Qwe.fromParams({this.qwe}); @@ -56,4 +59,6 @@ class Qwe { String toString() { return '{"qwe": ${qwe != null ? '${json.encode(qwe)}' : 'null'}}'; } + + String toJSON() => this.toString(); } diff --git a/Test/ListsResp.dart b/Test/ListsResp.dart index ebcbe38..2f33a6f 100644 --- a/Test/ListsResp.dart +++ b/Test/ListsResp.dart @@ -1,52 +1,53 @@ import 'dart:convert' show json; class ListsResp { - List>> asd; - List qaz; - List>> qwe; + List?>?>? asd; + List? qaz; + List?>?>? qwe; ListsResp.fromParams({this.asd, this.qaz, this.qwe}); - factory ListsResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? ListsResp.fromJson(json.decode(jsonStr)) - : ListsResp.fromJson(jsonStr); + factory ListsResp(Object jsonStr) => jsonStr is String + ? ListsResp.fromJson(json.decode(jsonStr)) + : ListsResp.fromJson(jsonStr); + + static ListsResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : ListsResp(jsonStr); ListsResp.fromJson(jsonRes) { asd = jsonRes['asd'] == null ? null : []; for (var asdItem in asd == null ? [] : jsonRes['asd']) { - List> asdChild = asdItem == null ? null : []; + List?>? asdChild = asdItem == null ? null : []; for (var asdItemItem in asdChild == null ? [] : asdItem) { - List asdChildChild = asdItemItem == null ? null : []; + List? asdChildChild = asdItemItem == null ? null : []; for (var asdItemItemItem in asdChildChild == null ? [] : asdItemItem) { - asdChildChild.add(asdItemItemItem); + asdChildChild!.add(asdItemItemItem); } - asdChild.add(asdChildChild); + asdChild!.add(asdChildChild); } - asd.add(asdChild); + asd!.add(asdChild); } qaz = jsonRes['qaz'] == null ? null : []; for (var qazItem in qaz == null ? [] : jsonRes['qaz']) { - qaz.add(qazItem); + qaz!.add(qazItem); } qwe = jsonRes['qwe'] == null ? null : []; for (var qweItem in qwe == null ? [] : jsonRes['qwe']) { - List> qweChild = qweItem == null ? null : []; + List?>? qweChild = qweItem == null ? null : []; for (var qweItemItem in qweChild == null ? [] : qweItem) { - List qweChildChild = qweItemItem == null ? null : []; + List? qweChildChild = qweItemItem == null ? null : []; for (var qweItemItemItem in qweChildChild == null ? [] : qweItemItem) { - qweChildChild.add( + qweChildChild!.add( qweItemItemItem == null ? null : Zxc.fromJson(qweItemItemItem)); } - qweChild.add(qweChildChild); + qweChild!.add(qweChildChild); } - qwe.add(qweChild); + qwe!.add(qweChild); } } @@ -54,10 +55,12 @@ class ListsResp { String toString() { return '{"asd": $asd, "qaz": $qaz, "qwe": $qwe}'; } + + String toJSON() => this.toString(); } class Zxc { - int zxc; + int? zxc; Zxc.fromParams({this.zxc}); @@ -69,4 +72,6 @@ class Zxc { String toString() { return '{"zxc": $zxc}'; } + + String toJSON() => this.toString(); } diff --git a/Test/RegionResp.dart b/Test/RegionResp.dart index a91b89d..278ce27 100644 --- a/Test/RegionResp.dart +++ b/Test/RegionResp.dart @@ -1,18 +1,19 @@ import 'dart:convert' show json; class RegionResp { - int code; - int ttl; - String message; - Data data; + int? code; + int? ttl; + String? message; + Data? data; RegionResp.fromParams({this.code, this.ttl, this.message, this.data}); - factory RegionResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? RegionResp.fromJson(json.decode(jsonStr)) - : RegionResp.fromJson(jsonStr); + factory RegionResp(Object jsonStr) => jsonStr is String + ? RegionResp.fromJson(json.decode(jsonStr)) + : RegionResp.fromJson(jsonStr); + + static RegionResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : RegionResp(jsonStr); RegionResp.fromJson(jsonRes) { code = jsonRes['code']; @@ -25,11 +26,13 @@ class RegionResp { String toString() { return '{"code": $code, "ttl": $ttl, "message": ${message != null ? '${json.encode(message)}' : 'null'}, "data": $data}'; } + + String toJSON() => this.toString(); } class Data { - List archives; - Page page; + List? archives; + Page? page; Data.fromParams({this.archives, this.page}); @@ -37,7 +40,7 @@ class Data { archives = jsonRes['archives'] == null ? null : []; for (var archivesItem in archives == null ? [] : jsonRes['archives']) { - archives.add(archivesItem == null ? null : Arch.fromJson(archivesItem)); + archives!.add(archivesItem == null ? null : Arch.fromJson(archivesItem)); } page = jsonRes['page'] == null ? null : Page.fromJson(jsonRes['page']); @@ -47,12 +50,14 @@ class Data { String toString() { return '{"archives": $archives, "page": $page}'; } + + String toJSON() => this.toString(); } class Page { - int count; - int num; - int size; + int? count; + int? num; + int? size; Page.fromParams({this.count, this.num, this.size}); @@ -66,26 +71,28 @@ class Page { String toString() { return '{"count": $count, "num": $num, "size": $size}'; } + + String toJSON() => this.toString(); } class Arch { - int aid; - int attribute; - int copyright; - int ctime; - int duration; - int pubdate; - int state; - int tid; - int videos; - String desc; - String dynamic; - String pic; - String title; - String tname; - Owner owner; - Right rights; - Stat stat; + int? aid; + int? attribute; + int? copyright; + int? ctime; + int? duration; + int? pubdate; + int? state; + int? tid; + int? videos; + String? desc; + String? dynamic; + String? pic; + String? title; + String? tname; + Owner? owner; + Right? rights; + Stat? stat; Arch.fromParams( {this.aid, @@ -131,19 +138,21 @@ class Arch { String toString() { return '{"aid": $aid, "attribute": $attribute, "copyright": $copyright, "ctime": $ctime, "duration": $duration, "pubdate": $pubdate, "state": $state, "tid": $tid, "videos": $videos, "desc": ${desc != null ? '${json.encode(desc)}' : 'null'}, "dynamic": ${dynamic != null ? '${json.encode(dynamic)}' : 'null'}, "pic": ${pic != null ? '${json.encode(pic)}' : 'null'}, "title": ${title != null ? '${json.encode(title)}' : 'null'}, "tname": ${tname != null ? '${json.encode(tname)}' : 'null'}, "owner": $owner, "rights": $rights, "stat": $stat}'; } + + String toJSON() => this.toString(); } class Stat { - int aid; - int coin; - int danmaku; - int favorite; - int his_rank; - int like; - int now_rank; - int reply; - int share; - int view; + int? aid; + int? coin; + int? danmaku; + int? favorite; + int? his_rank; + int? like; + int? now_rank; + int? reply; + int? share; + int? view; Stat.fromParams( {this.aid, @@ -174,16 +183,18 @@ class Stat { String toString() { return '{"aid": $aid, "coin": $coin, "danmaku": $danmaku, "favorite": $favorite, "his_rank": $his_rank, "like": $like, "now_rank": $now_rank, "reply": $reply, "share": $share, "view": $view}'; } + + String toJSON() => this.toString(); } class Right { - int bp; - int download; - int elec; - int hd5; - int movie; - int no_reprint; - int pay; + int? bp; + int? download; + int? elec; + int? hd5; + int? movie; + int? no_reprint; + int? pay; Right.fromParams( {this.bp, @@ -208,12 +219,14 @@ class Right { String toString() { return '{"bp": $bp, "download": $download, "elec": $elec, "hd5": $hd5, "movie": $movie, "no_reprint": $no_reprint, "pay": $pay}'; } + + String toJSON() => this.toString(); } class Owner { - int mid; - String face; - String name; + int? mid; + String? face; + String? name; Owner.fromParams({this.mid, this.face, this.name}); @@ -227,4 +240,6 @@ class Owner { String toString() { return '{"mid": $mid, "face": ${face != null ? '${json.encode(face)}' : 'null'}, "name": ${name != null ? '${json.encode(name)}' : 'null'}}'; } + + String toJSON() => this.toString(); } diff --git a/Test/WanResp.dart b/Test/WanResp.dart index 0fe81f9..86b29fd 100644 --- a/Test/WanResp.dart +++ b/Test/WanResp.dart @@ -1,17 +1,18 @@ import 'dart:convert' show json; class WanResp { - int errorCode; - String errorMsg; - List data; + int? errorCode; + String? errorMsg; + List? data; WanResp.fromParams({this.errorCode, this.errorMsg, this.data}); - factory WanResp(jsonStr) => jsonStr == null - ? null - : jsonStr is String - ? WanResp.fromJson(json.decode(jsonStr)) - : WanResp.fromJson(jsonStr); + factory WanResp(Object jsonStr) => jsonStr is String + ? WanResp.fromJson(json.decode(jsonStr)) + : WanResp.fromJson(jsonStr); + + static WanResp? parse(jsonStr) => + ['null', '', null].contains(jsonStr) ? null : WanResp(jsonStr); WanResp.fromJson(jsonRes) { errorCode = jsonRes['errorCode']; @@ -19,7 +20,7 @@ class WanResp { data = jsonRes['data'] == null ? null : []; for (var dataItem in data == null ? [] : jsonRes['data']) { - data.add(dataItem == null ? null : Data.fromJson(dataItem)); + data!.add(dataItem == null ? null : Data.fromJson(dataItem)); } } @@ -27,16 +28,18 @@ class WanResp { String toString() { return '{"errorCode": $errorCode, "errorMsg": ${errorMsg != null ? '${json.encode(errorMsg)}' : 'null'}, "data": $data}'; } + + String toJSON() => this.toString(); } class Data { - int courseId; - int id; - int order; - int parentChapterId; - int visible; - String name; - List children; + int? courseId; + int? id; + int? order; + int? parentChapterId; + int? visible; + String? name; + List? children; Data.fromParams( {this.courseId, @@ -57,7 +60,7 @@ class Data { children = jsonRes['children'] == null ? null : []; for (var childrenItem in children == null ? [] : jsonRes['children']) { - children + children! .add(childrenItem == null ? null : Children.fromJson(childrenItem)); } } @@ -66,16 +69,18 @@ class Data { String toString() { return '{"courseId": $courseId, "id": $id, "order": $order, "parentChapterId": $parentChapterId, "visible": $visible, "name": ${name != null ? '${json.encode(name)}' : 'null'}, "children": $children}'; } + + String toJSON() => this.toString(); } class Children { - int courseId; - int id; - int order; - int parentChapterId; - int visible; - String name; - List children; + int? courseId; + int? id; + int? order; + int? parentChapterId; + int? visible; + String? name; + List? children; Children.fromParams( {this.courseId, @@ -96,7 +101,7 @@ class Children { children = jsonRes['children'] == null ? null : []; for (var childrenItem in children == null ? [] : jsonRes['children']) { - children.add(childrenItem); + children!.add(childrenItem); } } @@ -104,4 +109,6 @@ class Children { String toString() { return '{"courseId": $courseId, "id": $id, "order": $order, "parentChapterId": $parentChapterId, "visible": $visible, "name": ${name != null ? '${json.encode(name)}' : 'null'}, "children": $children}'; } + + String toJSON() => this.toString(); } diff --git a/Test/pubspec.yaml b/Test/pubspec.yaml index 05066e6..a953fde 100644 --- a/Test/pubspec.yaml +++ b/Test/pubspec.yaml @@ -1,5 +1,8 @@ name: Test description: Test -dependencies: - test: "^1.2.0" +environment: + sdk: '>=2.12.0 <3.0.0' + +dev_dependencies: + test: ^1.16.5 diff --git a/Test/test.dart b/Test/test.dart index 155a70e..e2b858e 100644 --- a/Test/test.dart +++ b/Test/test.dart @@ -11,23 +11,22 @@ import 'ListTopResp.dart'; import 'ListWithStringResp.dart'; void main() { - - test('Test null', (){ + test('Test null', () { String str0 = '{"asd":[[[1]]],"qwe":[[[{"zxc":1}]]],"qaz":[1]}'; - String str1 = '{"asd":[[[null,null]]],"qwe":[[[null]]],"qaz":[null,null,null]}'; + String str1 = + '{"asd":[[[null,null]]],"qwe":[[[null]]],"qaz":[null,null,null]}'; String str2 = '{"asd":[[[]]],"qwe":[[[{"zxc":null}]]],"qaz":[null]}'; String str3 = '{"asd":[[]],"qwe":[[[]]],"qaz":null}'; String str4 = '{"asd":[],"qwe":[[]],"qaz":null}'; String str5 = '{"asd":null,"qwe":[],"qaz":null}'; - String str6; + String? str6; expect(json.decode(new ListsResp(str0).toString()), json.decode(str0)); expect(json.decode(new ListsResp(str1).toString()), json.decode(str1)); expect(json.decode(new ListsResp(str2).toString()), json.decode(str2)); expect(json.decode(new ListsResp(str3).toString()), json.decode(str3)); expect(json.decode(new ListsResp(str4).toString()), json.decode(str4)); expect(json.decode(new ListsResp(str5).toString()), json.decode(str5)); - expect(json.decode(new ListsResp(str6).toString()), null); - + expect(ListsResp.parse(str6), null); }); test('Test Region', () { String str = readFromFile('Region'); @@ -41,80 +40,80 @@ void main() { expect(resp.code, jsonRes['code']); expect(resp.message, jsonRes['message']); expect(resp.ttl, jsonRes['ttl']); - expect(resp.data.page.num, jsonRes['data']['page']['num']); - expect(resp.data.page.size, jsonRes['data']['page']['size']); - expect(resp.data.page.count, jsonRes['data']['page']['count']); + expect(resp.data!.page!.num, jsonRes['data']['page']['num']); + expect(resp.data!.page!.size, jsonRes['data']['page']['size']); + expect(resp.data!.page!.count, jsonRes['data']['page']['count']); for (var index = 0; index < jsonRes['data']['archives'].length; index++) { - expect(resp.data.archives[index].aid, + expect(resp.data!.archives![index]!.aid, jsonRes['data']['archives'][index]['aid']); - expect(resp.data.archives[index].attribute, + expect(resp.data!.archives![index]!.attribute, jsonRes['data']['archives'][index]['attribute']); - expect(resp.data.archives[index].copyright, + expect(resp.data!.archives![index]!.copyright, jsonRes['data']['archives'][index]['copyright']); - expect(resp.data.archives[index].ctime, + expect(resp.data!.archives![index]!.ctime, jsonRes['data']['archives'][index]['ctime']); - expect(resp.data.archives[index].desc, + expect(resp.data!.archives![index]!.desc, jsonRes['data']['archives'][index]['desc']); - expect(resp.data.archives[index].duration, + expect(resp.data!.archives![index]!.duration, jsonRes['data']['archives'][index]['duration']); - expect(resp.data.archives[index].dynamic, + expect(resp.data!.archives![index]!.dynamic, jsonRes['data']['archives'][index]['dynamic']); - expect(resp.data.archives[index].pic, + expect(resp.data!.archives![index]!.pic, jsonRes['data']['archives'][index]['pic']); - expect(resp.data.archives[index].pubdate, + expect(resp.data!.archives![index]!.pubdate, jsonRes['data']['archives'][index]['pubdate']); - expect(resp.data.archives[index].state, + expect(resp.data!.archives![index]!.state, jsonRes['data']['archives'][index]['state']); - expect(resp.data.archives[index].tid, + expect(resp.data!.archives![index]!.tid, jsonRes['data']['archives'][index]['tid']); - expect(resp.data.archives[index].title, + expect(resp.data!.archives![index]!.title, jsonRes['data']['archives'][index]['title']); - expect(resp.data.archives[index].tname, + expect(resp.data!.archives![index]!.tname, jsonRes['data']['archives'][index]['tname']); - expect(resp.data.archives[index].videos, + expect(resp.data!.archives![index]!.videos, jsonRes['data']['archives'][index]['videos']); - expect(resp.data.archives[index].rights.bp, + expect(resp.data!.archives![index]!.rights!.bp, jsonRes['data']['archives'][index]['rights']['bp']); - expect(resp.data.archives[index].rights.download, + expect(resp.data!.archives![index]!.rights!.download, jsonRes['data']['archives'][index]['rights']['download']); - expect(resp.data.archives[index].rights.elec, + expect(resp.data!.archives![index]!.rights!.elec, jsonRes['data']['archives'][index]['rights']['elec']); - expect(resp.data.archives[index].rights.hd5, + expect(resp.data!.archives![index]!.rights!.hd5, jsonRes['data']['archives'][index]['rights']['hd5']); - expect(resp.data.archives[index].rights.movie, + expect(resp.data!.archives![index]!.rights!.movie, jsonRes['data']['archives'][index]['rights']['movie']); - expect(resp.data.archives[index].rights.no_reprint, + expect(resp.data!.archives![index]!.rights!.no_reprint, jsonRes['data']['archives'][index]['rights']['no_reprint']); - expect(resp.data.archives[index].rights.pay, + expect(resp.data!.archives![index]!.rights!.pay, jsonRes['data']['archives'][index]['rights']['pay']); - expect(resp.data.archives[index].owner.face, + expect(resp.data!.archives![index]!.owner!.face, jsonRes['data']['archives'][index]['owner']['face']); - expect(resp.data.archives[index].owner.mid, + expect(resp.data!.archives![index]!.owner!.mid, jsonRes['data']['archives'][index]['owner']['mid']); - expect(resp.data.archives[index].owner.name, + expect(resp.data!.archives![index]!.owner!.name, jsonRes['data']['archives'][index]['owner']['name']); - expect(resp.data.archives[index].stat.aid, + expect(resp.data!.archives![index]!.stat!.aid, jsonRes['data']['archives'][index]['stat']['aid']); - expect(resp.data.archives[index].stat.coin, + expect(resp.data!.archives![index]!.stat!.coin, jsonRes['data']['archives'][index]['stat']['coin']); - expect(resp.data.archives[index].stat.danmaku, + expect(resp.data!.archives![index]!.stat!.danmaku, jsonRes['data']['archives'][index]['stat']['danmaku']); - expect(resp.data.archives[index].stat.favorite, + expect(resp.data!.archives![index]!.stat!.favorite, jsonRes['data']['archives'][index]['stat']['favorite']); - expect(resp.data.archives[index].stat.his_rank, + expect(resp.data!.archives![index]!.stat!.his_rank, jsonRes['data']['archives'][index]['stat']['his_rank']); - expect(resp.data.archives[index].stat.like, + expect(resp.data!.archives![index]!.stat!.like, jsonRes['data']['archives'][index]['stat']['like']); - expect(resp.data.archives[index].stat.now_rank, + expect(resp.data!.archives![index]!.stat!.now_rank, jsonRes['data']['archives'][index]['stat']['now_rank']); - expect(resp.data.archives[index].stat.reply, + expect(resp.data!.archives![index]!.stat!.reply, jsonRes['data']['archives'][index]['stat']['reply']); - expect(resp.data.archives[index].stat.share, + expect(resp.data!.archives![index]!.stat!.share, jsonRes['data']['archives'][index]['stat']['share']); - expect(resp.data.archives[index].stat.view, + expect(resp.data!.archives![index]!.stat!.view, jsonRes['data']['archives'][index]['stat']['view']); } @@ -135,30 +134,30 @@ void main() { expect(resp.errorMsg, jsonRes['errorMsg']); for (var index = 0; index < jsonRes['data'].length; index++) { - expect(resp.data[index].name, jsonRes['data'][index]['name']); - expect(resp.data[index].courseId, jsonRes['data'][index]['courseId']); - expect(resp.data[index].id, jsonRes['data'][index]['id']); - expect(resp.data[index].order, jsonRes['data'][index]['order']); - expect(resp.data[index].parentChapterId, + expect(resp.data![index]!.name, jsonRes['data'][index]['name']); + expect(resp.data![index]!.courseId, jsonRes['data'][index]['courseId']); + expect(resp.data![index]!.id, jsonRes['data'][index]['id']); + expect(resp.data![index]!.order, jsonRes['data'][index]['order']); + expect(resp.data![index]!.parentChapterId, jsonRes['data'][index]['parentChapterId']); - expect(resp.data[index].visible, jsonRes['data'][index]['visible']); + expect(resp.data![index]!.visible, jsonRes['data'][index]['visible']); for (var cindex = 0; cindex < jsonRes['data'][index]['children'].length; cindex++) { - expect(resp.data[index].children[cindex].children, + expect(resp.data![index]!.children![cindex]!.children, jsonRes['data'][index]['children'][cindex]['children']); - expect(resp.data[index].children[cindex].visible, + expect(resp.data![index]!.children![cindex]!.visible, jsonRes['data'][index]['children'][cindex]['visible']); - expect(resp.data[index].children[cindex].parentChapterId, + expect(resp.data![index]!.children![cindex]!.parentChapterId, jsonRes['data'][index]['children'][cindex]['parentChapterId']); - expect(resp.data[index].children[cindex].order, + expect(resp.data![index]!.children![cindex]!.order, jsonRes['data'][index]['children'][cindex]['order']); - expect(resp.data[index].children[cindex].courseId, + expect(resp.data![index]!.children![cindex]!.courseId, jsonRes['data'][index]['children'][cindex]['courseId']); - expect(resp.data[index].children[cindex].name, + expect(resp.data![index]!.children![cindex]!.name, jsonRes['data'][index]['children'][cindex]['name']); - expect(resp.data[index].children[cindex].id, + expect(resp.data![index]!.children![cindex]!.id, jsonRes['data'][index]['children'][cindex]['id']); } @@ -168,7 +167,6 @@ void main() { }); test('test many empty', () { - String str = readFromFile('Empty'); EmptyResp resp = new EmptyResp(str); var jsonRes = json.decode(str); @@ -177,17 +175,15 @@ void main() { expect(resp.toString(), new EmptyResp(jsonRes).toString()); /// 逐个字段检查是否与json.decode()的结果相同 - expect(resp.qwe.asd, jsonRes['qwe']['asd']); - expect(resp.qwe.zxc, jsonRes['qwe']['zxc']); - expect(resp.qwe.qaz, jsonRes['qwe']['qaz']); + expect(resp.qwe!.asd, jsonRes['qwe']['asd']); + expect(resp.qwe!.zxc, jsonRes['qwe']['zxc']); + expect(resp.qwe!.qaz, jsonRes['qwe']['qaz']); /// 检查bean对象toString还原成json字符串后再交给json解析的结果是否与原始字符串相同 expect(jsonRes, json.decode(resp.toString())); }); - test('test fucking lists', () { - String str = readFromFile('Lists'); ListsResp resp = new ListsResp(str); var jsonRes = json.decode(str); @@ -196,16 +192,15 @@ void main() { expect(resp.toString(), new ListsResp(jsonRes).toString()); /// 逐个字段检查是否与json.decode()的结果相同 - expect(resp.asd[0][0][0], jsonRes['asd'][0][0][0]); - expect(resp.qwe[0][0][0].zxc, jsonRes['qwe'][0][0][0]['zxc']); + expect(resp.asd![0]![0]![0], jsonRes['asd'][0][0][0]); + expect(resp.qwe![0]![0]![0]!.zxc, jsonRes['qwe'][0][0][0]['zxc']); expect(resp.qaz, jsonRes['qaz']); /// 检查bean对象toString还原成json字符串后再交给json解析的结果是否与原始字符串相同 expect(jsonRes, json.decode(resp.toString())); -}); + }); test('test ignore map', () { - String str = readFromFile('IgnoreMap'); IgnoreMapResp resp = new IgnoreMapResp(str); var jsonRes = json.decode(str); @@ -214,15 +209,15 @@ void main() { expect(resp.toString(), new IgnoreMapResp(jsonRes).toString()); /// 逐个字段检查是否与json.decode()的结果相同 - expect(resp.data.wc, jsonRes['data']['wc']); - expect(resp.data.author, jsonRes['data']['author']); - expect(resp.data.content, jsonRes['data']['content']); - expect(resp.data.digest, jsonRes['data']['digest']); - expect(resp.data.title, jsonRes['data']['title']); - expect(resp.data.extra.a, jsonRes['data']['extra']['a']); - expect(resp.data.extra.b, jsonRes['data']['extra']['b']); - expect(resp.data.extra.c, jsonRes['data']['extra']['c']); - expect(resp.data.date, jsonRes['data']['date']); + expect(resp.data!.wc, jsonRes['data']['wc']); + expect(resp.data!.author, jsonRes['data']['author']); + expect(resp.data!.content, jsonRes['data']['content']); + expect(resp.data!.digest, jsonRes['data']['digest']); + expect(resp.data!.title, jsonRes['data']['title']); + expect(resp.data!.extra!.a, jsonRes['data']['extra']['a']); + expect(resp.data!.extra!.b, jsonRes['data']['extra']['b']); + expect(resp.data!.extra!.c, jsonRes['data']['extra']['c']); + expect(resp.data!.date, jsonRes['data']['date']); /// 检查bean对象toString还原成json字符串后再交给json解析的结果是否与原始字符串相同 expect(jsonRes, json.decode(resp.toString())); @@ -233,9 +228,11 @@ void main() { ListTopResp resp = ListTopResp(str); var jsonRes = json.decode(str); + /// 测试传入String和json进行解析的结果是否相同 expect(resp.toString(), new ListTopResp(jsonRes).toString()); - expect(resp.list.toString().replaceAll(' ', ''), json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', '')); + expect(resp.list.toString().replaceAll(' ', ''), + json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', '')); }); test('test list with string', () { @@ -243,18 +240,19 @@ void main() { ListWithStringResp resp = ListWithStringResp(str); var jsonRes = json.decode(str); + /// 测试传入String和json进行解析的结果是否相同 expect(resp.toString(), new ListWithStringResp(jsonRes).toString()); - expect(resp.toString().replaceAll(' ', ''), json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', '')); + expect(resp.toString().replaceAll(' ', ''), + json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', '')); }); - } String readFromFile(String name) { String str = new File('${name}Test.json').readAsStringSync(); int index1 = str.indexOf('{'); int index2 = str.indexOf('['); - index1 = index1 == -1 ? 0: index1; - index2 = index2 == -1 ? 0: index2; + index1 = index1 == -1 ? 0 : index1; + index2 = index2 == -1 ? 0 : index2; return str.substring(min(index1, index2)); } diff --git a/Test/test.sh b/Test/test.sh index f893920..3d00459 100644 --- a/Test/test.sh +++ b/Test/test.sh @@ -5,4 +5,4 @@ env PUB_ALLOW_PRERELEASE_SDK=quiet $(echo $(which flutter) | awk '{print substr( # 运行测试 echo test start... -$(echo $(which flutter) | awk '{print substr($0,0,length()-7)}')cache/dart-sdk/bin/dart --preview-dart-2 --null_safety test.dart \ No newline at end of file +$(echo $(which flutter) | awk '{print substr($0,0,length()-7)}')cache/dart-sdk/bin/dart test.dart diff --git a/check_version.py b/check_version.py index 694c5fd..6f3c9e4 100644 --- a/check_version.py +++ b/check_version.py @@ -16,7 +16,7 @@ from PyQt5.QtWidgets import QMessageBox from tools import msg_box_ui -code = 0.8 +code = 0.9 ignore_code = 0.0 check_last_version_thread = None diff --git a/formatter.py b/formatter.py index 30e8748..9d46412 100644 --- a/formatter.py +++ b/formatter.py @@ -284,5 +284,5 @@ def custom_ui(): custom_ui() widget.show() code = check_version() - widget.setWindowTitle(widget.windowTitle().replace('code', str(code))) + widget.setWindowTitle(widget.windowTitle().replace('code', str(code) + '-nullsafety')) sys.exit(app.exec_()) diff --git a/template_code.py b/template_code.py index bb00e1d..b254bbd 100644 --- a/template_code.py +++ b/template_code.py @@ -21,6 +21,8 @@ class ${type} { String toString() { return '{${toString}}'; } + + String toJson() => this.toString(); } """ @@ -29,7 +31,7 @@ class ${type} { ${list_count}${class_type}${>count} ${name}${current_child} = ${name}${parent_items} == null ? null : []; for (var ${name}${current_items} in ${name}${current_child} == null ? [] : ${name}${parent_items}){ ${loop} - ${name}${current_child}.add(${name}${child_child}); + ${name}${current_child}!.add(${name}${child_child}); } """ diff --git a/tools.py b/tools.py index 6366ece..f5f7715 100644 --- a/tools.py +++ b/tools.py @@ -57,7 +57,7 @@ def check_level_type(t): def list_code_loop(code, count, total, n, ct): list_count = 'List<' * (total - count) - ab_count = '>' * (total - count) + ab_count = '?>' * (total - count) + '?' current_child = 'Child' * count child_child = 'Child' * (count + 1) @@ -75,7 +75,7 @@ def list_code_loop(code, count, total, n, ct): # 向代码模板中插入变量 def build_list_construction(t, f, n): - class_type = t.replace('List<', '').replace('>', '') + class_type = t.replace('List<', '').replace('>', '').replace('?', '') list_loop = 'jsonRes[\'%s\'] == null ? null : [];\n' % f assert isinstance(t, str) @@ -102,7 +102,7 @@ def add_param_to_code(code, param): (f, t, n) = param # 先按照基本数据类型方式处理 - properties = ' %s %s;\n' % (t, n) + properties = ' %s? %s;\n' % (t, n) this_properties = 'this.%s, ' % n construction = ' %s = jsonRes[\'%s\'];\n' % (n, f) to_string = '"%s": $%s, ' % (f, n) @@ -122,7 +122,7 @@ def add_param_to_code(code, param): t_code = check_level_type(t) # 字符串类型、List类型和Map类型处理,需要修改toString中的输出方式 - if t_code in [0, 2] or 'List' in t: + if t_code in [0, 2] or 'List' in t: code = code.replace(': $%s' % n, ': ${%s != null?\'${json.encode(%s)}\':\'null\'}' % (n, n)) # dict类型处理,只需要修改construction中的输出方式 @@ -174,6 +174,9 @@ def build_level_code(level_bean): child_bean.append(level_bean.pop(0)) build_level_code(child_bean) + if t != 'List<>': + t = t.replace('>', '?>') + # 不管如何,到这里的数据都是目前dict的一级子数据,作为参数传入模板中 code = add_param_to_code(code, (f, t, n)) codes.append(code.replace(', ${toString}', '').replace('${construction}', '').replace('${properties}', '').replace('${this.properties}', '')) @@ -205,7 +208,7 @@ def generate_code(work_bean): res = res.replace('jsonRes[\'json_list\']', 'jsonRes', 2) # 如果json中存在空list这种操蛋情况,将list类型从list<>修改成list - res = res.replace('List<>', 'List') + res = res.replace('List<>', 'List') # 移除参数构造函数用模板生成后多余的逗号和空格 res = res.replace(', });', '});') @@ -225,9 +228,10 @@ def generate_code(work_bean): out_res += (line + '\n') if first and r'.fromParams({this.' in line: class_name = line.split(r'.fromParams({this.')[0].strip() - out_res += '\n factory %s(jsonStr) => [\'null\', \'\', null].contains(jsonStr) ? null : ' \ - 'jsonStr is String ? %s.fromJson(json.decode(jsonStr)) : %s.fromJson(jsonStr);\n' \ + out_res += '\n factory %s(Object jsonStr) => jsonStr is String ? %s.fromJson(json.decode(jsonStr)) : %s.fromJson(jsonStr);\n' \ % (class_name, class_name, class_name) + out_res += '\n static %s? parse(jsonStr) => [\'null\', \'\', null].contains(jsonStr) ? null : %s(jsonStr);\n' \ + % (class_name, class_name) first = False return out_res diff --git a/version b/version index f726831..9bf5bf5 100644 --- a/version +++ b/version @@ -1,4 +1,4 @@ { - "code": 0.7, - "desc": "1.可以通过下拉框将对象类型设置为Map来避免该对象字段的解析,参考issue #13\n2.点击Name栏使字段转为驼峰 & 解析List的时候读取多个元素,参考issue #17\n3.修复闪退崩溃issue #23" + "code": 0.9, + "desc": "迁移至空安全语法。" }