feat(proto):add proto auto tool

This commit is contained in:
yu_lang 2021-09-20 21:04:01 +08:00
parent 2e9216d894
commit 8bc30d3b85
3 changed files with 115 additions and 40 deletions

22
protobuf/listx.proto Normal file
View File

@ -0,0 +1,22 @@
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

@ -2,12 +2,24 @@ syntax = "proto3";
option go_package = "pkg/proto"; option go_package = "pkg/proto";
import "stringx.proto"; import "stringx.proto";
import "listx.proto";
message CommendResponse { message CommendResponse {
repeated string result = 1; repeated string result = 1;
} }
service CommServer { service CommServer {
rpc Get (GetRequest) returns (CommendResponse);
rpc Set (SetRequest) returns (CommendResponse); rpc Set (SetRequest) returns (CommendResponse);
rpc Get (GetRequest) returns (CommendResponse);
rpc Add (AddRequest) returns (CommendResponse);
rpc Reduce (ReduceRequest) returns (CommendResponse);
rpc Setbit (SetbitRequest) returns (CommendResponse);
rpc Getbit (GetbitRequest) returns (CommendResponse);
rpc LSet (LSetRequest) returns (CommendResponse);
rpc LGet (LGetRequest) returns (CommendResponse);
rpc LAdd (LAddRequest) returns (CommendResponse);
rpc LQueue (LQueueRequest) returns (CommendResponse);
rpc LSetbit (LSetbitRequest) returns (CommendResponse);
rpc LGetbit (LGetbitRequest) returns (CommendResponse);
} }

View File

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