diff --git a/shell/make_service.py b/shell/make_service.py index c7d00d3..8ab2ea2 100644 --- a/shell/make_service.py +++ b/shell/make_service.py @@ -7,6 +7,7 @@ sysPath = os.getcwd() protoAddress = f"{sysPath}/protobuf" tempPath = f"{sysPath}/storage/temp" daoPath = f"{sysPath}/storage/dao" +servicePath = f"{sysPath}/storage/service" class ProtoOption(object): @@ -121,14 +122,27 @@ def gen_dao_interface(proto): with open(temp_path, 'w', encoding='utf-8') as f: f.write(text) + +def gen_single_service(proto): + tem_text = load_template("service.template") + template = Template(tem_text) + + text = template.render(keys=proto) + + temp_path = f"{servicePath}/single_service.gen.go" + 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") def main(): # 加载 protobuf protobuf = load_protobuf() # 生成 dao 接口 gen_dao_interface(protobuf) + gen_single_service(protobuf) format_code_go() if __name__ == "__main__": diff --git a/storage/temp/service.template b/storage/temp/service.template new file mode 100644 index 0000000..e1eb81e --- /dev/null +++ b/storage/temp/service.template @@ -0,0 +1,35 @@ +// Code generated by gen-struct. DO NOT EDIT. +// make gen-service generated +package service + +import ( + context "context" + + "gitee.com/timedb/wheatCache/pkg/event" + "gitee.com/timedb/wheatCache/pkg/lru" + "gitee.com/timedb/wheatCache/pkg/proto" +) + +{% for key in keys %} +func (s *singleService) {{key.method}}( + ctx context.Context, + req *proto.{{key.method}}Request, +) (*proto.{{key.method}}Response, error) { + work := event.EventWorkFunc(func() (interface{}, error) { + return s.dao.{{key.method}}({% for opt in key.option %}req.{{opt[0]}}, {% endfor %}) + }) + + lruEvent := s.lruProduce.NewEvent(lru.OptionEventName) + lruEvent.InitWaitEvent() + lruEvent.SetValue(lru.WorkFuncEventKey, work) + s.lruProduce.Call(ctx, lruEvent) + resp, err := lruEvent.StartWaitEvent(s.timeOut) + s.lruProduce.Recovery(lruEvent) + if err != nil { + return nil, err + } + + return resp.(*proto.{{key.method}}Response), nil +} + +{% endfor %} \ No newline at end of file