Skip to content
Merged
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
59 changes: 59 additions & 0 deletions Test/ListWithStringResp.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'dart:convert' show json;

class ListWithStringResp {
List<String> asd;
List<List<String>> qaz;
List<Qwe> 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);

ListWithStringResp.fromJson(jsonRes) {
asd = jsonRes['asd'] == null ? null : [];

for (var asdItem in asd == null ? [] : jsonRes['asd']) {
asd.add(asdItem);
}

qaz = jsonRes['qaz'] == null ? null : [];

for (var qazItem in qaz == null ? [] : jsonRes['qaz']) {
List<String> qazChild = qazItem == null ? null : [];
for (var qazItemItem in qazChild == null ? [] : qazItem) {
qazChild.add(qazItemItem);
}
qaz.add(qazChild);
}

zxc = jsonRes['zxc'] == null ? null : [];

for (var zxcItem in zxc == null ? [] : jsonRes['zxc']) {
zxc.add(zxcItem == null ? null : Qwe.fromJson(zxcItem));
}
}

@override
String toString() {
return '{"asd": ${asd != null ? '${json.encode(asd)}' : 'null'}, "qaz": ${qaz != null ? '${json.encode(qaz)}' : 'null'}, "zxc": $zxc}';
}
}

class Qwe {
String qwe;

Qwe.fromParams({this.qwe});

Qwe.fromJson(jsonRes) {
qwe = jsonRes['qwe'];
}

@override
String toString() {
return '{"qwe": ${qwe != null ? '${json.encode(qwe)}' : 'null'}}';
}
}
5 changes: 5 additions & 0 deletions Test/ListWithStringTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"asd": ["asd", "zxc", "qwe"],
"qaz": [["qaz"]],
"zxc": [{"qwe": "qwe"}]
}
11 changes: 11 additions & 0 deletions Test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'WanResp.dart';
import 'ListsResp.dart';
import 'IgnoreMapResp.dart';
import 'ListTopResp.dart';
import 'ListWithStringResp.dart';

void main() {

Expand Down Expand Up @@ -237,6 +238,16 @@ void main() {
expect(resp.list.toString().replaceAll(' ', ''), json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', ''));
});

test('test list with string', () {
String str = readFromFile('ListWithString');
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(' ', ''));
});

}

String readFromFile(String name) {
Expand Down
2 changes: 1 addition & 1 deletion Test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 test.dart
$(echo $(which flutter) | awk '{print substr($0,0,length()-7)}')cache/dart-sdk/bin/dart --preview-dart-2 --null_safety test.dart
10 changes: 5 additions & 5 deletions tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ def add_param_to_code(code, param):

t_code = check_level_type(t)

# 字符串类型和Map类型处理,只需要修改toString中的输出方式
if t_code in [0, 2]:
# 字符串类型、List<String>类型和Map类型处理,需要修改toString中的输出方式
if t_code in [0, 2] or 'List<String>' in t:
code = code.replace(': $%s' % n, ': ${%s != null?\'${json.encode(%s)}\':\'null\'}' % (n, n))

# dict类型处理,只需要修改construction中的输出方式
elif t_code == 4:
code = code.replace('jsonRes[\'%s\']' % f, 'jsonRes[\'%s\'] == null ? null : %s.fromJson(jsonRes[\'%s\'])' % (f, t, f))

# list类型处理,只需要修改construction中的输出方式
elif t_code == 3:
if t_code == 3:
list_loop = build_list_construction(t, f, n)

code = code.replace('jsonRes[\'%s\'];' % f, list_loop)
Expand Down Expand Up @@ -225,8 +225,8 @@ 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) => jsonStr == null ? null : jsonStr is String ? %s.fromJson(json.decode(jsonStr)) : ' \
'%s.fromJson(jsonStr);\n' \
out_res += '\n factory %s(jsonStr) => [\'null\', \'\', null].contains(jsonStr) ? null : ' \
'jsonStr is String ? %s.fromJson(json.decode(jsonStr)) : %s.fromJson(jsonStr);\n' \
% (class_name, class_name, class_name)
first = False
return out_res
Expand Down