!23 自动生成interface脚本

Merge pull request !23 from K-on/feat-update-tamplate
This commit is contained in:
bandl 2021-09-21 02:14:41 +00:00 committed by Gitee
commit 23baebe2d7
6 changed files with 58 additions and 71 deletions

View File

@ -17,8 +17,8 @@ install:
dev: dev:
@./bin/storage storage @./bin/storage storage
.PHONY: gen-struct .PHONY: gen-struct-const
gen-struct: gen-struct-const:
@python3 ./shell/make-struct.py @python3 ./shell/make-struct.py
.PHONY: gen-protobuf .PHONY: gen-protobuf

View File

@ -1,34 +1,34 @@
// Code generated by gen-struct. DO NOT EDIT. // Code generated by gen-struct-const. DO NOT EDIT.
// make gen-struct generated // make gen-struct generated
package generate package generate
const ( const (
DEFAULT_KEY = iota DEFAULT_KEY = iota
{% for key in keys %} {% for key in keys -%}
{{key}} {{key}}
{% endfor %} {%- endfor %}
) )
const ( const (
DEFAULT_COMM = iota DEFAULT_COMM = iota
{% for key in sets %} {% for key in sets -%}
{{key}} {{key}}
{% endfor %} {% endfor %}
) )
var CommKeyString = map[string]int { var CommKeyString = map[string]int {
{% for kmp in key_maps %} {%- for kmp in key_maps -%}
{% for comm in kmp.val %} {% for comm in kmp.val -%}
"{{comm}}": {{kmp.key}}, "{{comm}}": {{kmp.key}},
{% endfor %} {% endfor -%}
{% endfor %} {% endfor %}
} }
var CommKey = map[int]int { var CommKey = map[int]int {
{% for kmp in key_maps %} {%- for kmp in key_maps -%}
{% for comm in kmp.upper %} {% for comm in kmp.upper -%}
{{comm}}: {{kmp.key}}, {{comm}}: {{kmp.key}},
{% endfor %} {% endfor -%}
{% endfor %} {% endfor %}
} }

View File

@ -0,0 +1,28 @@
package structure
import "gitee.com/timedb/wheatCache/pkg/proto"
type KeyBaseInterface interface {
SizeByte() int64
// TODO RollBack 事务相关, V2 实现
RollBack() error
// Begin 事务相关, V2 实现
Begin() error
// Comment 事务相关, V2 实现
Comment() error
Encode() ([]byte, error)
}
type StringXInterface interface {
KeyBaseInterface
Set(*proto.SetRequest) ([]string, error)
Get(*proto.GetRequest) ([]string, error)
}
type ListXInterface interface {
KeyBaseInterface
LSet(*proto.SetRequest) ([]string, error)
}

View File

@ -1,76 +1,38 @@
// Code generated by gen-struct. DO NOT EDIT. // Code generated by gen-struct-const. DO NOT EDIT.
// make gen-struct generated // make gen-struct generated
package generate package generate
const ( const (
DEFAULT_KEY = iota DEFAULT_KEY = iota
STRING_X STRING_X
LIST_X
) )
const ( const (
DEFAULT_COMM = iota DEFAULT_COMM = iota
SET
SET
GET GET
ADD ADD
REDUCE REDUCE
SETBIT SETBIT
GETBIT GETBIT
L_SET
) )
var CommKeyString = map[string]int { var CommKeyString = map[string]int {"set": STRING_X,
"set": STRING_X,
"get": STRING_X, "get": STRING_X,
"add": STRING_X, "add": STRING_X,
"reduce": STRING_X, "reduce": STRING_X,
"setbit": STRING_X, "setbit": STRING_X,
"getbit": STRING_X, "getbit": STRING_X,
"l_set": LIST_X,
} }
var CommKey = map[int]int { var CommKey = map[int]int {SET: STRING_X,
SET: STRING_X,
GET: STRING_X, GET: STRING_X,
ADD: STRING_X, ADD: STRING_X,
REDUCE: STRING_X, REDUCE: STRING_X,
SETBIT: STRING_X, SETBIT: STRING_X,
GETBIT: STRING_X, GETBIT: STRING_X,
L_SET: LIST_X,
} }

View File

@ -2,10 +2,6 @@ package structure
import "gitee.com/timedb/wheatCache/pkg/proto" import "gitee.com/timedb/wheatCache/pkg/proto"
type CacheValue interface {
LengthByte() int64
}
type KeyBaseInterface interface { type KeyBaseInterface interface {
SizeByte() int64 SizeByte() int64

View File

@ -56,7 +56,6 @@ def set_structure_const_template(conf: dict):
template = Template(tem_text) template = Template(tem_text)
text = template.render(keys=keys, key_maps=key_map, sets=val_set) text = template.render(keys=keys, key_maps=key_map, sets=val_set)
text.replace("\n\n", "\n")
temp_path = f"{tempPath}/structure.gen.go" temp_path = f"{tempPath}/structure.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f: with open(temp_path, 'w', encoding='utf-8') as f:
@ -66,19 +65,21 @@ def set_structure_const_template(conf: dict):
def set_structure_interface(conf): def set_structure_interface(conf):
print(conf) text = load_template("interface.template")
Dic = {}
# class KeyInterFace template = Template(text)
for i in conf.keys():
with open(f"{structurePath}/interface.go") as fp: x = i.lower().title()
interfaceText = fp.read() Dic["".join(x.split("_"))] = conf[i]
yamlData = [Dic]
for key, item in conf.items(): text = template.render(Data=yamlData)
interfaceKey = "".join([k.capitalize() for k in key.split('_')]) temp_path = f"{structurePath}/interface.gen.go"
interfaceTitle = "type " with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
if __name__ == "__main__": if __name__ == "__main__":
conf, cfg_camel = load_conf() conf, cfg_camel = load_conf()
set_structure_const_template(conf) # set_structure_const_template(conf)
set_structure_interface(conf) # set_structure_interface(conf)
set_structure_interface(cfg_camel)