forked from p93542168/wheat-cache
feat(shell): add gen service shell
This commit is contained in:
parent
ea8e10fcbf
commit
c67e0c863c
|
@ -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__":
|
||||
|
|
|
@ -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 %}
|
Loading…
Reference in New Issue