From c1ca0a499f642816913f6aa30d0ef12b92728f27 Mon Sep 17 00:00:00 2001 From: bandl <1658002533@qq.com> Date: Sun, 19 Sep 2021 10:37:30 +0800 Subject: [PATCH] feat(structure): add gen-tools --- conf/public_conf.go | 2 +- makefile | 5 +- pkg/proto/stringx.pb.go | 110 ++++++++++++------------ pkg/structure/generate/const.template | 34 ++++++++ pkg/structure/generate/structure.gen.go | 75 +++++++++++----- pkg/structure/generate/tem.yaml | 2 + pkg/structure/interface.go | 6 +- protobuf/storage.proto | 17 +--- protobuf/stringx.proto | 18 ++-- shell/gen_protobuf.py | 1 + shell/make-struct.py | 90 ++++++++++--------- 11 files changed, 217 insertions(+), 143 deletions(-) create mode 100644 pkg/structure/generate/const.template create mode 100644 shell/gen_protobuf.py diff --git a/conf/public_conf.go b/conf/public_conf.go index ff0aadd..088f4ea 100644 --- a/conf/public_conf.go +++ b/conf/public_conf.go @@ -29,7 +29,7 @@ func init() { func setDefaultConfValue() { // 设置一些默认值 - viper.Set("version", "base-01") + viper.SetDefault("version", "base-01") } func LoadConf(path string) error { diff --git a/makefile b/makefile index c98cc26..93c6893 100644 --- a/makefile +++ b/makefile @@ -20,4 +20,7 @@ dev: .PHONY: gen-struct gen-struct: @python3 ./shell/make-struct.py - @gofmt -w $(BASE_PATH)/structure/generate/structure.gen.go \ No newline at end of file + +.PHONY: gen-protobuf +gen-protobuf: + @python3 ./shell/gen_protobuf.py \ No newline at end of file diff --git a/pkg/proto/stringx.pb.go b/pkg/proto/stringx.pb.go index c963904..475b34b 100644 --- a/pkg/proto/stringx.pb.go +++ b/pkg/proto/stringx.pb.go @@ -20,7 +20,7 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -type StringXSet struct { +type Set struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields @@ -29,8 +29,8 @@ type StringXSet struct { Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` } -func (x *StringXSet) Reset() { - *x = StringXSet{} +func (x *Set) Reset() { + *x = Set{} if protoimpl.UnsafeEnabled { mi := &file_stringx_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -38,13 +38,13 @@ func (x *StringXSet) Reset() { } } -func (x *StringXSet) String() string { +func (x *Set) String() string { return protoimpl.X.MessageStringOf(x) } -func (*StringXSet) ProtoMessage() {} +func (*Set) ProtoMessage() {} -func (x *StringXSet) ProtoReflect() protoreflect.Message { +func (x *Set) ProtoReflect() protoreflect.Message { mi := &file_stringx_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -56,26 +56,26 @@ func (x *StringXSet) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use StringXSet.ProtoReflect.Descriptor instead. -func (*StringXSet) Descriptor() ([]byte, []int) { +// Deprecated: Use Set.ProtoReflect.Descriptor instead. +func (*Set) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{0} } -func (x *StringXSet) GetKey() *BaseKey { +func (x *Set) GetKey() *BaseKey { if x != nil { return x.Key } return nil } -func (x *StringXSet) GetValue() string { +func (x *Set) GetValue() string { if x != nil { return x.Value } return "" } -type StringxGet struct { +type Get struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields @@ -83,8 +83,8 @@ type StringxGet struct { Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` } -func (x *StringxGet) Reset() { - *x = StringxGet{} +func (x *Get) Reset() { + *x = Get{} if protoimpl.UnsafeEnabled { mi := &file_stringx_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -92,13 +92,13 @@ func (x *StringxGet) Reset() { } } -func (x *StringxGet) String() string { +func (x *Get) String() string { return protoimpl.X.MessageStringOf(x) } -func (*StringxGet) ProtoMessage() {} +func (*Get) ProtoMessage() {} -func (x *StringxGet) ProtoReflect() protoreflect.Message { +func (x *Get) ProtoReflect() protoreflect.Message { mi := &file_stringx_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -110,12 +110,12 @@ func (x *StringxGet) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use StringxGet.ProtoReflect.Descriptor instead. -func (*StringxGet) Descriptor() ([]byte, []int) { +// Deprecated: Use Get.ProtoReflect.Descriptor instead. +func (*Get) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{1} } -func (x *StringxGet) GetKey() *BaseKey { +func (x *Get) GetKey() *BaseKey { if x != nil { return x.Key } @@ -127,10 +127,10 @@ type StringxComm struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // Types that are assignable to StringXComm: + // Types that are assignable to Comm: // *StringxComm_Get // *StringxComm_Set - StringXComm isStringxComm_StringXComm `protobuf_oneof:"string_x_comm"` + Comm isStringxComm_Comm `protobuf_oneof:"comm"` } func (x *StringxComm) Reset() { @@ -165,62 +165,60 @@ func (*StringxComm) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{2} } -func (m *StringxComm) GetStringXComm() isStringxComm_StringXComm { +func (m *StringxComm) GetComm() isStringxComm_Comm { if m != nil { - return m.StringXComm + return m.Comm } return nil } -func (x *StringxComm) GetGet() *StringXSet { - if x, ok := x.GetStringXComm().(*StringxComm_Get); ok { +func (x *StringxComm) GetGet() *Set { + if x, ok := x.GetComm().(*StringxComm_Get); ok { return x.Get } return nil } -func (x *StringxComm) GetSet() *StringxGet { - if x, ok := x.GetStringXComm().(*StringxComm_Set); ok { +func (x *StringxComm) GetSet() *Get { + if x, ok := x.GetComm().(*StringxComm_Set); ok { return x.Set } return nil } -type isStringxComm_StringXComm interface { - isStringxComm_StringXComm() +type isStringxComm_Comm interface { + isStringxComm_Comm() } type StringxComm_Get struct { - Get *StringXSet `protobuf:"bytes,1,opt,name=get,proto3,oneof"` + Get *Set `protobuf:"bytes,1,opt,name=get,proto3,oneof"` } type StringxComm_Set struct { - Set *StringxGet `protobuf:"bytes,2,opt,name=set,proto3,oneof"` + Set *Get `protobuf:"bytes,2,opt,name=set,proto3,oneof"` } -func (*StringxComm_Get) isStringxComm_StringXComm() {} +func (*StringxComm_Get) isStringxComm_Comm() {} -func (*StringxComm_Set) isStringxComm_StringXComm() {} +func (*StringxComm_Set) isStringxComm_Comm() {} var File_stringx_proto protoreflect.FileDescriptor var file_stringx_proto_rawDesc = []byte{ 0x0a, 0x0d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, - 0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x3e, 0x0a, 0x0a, 0x53, - 0x74, 0x72, 0x69, 0x6e, 0x67, 0x58, 0x53, 0x65, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, - 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x28, 0x0a, 0x0a, 0x53, - 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, 0x47, 0x65, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, - 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x60, 0x0a, 0x0b, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, - 0x43, 0x6f, 0x6d, 0x6d, 0x12, 0x1f, 0x0a, 0x03, 0x67, 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x0b, 0x32, 0x0b, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x58, 0x53, 0x65, 0x74, 0x48, 0x00, - 0x52, 0x03, 0x67, 0x65, 0x74, 0x12, 0x1f, 0x0a, 0x03, 0x73, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, 0x47, 0x65, 0x74, 0x48, - 0x00, 0x52, 0x03, 0x73, 0x65, 0x74, 0x42, 0x0f, 0x0a, 0x0d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, - 0x5f, 0x78, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x0a, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x37, 0x0a, 0x03, 0x53, + 0x65, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, + 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, + 0x61, 0x6c, 0x75, 0x65, 0x22, 0x21, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x1a, 0x0a, 0x03, 0x6b, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x08, 0x2e, 0x42, 0x61, 0x73, 0x65, 0x4b, + 0x65, 0x79, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x49, 0x0a, 0x0b, 0x53, 0x74, 0x72, 0x69, 0x6e, + 0x67, 0x78, 0x43, 0x6f, 0x6d, 0x6d, 0x12, 0x18, 0x0a, 0x03, 0x67, 0x65, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x04, 0x2e, 0x53, 0x65, 0x74, 0x48, 0x00, 0x52, 0x03, 0x67, 0x65, 0x74, + 0x12, 0x18, 0x0a, 0x03, 0x73, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x04, 0x2e, + 0x47, 0x65, 0x74, 0x48, 0x00, 0x52, 0x03, 0x73, 0x65, 0x74, 0x42, 0x06, 0x0a, 0x04, 0x63, 0x6f, + 0x6d, 0x6d, 0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -237,16 +235,16 @@ func file_stringx_proto_rawDescGZIP() []byte { var file_stringx_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_stringx_proto_goTypes = []interface{}{ - (*StringXSet)(nil), // 0: StringXSet - (*StringxGet)(nil), // 1: StringxGet + (*Set)(nil), // 0: Set + (*Get)(nil), // 1: Get (*StringxComm)(nil), // 2: StringxComm (*BaseKey)(nil), // 3: BaseKey } var file_stringx_proto_depIdxs = []int32{ - 3, // 0: StringXSet.key:type_name -> BaseKey - 3, // 1: StringxGet.key:type_name -> BaseKey - 0, // 2: StringxComm.get:type_name -> StringXSet - 1, // 3: StringxComm.set:type_name -> StringxGet + 3, // 0: Set.key:type_name -> BaseKey + 3, // 1: Get.key:type_name -> BaseKey + 0, // 2: StringxComm.get:type_name -> Set + 1, // 3: StringxComm.set:type_name -> Get 4, // [4:4] is the sub-list for method output_type 4, // [4:4] is the sub-list for method input_type 4, // [4:4] is the sub-list for extension type_name @@ -262,7 +260,7 @@ func file_stringx_proto_init() { file_base_proto_init() if !protoimpl.UnsafeEnabled { file_stringx_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*StringXSet); i { + switch v := v.(*Set); i { case 0: return &v.state case 1: @@ -274,7 +272,7 @@ func file_stringx_proto_init() { } } file_stringx_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*StringxGet); i { + switch v := v.(*Get); i { case 0: return &v.state case 1: diff --git a/pkg/structure/generate/const.template b/pkg/structure/generate/const.template new file mode 100644 index 0000000..8fa6f67 --- /dev/null +++ b/pkg/structure/generate/const.template @@ -0,0 +1,34 @@ +// Code generated by gen-struct. DO NOT EDIT. +// make gen-struct generated + +package generate + +const ( + DEFAULT_KEY = iota + {% for key in keys %} + {{key}} + {% endfor %} +) + +const ( + DEFAULT_COMM = iota + {% for key in sets %} + {{key}} + {% endfor %} +) + +var CommKeyString = map[string]int { +{% for kmp in key_maps %} + {% for comm in kmp.val %} + "{{comm}}": {{kmp.key}}, + {% endfor %} +{% endfor %} +} + +var CommKey = map[int]int { +{% for kmp in key_maps %} + {% for comm in kmp.upper %} + {{comm}}: {{kmp.key}}, + {% endfor %} +{% endfor %} +} \ No newline at end of file diff --git a/pkg/structure/generate/structure.gen.go b/pkg/structure/generate/structure.gen.go index 214098f..d9c8589 100644 --- a/pkg/structure/generate/structure.gen.go +++ b/pkg/structure/generate/structure.gen.go @@ -5,37 +5,72 @@ package generate const ( DEFAULT_KEY = iota - + STRING_X + + LIST_X + ) const ( - DEFAULT_COMM = iota - - SET_COMM - - GET_COMM - - ADD_COMM - - REDUCE_COMM - - SETBIT_COMM - - GETBIT_COMM + DEFAULT_COMM = iota + + SET + + GET + + ADD + + REDUCE + + SETBIT + + GETBIT + + L_SET + ) -var CommKey = map[string]int{ +var CommKeyString = map[string]int { + "set": STRING_X, - + "get": STRING_X, - + "add": STRING_X, - + "reduce": STRING_X, - + "setbit": STRING_X, - + "getbit": STRING_X, + + + + "l_set": LIST_X, + + } + +var CommKey = map[int]int { + + + SET: STRING_X, + + GET: STRING_X, + + ADD: STRING_X, + + REDUCE: STRING_X, + + SETBIT: STRING_X, + + GETBIT: STRING_X, + + + + L_SET: LIST_X, + + +} \ No newline at end of file diff --git a/pkg/structure/generate/tem.yaml b/pkg/structure/generate/tem.yaml index 945712e..81cb823 100644 --- a/pkg/structure/generate/tem.yaml +++ b/pkg/structure/generate/tem.yaml @@ -8,3 +8,5 @@ STRING_X: - setbit - getbit +LIST_X: + - l_set \ No newline at end of file diff --git a/pkg/structure/interface.go b/pkg/structure/interface.go index 0cffbde..ad79e4c 100644 --- a/pkg/structure/interface.go +++ b/pkg/structure/interface.go @@ -4,9 +4,7 @@ type CacheValue interface { LengthByte() int64 } -type ParseComm func(comm interface{}) ([]string, error) - -type CacheStruct interface { +type KeyBase interface { SizeByte() int64 // TODO RollBack 事务相关, V2 实现 @@ -16,7 +14,5 @@ type CacheStruct interface { // Comment 事务相关, V2 实现 Comment() error - ParseCommend(comm ParseComm) ([]string, error) - Encode() ([]byte, error) } diff --git a/protobuf/storage.proto b/protobuf/storage.proto index 45b4e2b..440914c 100644 --- a/protobuf/storage.proto +++ b/protobuf/storage.proto @@ -3,22 +3,13 @@ syntax = "proto3"; option go_package = "pkg/proto"; import "stringx.proto"; -message Comm { - oneof comm_option { - StringxComm string_comm = 1; - } -} - -message CommendRequest { - Comm commends = 1; -} - message CommendResponse { repeated string result = 1; } service CommServer { - rpc Commend (CommendRequest) returns (CommendResponse); -} - + rpc Get (GetRequest) returns (CommendResponse); + rpc Set (SetRequest) returns (CommendResponse); + rpc Add () +} \ No newline at end of file diff --git a/protobuf/stringx.proto b/protobuf/stringx.proto index edf4ebc..edf3c43 100644 --- a/protobuf/stringx.proto +++ b/protobuf/stringx.proto @@ -5,18 +5,20 @@ import "base.proto"; option go_package = "pkg/proto"; -message StringXSet { +message SetRequest { BaseKey key = 1; string value = 2; } -message StringxGet { +message GetRequest { BaseKey key = 1; } -message StringxComm { - oneof string_x_comm { - StringXSet get = 1; - StringxGet set = 2; - } -} \ No newline at end of file +message AddRequest { + BaseKey key = 1; + int64 value = 2; +} + +message RedceRequest { + BaseKey key = 1; +} diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py new file mode 100644 index 0000000..0aba17d --- /dev/null +++ b/shell/gen_protobuf.py @@ -0,0 +1 @@ +import os \ No newline at end of file diff --git a/shell/make-struct.py b/shell/make-struct.py index 3a9f981..b03c8b2 100644 --- a/shell/make-struct.py +++ b/shell/make-struct.py @@ -1,72 +1,84 @@ import os +from typing import Dict, List from jinja2 import Template import yaml sysPath = os.getcwd() - -tem_text = '''// Code generated by gen-struct. DO NOT EDIT. -// make gen-struct generated - -package generate - -const ( - DEFAULT_KEY = iota - {% for key in keys %} - {{key}} - {% endfor %} -) - -const ( - DEFAULT_COMM = iota - {% for key in sets %} - {{key}} - {% endfor %} -) - -var CommKey = map[string]int { -{% for kmp in key_maps %} - {% for comm in kmp.val %} - "{{comm}}": {{kmp.key}}, - {% endfor %} -{% endfor %} -} -''' +tempPath = f"{sysPath}/pkg/structure/generate" +structurePath = f"{sysPath}/pkg/structure" +protobufPath = f"{sysPath}/protobuf" class KeyMap(object): - def __init__(self, key, val) -> None: + def __init__(self, key: str, val) -> None: self.key = key self.val = val + self.upper = [v.upper() for v in val] + + +def to_camel(val: str) -> str: + return "".join([k.capitalize() for k in val.split('_')]) + + +def load_template(name: str) -> str: + with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp: + return fp.read() def load_conf(): - conf_path = f"{sysPath}/structure/generate/tem.yaml" + conf_path = f"{tempPath}/tem.yaml" with open(conf_path, 'r', encoding='utf-8') as f: cfg = f.read() cfg = yaml.load(cfg) - return cfg + cfg_camel = {} + + for key, val in cfg.items(): + cfg_camel[key] = [to_camel(v) for v in val] + + print(cfg_camel) + + return cfg, cfg_camel -def set_structure_template(conf: dict): +# 生成常量 +def set_structure_const_template(conf: dict): + tem_text = load_template("const.template") keys = conf.keys() key_map = [] val_set = [] for k, v in conf.items(): key_map.append(KeyMap(key=k, val=v)) - for val in v: - val_set.append(val.upper() + "_COMM") + for val in v: + val_set.append(val.upper()) template = Template(tem_text) text = template.render(keys=keys, key_maps=key_map, sets=val_set) - - temp_path = f"{sysPath}/structure/generate/structure.gen.go" + text.replace("\n\n", "\n") + + temp_path = f"{tempPath}/structure.gen.go" with open(temp_path, 'w', encoding='utf-8') as f: f.write(text) - + +# 生成接口 + + +def set_structure_interface(conf): + print(conf) + + # class KeyInterFace + + with open(f"{structurePath}/interface.go") as fp: + interfaceText = fp.read() + + for key, item in conf.items(): + interfaceKey = "".join([k.capitalize() for k in key.split('_')]) + interfaceTitle = "type " + if __name__ == "__main__": - conf = load_conf() - set_structure_template(conf) + conf, cfg_camel = load_conf() + set_structure_const_template(conf) + set_structure_interface(conf)