forked from p93542168/wheat-cache
自动生成代码脚本
This commit is contained in:
parent
81f85009ab
commit
d9a9da6dee
|
@ -0,0 +1,109 @@
|
|||
import os
|
||||
|
||||
|
||||
def DealList(li: list):
|
||||
# 对列表进行处理
|
||||
flag = 0
|
||||
InterName = []
|
||||
BlankNum = []
|
||||
SliceNum = []
|
||||
for i in range(len(li)):
|
||||
if flag == 0:
|
||||
SliceNum.append(i)
|
||||
InterName.append(f"{li[i].lower().title()}Interface".replace("_", "").replace(":", ""))
|
||||
flag = 1
|
||||
if li[i] == '':
|
||||
BlankNum.append(i)
|
||||
flag = 0
|
||||
Dict = {}
|
||||
for i in range(len(InterName)-1):
|
||||
Dict[InterName[i]] = li[SliceNum[i]+1:BlankNum[i]]
|
||||
Dict[InterName[-1]] = li[BlankNum[-1]+2:]
|
||||
return Dict
|
||||
|
||||
|
||||
def SetInterface():
|
||||
# 对读取到的yaml进行处理
|
||||
Temp = []
|
||||
path = os.getcwd()
|
||||
WirtePath = f"{path}/pkg/structure/interface.go"
|
||||
ReadPath = f"{path}/pkg/structure/generate/tem.yaml"
|
||||
with open(ReadPath, "r") as f:
|
||||
title = f.read()
|
||||
title = title.split("\n")[2:]
|
||||
for i in title:
|
||||
if ' - ' not in i:
|
||||
Temp.append(i)
|
||||
if ' - ' in i:
|
||||
i = i.split("-")[1]
|
||||
Temp.append(i)
|
||||
|
||||
# 对列表进行处理,便于自动补全
|
||||
dic = DealList(Temp)
|
||||
|
||||
# 通过传入的列表对interface.go进行自动补全
|
||||
Temp = []
|
||||
AppendList = []
|
||||
with open(WirtePath, "r") as f:
|
||||
context = f.read()
|
||||
context = context.split("\n")
|
||||
flag = 0
|
||||
for i in context:
|
||||
if "interface" in i:
|
||||
flag = 1 # 开锁
|
||||
if "}" in i:
|
||||
Temp.append(i)
|
||||
AppendList.append(Temp)
|
||||
Temp = []
|
||||
flag = 0 # 上锁
|
||||
if flag == 1:
|
||||
Temp.append(i)
|
||||
|
||||
# 拆分成功, 开始匹配
|
||||
Del = []
|
||||
res = context[0:20]
|
||||
for i in AppendList[2:]:
|
||||
x = i[0:2]
|
||||
for key, values in dic.items():
|
||||
if key in i[0]:
|
||||
Del.append(key)
|
||||
for val in values:
|
||||
x.append(f'\t{val.title().lstrip()}(*pr'
|
||||
f'oto.{val.title().lstrip()}Request) ([]string, error)'.replace("_", ""))
|
||||
x.append("\n}\n\n")
|
||||
for j in x:
|
||||
res.append(j)
|
||||
for i in Del:
|
||||
dic.pop(i)
|
||||
# 添加新增的
|
||||
for key, values in dic.items():
|
||||
w = []
|
||||
w.append(f"type {key} interface" + "{")
|
||||
w.append('\tKeyBaseInterface')
|
||||
for val in values:
|
||||
w.append(f'\t{val.title().lstrip()}(*pr'
|
||||
f'oto.{val.title().lstrip()}Request) ([]string, error)'.replace("_", ""))
|
||||
w.append("\n}\n\n")
|
||||
for j in w:
|
||||
res.append(j)
|
||||
f = open(WirtePath, 'w')
|
||||
judge = ""
|
||||
for i in res:
|
||||
if judge == i:
|
||||
continue
|
||||
judge = i
|
||||
if "package" in i:
|
||||
i = f"{i}\n"
|
||||
if "import" in i:
|
||||
i = f"{i}\n"
|
||||
if i == '':
|
||||
i = '\n'
|
||||
if i == '}' or i == '\n{\n':
|
||||
i = '\n}\n'
|
||||
i = i.replace('\t', '\n\t')
|
||||
f.write(i)
|
||||
f.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
SetInterface()
|
Loading…
Reference in New Issue