fix(gen-struct): add struct

This commit is contained in:
bandl 2021-09-20 16:36:47 +08:00 committed by yu_lang
parent cf4b5cd63c
commit b9060a64d5
2 changed files with 37 additions and 97 deletions

View File

@ -1,21 +0,0 @@
syntax = "proto3";
import "base.proto";
option go_package = "pkg/proto";
message LSetRequest {
BaseKey key = 1;
}
message LGetRequest {
BaseKey key = 1;
}
message LAddRequest {
BaseKey key = 1;
}
message LQueueRequest {
BaseKey key = 1;
}
message LSetbitRequest {
BaseKey key = 1;
}
message LGetbitRequest {
BaseKey key = 1;
}

View File

@ -24,92 +24,53 @@ def load_conf():
key = to_camel(key).lower() key = to_camel(key).lower()
cfg_camel[key] = [to_camel(v) for v in val] cfg_camel[key] = [to_camel(v) for v in val]
# print(cfg_camel)
return cfg, cfg_camel return cfg, cfg_camel
# 生成文件,并写入 # 生成文件,并写入
def mkdir(cfg_camel): def mkdir(cfg_camel):
path = "protobuf" path = "protobuf"
storagePath = f"{protobufPath}/storage.proto" path = path.strip()
for key,value in cfg_camel.items(): # 判断路径是否存在
'''生成、更新数据结构proto文件''' isExists = os.path.exists(path)
proto_path = path + '/' + key + '.proto' # 判断结果
if not os.path.exists(proto_path): # 如果这个文件不存在 if not isExists:
'''生成对应的数据结构proto文件''' os.makedirs(path)
file = open(proto_path,'w') print(path + ' 创建成功')
file.write('syntax = "proto3";\nimport "base.proto";\noption go_package = "pkg/proto";\n') else: # 如果目录存在则不创建,并提示目录已存在
for v in value: # print(path + ' 目录已存在')
file.write('\nmessage ' + v +'Request '+'{\n\tBaseKey key = 1;\n}') for key,value in cfg_camel.items():
file.close() # print(key)
'''给strong.proto添加import该新的数据结构''' proto_path = path + '/' + key + '.proto'
import_structure = 'import "'+ key +'.proto";\n' if not os.path.exists(proto_path): # 如果这个文件不存在
mess = 'message CommendResponse {\n' file = open(proto_path,'w')
with open(storagePath) as f: file.write('syntax = "proto3";\nimport "base.proto";\noption go_package = "pkg/proto";\n')
line = f.readlines() for v in value:
# print(line) file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}\n')
f.close file.close()
for i in range(len(line)): else: # 如果这个文件存在
if mess in line[i]: with open(proto_path) as f:
break line = f.readlines()
line.insert(i,import_structure) # print(line)
file = open(storagePath,'w') f.close()
for l in line: for v in value:
file.write(l) function = v + 'Request'
file.close() flag = 0
i=0 for l in line:
for i in range(len(line)-1,0,-1): if function in l:
if '}' in line[i]: flag=1
break
for v in value:
line.insert(i,'\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n')
file = open(storagePath,'w')
for l in line:
file.write(l)
file.close()
else: # 如果这个文件存在
with open(proto_path) as f:
line = f.readlines()
f.close()
for v in value:
function = v + 'Request'
flag = 0
for l in line:
if function in l:
flag=1
break
if flag == 0:
file = open(proto_path,'a')
file.write('\nmessage ' + v +'Request '+'{\n\tBaseKey key = 1;\n}')
file.close()
flag = 0
'''更新storage.proto文件'''
with open(storagePath) as f:
line = f.readlines()
f.close
for v in value:
flg=0
for l in range(len(line)):
if '\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n' in line[l]:
flg = 1
break
if flg==0:
i=0
for i in range(len(line)-1,0,-1):
if '}' in line[i]:
break break
line.insert(i,'\trpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') if flag == 0:
i=0 file = open(proto_path,'a')
file = open(storagePath,'w') file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}\n')
for l in line: file.close()
file.write(l) flag = 0
file.close()
if __name__ == "__main__": if __name__ == "__main__":
conf, cfg_camel = load_conf() conf, cfg_camel = load_conf()
print(cfg_camel) #
mkdir(cfg_camel) mkdir(cfg_camel)