feat(shell): add parse proto map

This commit is contained in:
bandl 2021-11-04 20:17:24 +08:00
parent 88774daede
commit dcada2f2fe
1 changed files with 19 additions and 5 deletions

View File

@ -32,6 +32,9 @@ class ProtoOption(object):
def dist_to_ProOpt(req, resp) -> List[ProtoOption]:
def to_camel(val: str) -> str:
return "".join([k.capitalize() for k in val.split('_')])
def parse_type(l: str) -> List[str]:
l = l.strip()
if l == "":
@ -48,14 +51,19 @@ def dist_to_ProOpt(req, resp) -> List[ProtoOption]:
val = l_list[0]
if val == "BaseKey":
val = "*proto.BaseKey"
result.append([l_list[1].capitalize(), val])
result.append([to_camel(l_list[1]), val])
elif val == "repeated":
val = f"[]{l_list[1]}"
result.append([l_list[2].capitalize(), val])
result.append([to_camel(l_list[2]), val])
elif "map" in val:
resMap = re.findall(
r"^map\s*<(.*?)\s*,\s*(.*?)\s*>.*?(\w+).*?", l_opt)
if len(resMap[0]) == 3:
mapKey, mapVal, var = resMap[0]
result.append([to_camel(var), f"map[{mapKey}]{mapVal}"])
else:
result.append([l_list[1].capitalize(), val])
result.append([to_camel(l_list[1]), val])
return result
lists = []
@ -104,6 +112,7 @@ def load_protobuf() -> List[ProtoOption]:
li.extend(parse_protobuf_to_Opt(name))
return li
def go_fmt(path: str):
os.system(f"go fmt {path}")
@ -112,6 +121,7 @@ def load_template(name: str) -> str:
with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp:
return fp.read()
def gen_dao_interface(proto):
tem_text = load_template("dao.template")
@ -134,6 +144,7 @@ def gen_single_service(proto):
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
def gen_aof(proto):
tem_text = load_template("aof.template")
template = Template(tem_text)
@ -143,7 +154,7 @@ def gen_aof(proto):
temp_path = f"{aofPath}/codec.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
# 生成 AOF 恢复机制
tem_text = load_template("dao_aof.template")
template = Template(tem_text)
@ -154,12 +165,14 @@ def gen_aof(proto):
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
def format_code_go():
go_fmt(f"{daoPath}/interface.gen.go")
go_fmt(f"{servicePath}/single_service.gen.go")
go_fmt(f"{aofPath}/codec.gen.go")
go_fmt(f"{daoPath}/dao.gen.go")
def main():
# 加载 protobuf
protobuf = load_protobuf()
@ -174,5 +187,6 @@ def main():
gen_aof(protobuf)
format_code_go()
if __name__ == "__main__":
main()