forked from p53841790/wheat-cache
feat(middle): Automated code generation
This commit is contained in:
parent
26f3f9f663
commit
488d84580e
3
makefile
3
makefile
|
@ -26,3 +26,6 @@ gen-struct:
|
|||
.PHONY: gen-protobuf
|
||||
gen-protobuf:
|
||||
@python3 ./shell/gen_protobuf.py
|
||||
|
||||
gen-middleware:
|
||||
@python3 ./shell/gen_middleware.py
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// Code generated by gen-middleware. DO NOT EDIT.
|
||||
// make gen-middleware generated
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"gitee.com/timedb/wheatCache/middleware"
|
||||
|
||||
logMiddle "gitee.com/timedb/wheatCache/middleware/log-middle"
|
||||
mapKey "gitee.com/timedb/wheatCache/middleware/map-key"
|
||||
)
|
||||
|
||||
|
||||
func GetMiddlewareMap() map[string]middleware.MiddlewareInterface {
|
||||
|
||||
return map[string]middleware.MiddlewareInterface{
|
||||
|
||||
"logMiddle":logMiddle.NewMiddleware(),
|
||||
"mapKey":mapKey.NewMiddleware(),
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
// Code generated by gen-middleware. DO NOT EDIT.
|
||||
// make gen-middleware generated
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"gitee.com/timedb/wheatCache/middleware"
|
||||
{%for dir in dirs %}
|
||||
{{dir[0]}} "gitee.com/timedb/wheatCache/middleware/{{dir[1]}}"
|
||||
{%- endfor%}
|
||||
)
|
||||
|
||||
|
||||
func GetMiddlewareMap() map[string]middleware.MiddlewareInterface {
|
||||
|
||||
return map[string]middleware.MiddlewareInterface{
|
||||
{%for dir in dirs %}
|
||||
"{{dir[0]}}":{{dir[0]}}.NewMiddleware(),
|
||||
{%- endfor%}
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"gitee.com/timedb/wheatCache/middleware"
|
||||
logMiddle "gitee.com/timedb/wheatCache/middleware/log-middle"
|
||||
)
|
||||
|
||||
func GetMiddlewareMap() map[string]middleware.MiddlewareInterface {
|
||||
logMiddlewareCase := logMiddle.NewMiddleware()
|
||||
|
||||
return map[string]middleware.MiddlewareInterface{
|
||||
"logMiddlewareCase": logMiddlewareCase,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package log_middle
|
||||
|
||||
func wait() {
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package log_middle
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitee.com/timedb/wheatCache/middleware"
|
||||
)
|
||||
|
||||
|
@ -9,7 +10,6 @@ type A struct {
|
|||
}
|
||||
|
||||
func (i *A) Init() {
|
||||
|
||||
}
|
||||
|
||||
func (i *A) Exat(interface{}) (interface{}, error) {
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package log_middle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitee.com/timedb/wheatCache/middleware"
|
||||
)
|
||||
|
||||
type ABB struct {
|
||||
}
|
||||
|
||||
func (i *ABB) Init() {
|
||||
}
|
||||
|
||||
func (i *ABB) Exat(interface{}) (interface{}, error) {
|
||||
fmt.Println(1)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func NewMiddleware() middleware.MiddlewareInterface {
|
||||
return &ABB{}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
import os
|
||||
from jinja2 import Template
|
||||
|
||||
|
||||
sysPath = os.getcwd()
|
||||
tempPath = f"{sysPath}/middleware/config"
|
||||
structurePath = f"{sysPath}/pkg/structure"
|
||||
protobufPath = f"{sysPath}/protobuf"
|
||||
storagePath = f"{sysPath}/storage"
|
||||
middlePath = f"{sysPath}/middleware"
|
||||
|
||||
def go_fmt(path: str):
|
||||
os.system(f"go fmt {path}")
|
||||
|
||||
|
||||
def to_camel(val: str) -> str:
|
||||
camel =""
|
||||
val = val.split('_')
|
||||
for k in range(0,len(val),1):
|
||||
if k == 0:
|
||||
camel = camel+str(val[k])
|
||||
else:
|
||||
camel = camel+str(val[k].capitalize())
|
||||
return camel
|
||||
|
||||
# 得到各个中间件的文件名
|
||||
def getMiddleName(path:str)->list:
|
||||
dic = []
|
||||
dataname = os.listdir(path)
|
||||
|
||||
for i in dataname :
|
||||
if i != 'config' and i != 'define.go':
|
||||
dic.append(i)
|
||||
return dic
|
||||
|
||||
#读取模板
|
||||
def load_template(name: str) -> str:
|
||||
with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp:
|
||||
return fp.read()
|
||||
|
||||
# 获取各个中间件的目录
|
||||
def getMiddleDir(keys:list)->list:
|
||||
dirs = []
|
||||
middleware = []
|
||||
for middlename in keys:
|
||||
middleware=[]
|
||||
name = to_camel(middlename.replace("-","_"))
|
||||
dir = middlename
|
||||
middleware.append(name)
|
||||
middleware.append(dir)
|
||||
dirs.append(middleware)
|
||||
return dirs
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
tem_text = load_template("middle.template")
|
||||
temp = Template(tem_text)
|
||||
keys = getMiddleName(middlePath)
|
||||
dirs = getMiddleDir(keys)
|
||||
text = temp.render(dirs=dirs,)
|
||||
temp_path = f"{middlePath}/config/middle.gen.go"
|
||||
with open(temp_path,'w',encoding='utf-8') as f:
|
||||
f.write(text)
|
||||
|
||||
|
Loading…
Reference in New Issue