!40 中间件自动化生成代码
Merge pull request !40 from Sodesnei/feat-middleware-python
This commit is contained in:
commit
04cb2a1ed1
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,
|
||||
}
|
||||
}
|
|
@ -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{}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package middle
|
||||
|
||||
import middleConf "gitee.com/timedb/wheatCache/middleware/config"
|
||||
import getMiddlewareMap "gitee.com/timedb/wheatCache/middleware/config"
|
||||
|
||||
func init() {
|
||||
middleConf.GetMiddlewareMap()
|
||||
func Init() {
|
||||
getMiddlewareMap.GetMiddlewareMap()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
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
|
||||
|
||||
def format_code_go():
|
||||
go_fmt(f"{middlePath}/config/middle.gen.go")
|
||||
|
||||
def set_middle_server():
|
||||
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
set_middle_server()
|
||||
format_code_go()
|
||||
|
||||
|
Loading…
Reference in New Issue