feat(proto):update proto auto tool

This commit is contained in:
yu_lang 2021-09-21 11:03:31 +08:00
parent 53fe26db48
commit f3e894a068
4 changed files with 39 additions and 91 deletions

View File

@ -7,3 +7,6 @@ STRING_X:
- reduce - reduce
- setbit - setbit
- getbit - getbit
LIST_X:
- set

View File

@ -1,21 +1,7 @@
syntax = "proto3"; syntax = "proto3";
import "base.proto"; import "base.proto";
option go_package = "pkg/proto"; option go_package = "pkg/proto";
message LSetRequest {
BaseKey key = 1; message SetRequest {
} 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

@ -1,36 +1,23 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-protobuf generated
syntax = "proto3"; syntax = "proto3";
option go_package = "pkg/proto"; option go_package = "pkg/proto";
import "stringx.proto"; import "stringx.proto";
import "listx.proto"; import "listx.proto";
message CommendResponse { message CommendResponse {
repeated string result = 1; repeated string result = 1;
} }
service CommServer { service CommServer {
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);
rpc Set (SetRequest) returns (CommendResponse); rpc Set (SetRequest) returns (CommendResponse);
rpc Get (GetRequest) returns (CommendResponse); rpc Get (GetRequest) returns (CommendResponse);
rpc Add (AddRequest) returns (CommendResponse); rpc Add (AddRequest) returns (CommendResponse);
rpc Reduce (ReduceRequest) returns (CommendResponse); rpc Reduce (ReduceRequest) returns (CommendResponse);
rpc Setbit (SetbitRequest) returns (CommendResponse); rpc Setbit (SetbitRequest) returns (CommendResponse);
rpc Getbit (GetbitRequest) returns (CommendResponse); rpc Getbit (GetbitRequest) returns (CommendResponse);
rpc LSet (LSetRequest) returns (CommendResponse); rpc Set (SetRequest) 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

@ -27,12 +27,11 @@ def load_conf():
return cfg, cfg_camel return cfg, cfg_camel
# 生成文件,并写入 '''生成对应的数据结构proto文件'''
def mkdir(cfg_camel): def mk_structure(cfg_camel):
path = "protobuf" path = "protobuf"
storagePath = f"{protobufPath}/storage.proto" storagePath = f"{protobufPath}/storage.proto"
for key,value in cfg_camel.items(): for key, value in cfg_camel.items():
'''生成、更新数据结构proto文件'''
proto_path = path + '/' + key + '.proto' proto_path = path + '/' + key + '.proto'
if not os.path.exists(proto_path): # 如果这个文件不存在 if not os.path.exists(proto_path): # 如果这个文件不存在
'''生成对应的数据结构proto文件''' '''生成对应的数据结构proto文件'''
@ -41,34 +40,9 @@ def mkdir(cfg_camel):
for v in value: for v in value:
file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}')
file.close() file.close()
'''给strong.proto添加import该新的数据结构'''
import_structure = 'import "'+ key +'.proto";\n'
mess = 'message CommendResponse {\n'
with open(storagePath) as f:
line = f.readlines()
# print(line)
f.close
for i in range(len(line)):
if mess in line[i]:
break
line.insert(i,import_structure)
file = open(storagePath,'w')
for l in line:
file.write(l)
file.close()
i=0
for i in range(len(line)-1,0,-1):
if '}' in line[i]:
break
for v in value:
line.insert(i,' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n')
file = open(storagePath,'w')
for l in line:
file.write(l)
file.close()
else: # 如果这个文件存在 else: # 如果这个文件存在
'''更新数据结构proto文件'''
with open(proto_path) as f: with open(proto_path) as f:
line = f.readlines() line = f.readlines()
f.close() f.close()
@ -84,32 +58,30 @@ def mkdir(cfg_camel):
file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}')
file.close() file.close()
flag = 0 flag = 0
'''更新storage.proto文件'''
with open(storagePath) as f: print(f"{key}.proto", "-> success")
line = f.readlines()
f.close
for v in value: def mk_storage(cfg_camel):
flg=0 storagePath = f"{protobufPath}/storage.proto"
for l in range(len(line)): file = open(storagePath,'w')
if ' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n' in line[l]: file.write('// Code generated by gen-struct. DO NOT EDIT.\n// make gen-protobuf generated\n\nsyntax = "proto3";\n\noption go_package = "pkg/proto";\n\n')
flg = 1 for key,value in cfg_camel.items():
break file.write('import "'+ key +'.proto";\n')
if flg==0: file.write('\nmessage CommendResponse {\n repeated string result = 1;\n}\n\nservice CommServer {\n')
i=0 for key,value in cfg_camel.items():
for i in range(len(line)-1,0,-1): for v in value:
if '}' in line[i]: file.write(' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n')
break file.write('}')
line.insert(i,' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') file.close()
i=0 print("storage.proto", "-> success")
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) mk_structure(cfg_camel) # 生成对应的数据结构proto文件
mkdir(cfg_camel) mk_storage(cfg_camel)