!38 feat-storage-template

Merge pull request !38 from bandl/feat-storage-tempalte
This commit is contained in:
bandl 2021-09-28 15:07:49 +00:00 committed by Gitee
commit c11d10d6e2
10 changed files with 355 additions and 78 deletions

View File

@ -5,4 +5,4 @@ env: 'dev'
storage:
host: '127.0.0.1'
port: 5890
timeOut: 2 # second

View File

@ -1,7 +1,7 @@
// Code generated by gen-struct-const. DO NOT EDIT.
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package generate
package structure
const (
DEFAULT_KEY = iota

View File

@ -1,7 +1,7 @@
// Code generated by gen-struct-const. DO NOT EDIT.
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package generate
package structure
const (
DEFAULT_KEY = iota

View File

@ -1,4 +1,4 @@
// Code generated by gen-struct-const. DO NOT EDIT.
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package structure

View File

@ -1,45 +1,78 @@
// TODO 添加 storage server 模板
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package server
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
{% for name in mod_name %}
"gitee.com/timedb/wheatCache/pkg/structure/{{name}}"
{% endfor %}
"time"
)
type server struct {
type serverSingle struct {
middleProduce event.ProduceInterface
lruProduce event.ProduceInterface
ttl time.Duration
}
func NewServer() proto.CommServerServer {
ser := &server{}
ser := &serverSingle{}
return ser
}
func (s *server) Get(
// TODO 移除
func mockLruValue() structure.KeyBaseInterface {
return stringx.NewStringSingle()
}
{% for opt in option %}
{% for fun in opt.option %}
func (s *serverSingle) {{ fun }} (
cxt context.Context,
req *proto.GetRequest,
) (*proto.CommendResponse, error) {
panic("implement me")
}
req *proto.{{ fun }}Request,
) (*proto.{{ fun }}Response, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
func (s *server) Set(
ctx context.Context,
req *proto.SetRequest,
) (*proto.CommendResponse, error) {
panic("implement me")
}
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.{{ opt.key }}Interface:
resp, err := value.(structure.{{ opt.key }}Interface).{{ fun }}(req)
if err != nil {
return nil, err
}
return resp, nil
func (s *server) Add(ctx context.Context, request *proto.AddRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
default:
return nil, errorx.New("value err")
}
func (s *server) Reduce(ctx context.Context, request *proto.ReduceRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
})
func (s *server) Setbit(ctx context.Context, request *proto.SetbitRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
func (s *server) Getbit(ctx context.Context, request *proto.GetbitRequest) (*proto.CommendResponse, error) {
panic("implement me")
switch resp.(type) {
case *proto.{{ fun }}Response:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.{{ fun }}Response), nil
}
{% endfor %}
{% endfor %}

View File

@ -1,4 +1,5 @@
# 这里定义结构体的支持的命令, 以后也许会添加结构体的命令验证
# 定义结构体名称以及方法使, 方法全部小写,结构体名称全部大写且结构体名称需要加上 _X, 如 LIST_X
STRING_X:
- set

View File

@ -1,4 +1,4 @@
// Code generated by gen-struct-const. DO NOT EDIT.
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package structure

View File

@ -7,6 +7,7 @@ sysPath = os.getcwd()
tempPath = f"{sysPath}/pkg/structure/generate"
structurePath = f"{sysPath}/pkg/structure"
protobufPath = f"{sysPath}/protobuf"
storagePath = f"{sysPath}/storage"
class KeyMap(object):
@ -24,6 +25,15 @@ def to_camel(val: str) -> str:
return "".join([k.capitalize() for k in val.split('_')])
class KeyOption(object):
def __init__(self, key, option):
self.key = to_camel(key)
self.option = option
def __str__(self):
return "{}:{}".format(self.key, self.option)
def load_template(name: str) -> str:
with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp:
return fp.read()
@ -59,7 +69,7 @@ def set_structure_const_template(conf: dict):
template = Template(tem_text)
text = template.render(keys=keys, key_maps=key_map, sets=val_set)
temp_path = f"{tempPath}/structure.gen.go"
temp_path = f"{structurePath}/const.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
@ -79,14 +89,31 @@ def set_structure_interface(conf):
f.write(text)
def set_storage_server(server_conf):
mod_name = [i.replace("_", "").lower() for i in server_conf.keys()]
option = []
for key, item in server_conf.items():
option.append(KeyOption(key, item))
text = load_template("storage.template")
template = Template(text)
text = template.render(option=option, mod_name=mod_name)
temp_path = f"{storagePath}/server/single.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
def format_code_go():
go_fmt(f"{structurePath}/interface.gen.go")
go_fmt(f"{structurePath}/generate/structure.gen.go")
go_fmt(f"{structurePath}/const.gen.go")
go_fmt(f"{storagePath}/server/*.go")
if __name__ == "__main__":
conf, cfg_camel = load_conf()
set_structure_const_template(conf)
set_structure_interface(cfg_camel)
set_storage_server(cfg_camel)
# 格式化代码
format_code_go()

View File

@ -1,44 +0,0 @@
package server
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/proto"
)
type server struct {
}
func NewServer() proto.CommServerServer {
ser := &server{}
return ser
}
func (s *server) Get(
cxt context.Context,
req *proto.GetRequest,
) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Set(
ctx context.Context,
req *proto.SetRequest,
) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Add(ctx context.Context, request *proto.AddRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Reduce(ctx context.Context, request *proto.ReduceRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Setbit(ctx context.Context, request *proto.SetbitRequest) (*proto.CommendResponse, error) {
panic("implement me")
}
func (s *server) Getbit(ctx context.Context, request *proto.GetbitRequest) (*proto.CommendResponse, error) {
panic("implement me")
}

View File

@ -0,0 +1,260 @@
// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package server
import (
context "context"
"gitee.com/timedb/wheatCache/pkg/errorx"
"gitee.com/timedb/wheatCache/pkg/event"
"gitee.com/timedb/wheatCache/pkg/proto"
"gitee.com/timedb/wheatCache/pkg/structure"
"gitee.com/timedb/wheatCache/pkg/structure/stringx"
"time"
)
type serverSingle struct {
middleProduce event.ProduceInterface
lruProduce event.ProduceInterface
ttl time.Duration
}
func NewServer() proto.CommServerServer {
ser := &serverSingle{}
return ser
}
// TODO 移除
func mockLruValue() structure.KeyBaseInterface {
return stringx.NewStringSingle()
}
func (s *serverSingle) Set(
cxt context.Context,
req *proto.SetRequest,
) (*proto.SetResponse, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.StringXInterface:
resp, err := value.(structure.StringXInterface).Set(req)
if err != nil {
return nil, err
}
return resp, nil
default:
return nil, errorx.New("value err")
}
})
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
switch resp.(type) {
case *proto.SetResponse:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.SetResponse), nil
}
func (s *serverSingle) Get(
cxt context.Context,
req *proto.GetRequest,
) (*proto.GetResponse, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.StringXInterface:
resp, err := value.(structure.StringXInterface).Get(req)
if err != nil {
return nil, err
}
return resp, nil
default:
return nil, errorx.New("value err")
}
})
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
switch resp.(type) {
case *proto.GetResponse:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.GetResponse), nil
}
func (s *serverSingle) Add(
cxt context.Context,
req *proto.AddRequest,
) (*proto.AddResponse, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.StringXInterface:
resp, err := value.(structure.StringXInterface).Add(req)
if err != nil {
return nil, err
}
return resp, nil
default:
return nil, errorx.New("value err")
}
})
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
switch resp.(type) {
case *proto.AddResponse:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.AddResponse), nil
}
func (s *serverSingle) Reduce(
cxt context.Context,
req *proto.ReduceRequest,
) (*proto.ReduceResponse, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.StringXInterface:
resp, err := value.(structure.StringXInterface).Reduce(req)
if err != nil {
return nil, err
}
return resp, nil
default:
return nil, errorx.New("value err")
}
})
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
switch resp.(type) {
case *proto.ReduceResponse:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.ReduceResponse), nil
}
func (s *serverSingle) Setbit(
cxt context.Context,
req *proto.SetbitRequest,
) (*proto.SetbitResponse, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.StringXInterface:
resp, err := value.(structure.StringXInterface).Setbit(req)
if err != nil {
return nil, err
}
return resp, nil
default:
return nil, errorx.New("value err")
}
})
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
switch resp.(type) {
case *proto.SetbitResponse:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.SetbitResponse), nil
}
func (s *serverSingle) Getbit(
cxt context.Context,
req *proto.GetbitRequest,
) (*proto.GetbitResponse, error) {
lruEvent := event.NewEvent("lru event")
lruEvent.InitWaitEvent()
work := event.EventWorkFunc(func() (interface{}, error) {
value := mockLruValue() // TODO 替换为从 lru 获取
switch value.(type) {
case structure.StringXInterface:
resp, err := value.(structure.StringXInterface).Getbit(req)
if err != nil {
return nil, err
}
return resp, nil
default:
return nil, errorx.New("value err")
}
})
lruEvent.SetValue("lru work", work)
s.lruProduce.Call(cxt, lruEvent)
resp, err := lruEvent.StartWaitEvent(s.ttl)
if err != nil {
return nil, err
}
switch resp.(type) {
case *proto.GetbitResponse:
default:
return nil, errorx.New("value err")
}
return resp.(*proto.GetbitResponse), nil
}