Skip to content

Commit 3621c69

Browse files
committed
修复多个连续的空数组导致的错误泛型关联问题;修复json字符串中含有'{}'这种空map导致的错误;针对泛型为普通数据类型时优化移除无用的list取值循环
1 parent 4a67432 commit 3621c69

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

formater.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ def change_background_color(com_box, current_text):
8787

8888

8989
# 生成字段类型下拉框
90-
def get_type_combobox(line):
90+
def get_type_combobox(need_connect, line):
9191
com_box = QtWidgets.QComboBox()
9292
com_box.setEditable(True)
9393
obj_type = int(line[-1]) if line[-1].isdigit() else 0
9494
global last_list_com_box
9595

9696
com_box.currentTextChanged.connect(partial(change_background_color, com_box))
9797

98-
if last_list_com_box is not None:
98+
if need_connect and last_list_com_box is not None:
9999
com_box.currentTextChanged.connect(partial(change_text, last_list_com_box))
100100
last_list_com_box = None
101101

@@ -134,12 +134,14 @@ def update_list(json_str):
134134
ui.tv_fields.setRowCount(len(res))
135135

136136
pre_type_combobox = None
137+
pre_index = -1
137138
ii = 0
138139
for i in range(len(res)):
139140
line = res[i]
140141
assert isinstance(line, str)
141142
index = line.find('<')
142-
temp_type_combobox = get_type_combobox(line)
143+
temp_type_combobox = get_type_combobox(index != pre_index, line)
144+
pre_index = index
143145
ui.tv_fields.setCellWidget(ii, 1, temp_type_combobox)
144146
if temp_type_combobox.count() > 1 and pre_type_combobox is not None and pre_type_combobox.currentText().startswith('List'):
145147
ui.tv_fields.setRowCount(ui.tv_fields.rowCount() - 1)
@@ -175,8 +177,9 @@ def json_format():
175177
# 将格式化后的json字符串覆盖到文本编辑框中
176178
ui.te_json.setText(jformat(json_str.replace('\n', '')))
177179

180+
# 为了修复json中含有内容为空的对象,也就是有'{}'这种玩意时解析失败的问题,先预处理把'{}'全部替换成null
178181
# 根据json更新表格条目
179-
update_list(json_str)
182+
update_list(json_str.replace('{}', 'null'))
180183

181184
else:
182185
msg = QtWidgets.QMessageBox()
@@ -236,22 +239,6 @@ def init_view():
236239
def custom_ui():
237240
init_view()
238241
init_event()
239-
#
240-
# json_str = '''
241-
# {
242-
# "x": {
243-
# "e": [
244-
# {}
245-
# ],
246-
# "q": [],
247-
# "t": []
248-
# }
249-
# }
250-
#
251-
# '''
252-
#
253-
# ui.te_json.setText(json_str)
254-
# ui.btn_format.click()
255242

256243

257244
if __name__ == "__main__":

tools.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,28 @@ def generate_code(work_bean):
198198
# 移除参数构造函数用模板生成后多余的逗号和空格
199199
res = res.replace(', });', '});')
200200

201+
# 移除没有必要的list取值循环,这次循环只是修改list声明代码,并将不需要的行加上注释
202+
# 下面一次的循环则忽略这些注释的行达到删除的效果
203+
lines = res.splitlines()
204+
for index in range(len(lines)):
205+
if r'.add(' in lines[index]:
206+
sp = lines[index].find(r'.add(')
207+
ep = lines[index].rfind(r')')
208+
if r'(' not in lines[index][sp + 5:ep]:
209+
sp = lines[index - 2].find('in ')
210+
ep = lines[index - 2].rfind(')')
211+
list_src = lines[index - 2][sp + 3:ep]
212+
lines[index - 4] = lines[index - 4].replace('[]', list_src)
213+
for i in range(6):
214+
lines[index - 3 + i] = '//%s' % lines[index - 3 + i]
215+
201216
# 最终修改,添加json库导包代码,并为顶层对象增加默认构造
202217
out_res = 'import \'dart:convert\' show json;\n'
203218
first = True
204-
for line in res.splitlines():
205-
out_res += (line+'\n')
219+
for line in lines:
220+
if line.startswith('//'):
221+
continue
222+
out_res += (line + '\n')
206223
if first and r'.fromParams({this.' in line:
207224
class_name = line.split(r'.fromParams({this.')[0].strip()
208225
out_res += '\n factory %s(jsonStr) => %s.fromJson(json.decode(jsonStr));\n' % (class_name, class_name)

0 commit comments

Comments
 (0)