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/storage.pb.go b/pkg/proto/storage.pb.go index 8fce5ea..219e2dc 100644 --- a/pkg/proto/storage.pb.go +++ b/pkg/proto/storage.pb.go @@ -24,119 +24,6 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -type Comm struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - // Types that are assignable to CommOption: - // *Comm_StringComm - CommOption isComm_CommOption `protobuf_oneof:"comm_option"` -} - -func (x *Comm) Reset() { - *x = Comm{} - if protoimpl.UnsafeEnabled { - mi := &file_storage_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Comm) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Comm) ProtoMessage() {} - -func (x *Comm) ProtoReflect() protoreflect.Message { - mi := &file_storage_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Comm.ProtoReflect.Descriptor instead. -func (*Comm) Descriptor() ([]byte, []int) { - return file_storage_proto_rawDescGZIP(), []int{0} -} - -func (m *Comm) GetCommOption() isComm_CommOption { - if m != nil { - return m.CommOption - } - return nil -} - -func (x *Comm) GetStringComm() *StringxComm { - if x, ok := x.GetCommOption().(*Comm_StringComm); ok { - return x.StringComm - } - return nil -} - -type isComm_CommOption interface { - isComm_CommOption() -} - -type Comm_StringComm struct { - StringComm *StringxComm `protobuf:"bytes,1,opt,name=string_comm,json=stringComm,proto3,oneof"` -} - -func (*Comm_StringComm) isComm_CommOption() {} - -type CommendRequest struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Commends *Comm `protobuf:"bytes,1,opt,name=commends,proto3" json:"commends,omitempty"` -} - -func (x *CommendRequest) Reset() { - *x = CommendRequest{} - if protoimpl.UnsafeEnabled { - mi := &file_storage_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *CommendRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*CommendRequest) ProtoMessage() {} - -func (x *CommendRequest) ProtoReflect() protoreflect.Message { - mi := &file_storage_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use CommendRequest.ProtoReflect.Descriptor instead. -func (*CommendRequest) Descriptor() ([]byte, []int) { - return file_storage_proto_rawDescGZIP(), []int{1} -} - -func (x *CommendRequest) GetCommends() *Comm { - if x != nil { - return x.Commends - } - return nil -} - type CommendResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -148,7 +35,7 @@ type CommendResponse struct { func (x *CommendResponse) Reset() { *x = CommendResponse{} if protoimpl.UnsafeEnabled { - mi := &file_storage_proto_msgTypes[2] + mi := &file_storage_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -161,7 +48,7 @@ func (x *CommendResponse) String() string { func (*CommendResponse) ProtoMessage() {} func (x *CommendResponse) ProtoReflect() protoreflect.Message { - mi := &file_storage_proto_msgTypes[2] + mi := &file_storage_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -174,7 +61,7 @@ func (x *CommendResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use CommendResponse.ProtoReflect.Descriptor instead. func (*CommendResponse) Descriptor() ([]byte, []int) { - return file_storage_proto_rawDescGZIP(), []int{2} + return file_storage_proto_rawDescGZIP(), []int{0} } func (x *CommendResponse) GetResult() []string { @@ -188,23 +75,17 @@ var File_storage_proto protoreflect.FileDescriptor var file_storage_proto_rawDesc = []byte{ 0x0a, 0x0d, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, - 0x0d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x46, - 0x0a, 0x04, 0x43, 0x6f, 0x6d, 0x6d, 0x12, 0x2f, 0x0a, 0x0b, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, - 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x53, 0x74, - 0x72, 0x69, 0x6e, 0x67, 0x78, 0x43, 0x6f, 0x6d, 0x6d, 0x48, 0x00, 0x52, 0x0a, 0x73, 0x74, 0x72, - 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6d, 0x6d, 0x42, 0x0d, 0x0a, 0x0b, 0x63, 0x6f, 0x6d, 0x6d, 0x5f, - 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x33, 0x0a, 0x0e, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, - 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x21, 0x0a, 0x08, 0x63, 0x6f, 0x6d, 0x6d, - 0x65, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x05, 0x2e, 0x43, 0x6f, 0x6d, - 0x6d, 0x52, 0x08, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x73, 0x22, 0x29, 0x0a, 0x0f, 0x43, - 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, - 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, - 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x3a, 0x0a, 0x0a, 0x43, 0x6f, 0x6d, 0x6d, 0x53, 0x65, - 0x72, 0x76, 0x65, 0x72, 0x12, 0x2c, 0x0a, 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x12, - 0x0f, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x10, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x0d, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x78, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x29, + 0x0a, 0x0f, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x03, 0x28, + 0x09, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x58, 0x0a, 0x0a, 0x43, 0x6f, 0x6d, + 0x6d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x24, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x0b, + 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x43, 0x6f, + 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, + 0x03, 0x53, 0x65, 0x74, 0x12, 0x0b, 0x2e, 0x53, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x10, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -219,23 +100,22 @@ func file_storage_proto_rawDescGZIP() []byte { return file_storage_proto_rawDescData } -var file_storage_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_storage_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_storage_proto_goTypes = []interface{}{ - (*Comm)(nil), // 0: Comm - (*CommendRequest)(nil), // 1: CommendRequest - (*CommendResponse)(nil), // 2: CommendResponse - (*StringxComm)(nil), // 3: StringxComm + (*CommendResponse)(nil), // 0: CommendResponse + (*GetRequest)(nil), // 1: GetRequest + (*SetRequest)(nil), // 2: SetRequest } var file_storage_proto_depIdxs = []int32{ - 3, // 0: Comm.string_comm:type_name -> StringxComm - 0, // 1: CommendRequest.commends:type_name -> Comm - 1, // 2: CommServer.Commend:input_type -> CommendRequest - 2, // 3: CommServer.Commend:output_type -> CommendResponse - 3, // [3:4] is the sub-list for method output_type - 2, // [2:3] is the sub-list for method input_type - 2, // [2:2] is the sub-list for extension type_name - 2, // [2:2] is the sub-list for extension extendee - 0, // [0:2] is the sub-list for field type_name + 1, // 0: CommServer.Get:input_type -> GetRequest + 2, // 1: CommServer.Set:input_type -> SetRequest + 0, // 2: CommServer.Get:output_type -> CommendResponse + 0, // 3: CommServer.Set:output_type -> CommendResponse + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name } func init() { file_storage_proto_init() } @@ -246,30 +126,6 @@ func file_storage_proto_init() { file_stringx_proto_init() if !protoimpl.UnsafeEnabled { file_storage_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Comm); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_storage_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*CommendRequest); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_storage_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*CommendResponse); i { case 0: return &v.state @@ -282,16 +138,13 @@ func file_storage_proto_init() { } } } - file_storage_proto_msgTypes[0].OneofWrappers = []interface{}{ - (*Comm_StringComm)(nil), - } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_storage_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 1, NumExtensions: 0, NumServices: 1, }, @@ -317,7 +170,8 @@ const _ = grpc.SupportPackageIsVersion6 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type CommServerClient interface { - Commend(ctx context.Context, in *CommendRequest, opts ...grpc.CallOption) (*CommendResponse, error) + Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*CommendResponse, error) + Set(ctx context.Context, in *SetRequest, opts ...grpc.CallOption) (*CommendResponse, error) } type commServerClient struct { @@ -328,9 +182,18 @@ func NewCommServerClient(cc grpc.ClientConnInterface) CommServerClient { return &commServerClient{cc} } -func (c *commServerClient) Commend(ctx context.Context, in *CommendRequest, opts ...grpc.CallOption) (*CommendResponse, error) { +func (c *commServerClient) Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*CommendResponse, error) { out := new(CommendResponse) - err := c.cc.Invoke(ctx, "/CommServer/Commend", in, out, opts...) + err := c.cc.Invoke(ctx, "/CommServer/Get", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *commServerClient) Set(ctx context.Context, in *SetRequest, opts ...grpc.CallOption) (*CommendResponse, error) { + out := new(CommendResponse) + err := c.cc.Invoke(ctx, "/CommServer/Set", in, out, opts...) if err != nil { return nil, err } @@ -339,35 +202,57 @@ func (c *commServerClient) Commend(ctx context.Context, in *CommendRequest, opts // CommServerServer is the server API for CommServer service. type CommServerServer interface { - Commend(context.Context, *CommendRequest) (*CommendResponse, error) + Get(context.Context, *GetRequest) (*CommendResponse, error) + Set(context.Context, *SetRequest) (*CommendResponse, error) } // UnimplementedCommServerServer can be embedded to have forward compatible implementations. type UnimplementedCommServerServer struct { } -func (*UnimplementedCommServerServer) Commend(context.Context, *CommendRequest) (*CommendResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method Commend not implemented") +func (*UnimplementedCommServerServer) Get(context.Context, *GetRequest) (*CommendResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Get not implemented") +} +func (*UnimplementedCommServerServer) Set(context.Context, *SetRequest) (*CommendResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Set not implemented") } func RegisterCommServerServer(s *grpc.Server, srv CommServerServer) { s.RegisterService(&_CommServer_serviceDesc, srv) } -func _CommServer_Commend_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(CommendRequest) +func _CommServer_Get_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(CommServerServer).Commend(ctx, in) + return srv.(CommServerServer).Get(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/CommServer/Commend", + FullMethod: "/CommServer/Get", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(CommServerServer).Commend(ctx, req.(*CommendRequest)) + return srv.(CommServerServer).Get(ctx, req.(*GetRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _CommServer_Set_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SetRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CommServerServer).Set(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/CommServer/Set", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CommServerServer).Set(ctx, req.(*SetRequest)) } return interceptor(ctx, in, info, handler) } @@ -377,8 +262,12 @@ var _CommServer_serviceDesc = grpc.ServiceDesc{ HandlerType: (*CommServerServer)(nil), Methods: []grpc.MethodDesc{ { - MethodName: "Commend", - Handler: _CommServer_Commend_Handler, + MethodName: "Get", + Handler: _CommServer_Get_Handler, + }, + { + MethodName: "Set", + Handler: _CommServer_Set_Handler, }, }, Streams: []grpc.StreamDesc{}, diff --git a/pkg/proto/stringx.pb.go b/pkg/proto/stringx.pb.go index c963904..b9dad31 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 SetRequest 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 *SetRequest) Reset() { + *x = SetRequest{} 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 *SetRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*StringXSet) ProtoMessage() {} +func (*SetRequest) ProtoMessage() {} -func (x *StringXSet) ProtoReflect() protoreflect.Message { +func (x *SetRequest) 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 SetRequest.ProtoReflect.Descriptor instead. +func (*SetRequest) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{0} } -func (x *StringXSet) GetKey() *BaseKey { +func (x *SetRequest) GetKey() *BaseKey { if x != nil { return x.Key } return nil } -func (x *StringXSet) GetValue() string { +func (x *SetRequest) GetValue() string { if x != nil { return x.Value } return "" } -type StringxGet struct { +type GetRequest 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 *GetRequest) Reset() { + *x = GetRequest{} 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 *GetRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*StringxGet) ProtoMessage() {} +func (*GetRequest) ProtoMessage() {} -func (x *StringxGet) ProtoReflect() protoreflect.Message { +func (x *GetRequest) ProtoReflect() protoreflect.Message { mi := &file_stringx_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -110,31 +110,29 @@ func (x *StringxGet) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use StringxGet.ProtoReflect.Descriptor instead. -func (*StringxGet) Descriptor() ([]byte, []int) { +// Deprecated: Use GetRequest.ProtoReflect.Descriptor instead. +func (*GetRequest) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{1} } -func (x *StringxGet) GetKey() *BaseKey { +func (x *GetRequest) GetKey() *BaseKey { if x != nil { return x.Key } return nil } -type StringxComm struct { +type AddRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // Types that are assignable to StringXComm: - // *StringxComm_Get - // *StringxComm_Set - StringXComm isStringxComm_StringXComm `protobuf_oneof:"string_x_comm"` + Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Value int64 `protobuf:"varint,2,opt,name=value,proto3" json:"value,omitempty"` } -func (x *StringxComm) Reset() { - *x = StringxComm{} +func (x *AddRequest) Reset() { + *x = AddRequest{} if protoimpl.UnsafeEnabled { mi := &file_stringx_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -142,13 +140,13 @@ func (x *StringxComm) Reset() { } } -func (x *StringxComm) String() string { +func (x *AddRequest) String() string { return protoimpl.X.MessageStringOf(x) } -func (*StringxComm) ProtoMessage() {} +func (*AddRequest) ProtoMessage() {} -func (x *StringxComm) ProtoReflect() protoreflect.Message { +func (x *AddRequest) ProtoReflect() protoreflect.Message { mi := &file_stringx_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -160,67 +158,92 @@ func (x *StringxComm) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use StringxComm.ProtoReflect.Descriptor instead. -func (*StringxComm) Descriptor() ([]byte, []int) { +// Deprecated: Use AddRequest.ProtoReflect.Descriptor instead. +func (*AddRequest) Descriptor() ([]byte, []int) { return file_stringx_proto_rawDescGZIP(), []int{2} } -func (m *StringxComm) GetStringXComm() isStringxComm_StringXComm { - if m != nil { - return m.StringXComm +func (x *AddRequest) GetKey() *BaseKey { + if x != nil { + return x.Key } return nil } -func (x *StringxComm) GetGet() *StringXSet { - if x, ok := x.GetStringXComm().(*StringxComm_Get); ok { - return x.Get +func (x *AddRequest) GetValue() int64 { + if x != nil { + return x.Value + } + return 0 +} + +type RedceRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key *BaseKey `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *RedceRequest) Reset() { + *x = RedceRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_stringx_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RedceRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RedceRequest) ProtoMessage() {} + +func (x *RedceRequest) ProtoReflect() protoreflect.Message { + mi := &file_stringx_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RedceRequest.ProtoReflect.Descriptor instead. +func (*RedceRequest) Descriptor() ([]byte, []int) { + return file_stringx_proto_rawDescGZIP(), []int{3} +} + +func (x *RedceRequest) GetKey() *BaseKey { + if x != nil { + return x.Key } return nil } -func (x *StringxComm) GetSet() *StringxGet { - if x, ok := x.GetStringXComm().(*StringxComm_Set); ok { - return x.Set - } - return nil -} - -type isStringxComm_StringXComm interface { - isStringxComm_StringXComm() -} - -type StringxComm_Get struct { - Get *StringXSet `protobuf:"bytes,1,opt,name=get,proto3,oneof"` -} - -type StringxComm_Set struct { - Set *StringxGet `protobuf:"bytes,2,opt,name=set,proto3,oneof"` -} - -func (*StringxComm_Get) isStringxComm_StringXComm() {} - -func (*StringxComm_Set) isStringxComm_StringXComm() {} - 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, + 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 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, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x28, 0x0a, 0x0a, 0x47, + 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 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, + 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x3e, 0x0a, 0x0a, 0x41, 0x64, 0x64, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 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, 0x03, 0x52, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x2a, 0x0a, 0x0c, 0x52, 0x65, 0x64, 0x63, 0x65, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 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, 0x42, 0x0b, 0x5a, 0x09, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -235,18 +258,19 @@ func file_stringx_proto_rawDescGZIP() []byte { return file_stringx_proto_rawDescData } -var file_stringx_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_stringx_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_stringx_proto_goTypes = []interface{}{ - (*StringXSet)(nil), // 0: StringXSet - (*StringxGet)(nil), // 1: StringxGet - (*StringxComm)(nil), // 2: StringxComm - (*BaseKey)(nil), // 3: BaseKey + (*SetRequest)(nil), // 0: SetRequest + (*GetRequest)(nil), // 1: GetRequest + (*AddRequest)(nil), // 2: AddRequest + (*RedceRequest)(nil), // 3: RedceRequest + (*BaseKey)(nil), // 4: 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 + 4, // 0: SetRequest.key:type_name -> BaseKey + 4, // 1: GetRequest.key:type_name -> BaseKey + 4, // 2: AddRequest.key:type_name -> BaseKey + 4, // 3: RedceRequest.key:type_name -> BaseKey 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 +286,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.(*SetRequest); i { case 0: return &v.state case 1: @@ -274,7 +298,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.(*GetRequest); i { case 0: return &v.state case 1: @@ -286,7 +310,19 @@ func file_stringx_proto_init() { } } file_stringx_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*StringxComm); i { + switch v := v.(*AddRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_stringx_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RedceRequest); i { case 0: return &v.state case 1: @@ -298,17 +334,13 @@ func file_stringx_proto_init() { } } } - file_stringx_proto_msgTypes[2].OneofWrappers = []interface{}{ - (*StringxComm_Get)(nil), - (*StringxComm_Set)(nil), - } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_stringx_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 4, NumExtensions: 0, NumServices: 0, }, diff --git a/pkg/structure/define.go b/pkg/structure/define.go index 53b61f7..0f37810 100644 --- a/pkg/structure/define.go +++ b/pkg/structure/define.go @@ -1,15 +1,27 @@ package structure -const ( - STRING_X = iota - LIST_X - SET_X - Z_SET_X -) - -// SetComm 操作描述 -type SetComm struct { - Typ int - Opt string - Params []string +type KeyBase struct { +} + +func (k *KeyBase) SizeByte() int64 { + panic("not implemented") // TODO: Implement +} + +// TODO RollBack 事务相关, V2 实现 +func (k *KeyBase) RollBack() error { + panic("not implemented") // TODO: Implement +} + +// Begin 事务相关, V2 实现 +func (k *KeyBase) Begin() error { + panic("not implemented") // TODO: Implement +} + +// Comment 事务相关, V2 实现 +func (k *KeyBase) Comment() error { + panic("not implemented") // TODO: Implement +} + +func (k *KeyBase) Encode() ([]byte, error) { + panic("not implemented") // TODO: Implement } 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..95156c6 100644 --- a/pkg/structure/generate/tem.yaml +++ b/pkg/structure/generate/tem.yaml @@ -8,3 +8,10 @@ STRING_X: - setbit - getbit +LIST_X: + - l_set + - l_get + - l_add + - l_queue + - l_setbit + - l_getbit diff --git a/pkg/structure/interface.go b/pkg/structure/interface.go index 0cffbde..1e69347 100644 --- a/pkg/structure/interface.go +++ b/pkg/structure/interface.go @@ -1,12 +1,12 @@ package structure +import "gitee.com/timedb/wheatCache/pkg/proto" + type CacheValue interface { LengthByte() int64 } -type ParseComm func(comm interface{}) ([]string, error) - -type CacheStruct interface { +type KeyBaseInterface interface { SizeByte() int64 // TODO RollBack 事务相关, V2 实现 @@ -16,7 +16,16 @@ type CacheStruct interface { // Comment 事务相关, V2 实现 Comment() error - ParseCommend(comm ParseComm) ([]string, 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) +} diff --git a/pkg/structure/stringx/option.go b/pkg/structure/stringx/option.go index 653ddc2..f6c7490 100644 --- a/pkg/structure/stringx/option.go +++ b/pkg/structure/stringx/option.go @@ -1 +1,15 @@ package stringx + +import "gitee.com/timedb/wheatCache/pkg/proto" + +func (s *StringX) Set(req *proto.SetRequest) ([]string, error) { + s.Value = req.Value + return nil, nil +} + +func (s *StringX) Get(req *proto.GetRequest) ([]string, error) { + result := make([]string, 0, 1) + result = append(result, s.Value) + + return result, nil +} diff --git a/pkg/structure/stringx/string.go b/pkg/structure/stringx/string.go index 653ddc2..a5f4e71 100644 --- a/pkg/structure/stringx/string.go +++ b/pkg/structure/stringx/string.go @@ -1 +1,8 @@ package stringx + +import "gitee.com/timedb/wheatCache/pkg/structure" + +type StringX struct { + structure.KeyBase + Value string +} diff --git a/protobuf/listx.proto b/protobuf/listx.proto new file mode 100644 index 0000000..cc2733b --- /dev/null +++ b/protobuf/listx.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +import "base.proto"; +option go_package = "pkg/proto"; + +message LSetRequest { + BaseKey key = 1; +} +message LGetRequest { + BaseKey key = 1; +} +message LAddRequest { + BaseKey key = 1; +} +message LQueueRequest { + BaseKey key = 1; +} +message LSetbitRequest { + BaseKey key = 1; +} +message LGetbitRequest { + BaseKey key = 1; +} \ No newline at end of file diff --git a/protobuf/storage.proto b/protobuf/storage.proto index 45b4e2b..b1f024e 100644 --- a/protobuf/storage.proto +++ b/protobuf/storage.proto @@ -3,22 +3,11 @@ 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); +} \ No newline at end of file diff --git a/protobuf/stringx.proto b/protobuf/stringx.proto index edf4ebc..9499847 100644 --- a/protobuf/stringx.proto +++ b/protobuf/stringx.proto @@ -5,18 +5,27 @@ 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; - } +message AddRequest { + BaseKey key = 1; + int64 value = 2; +} + +message ReduceRequest { + BaseKey key = 1; +} + +message SetbitRequest { + BaseKey key = 1; +} +message GetbitRequest { + BaseKey key = 1; } \ No newline at end of file diff --git a/shell/gen_protobuf.py b/shell/gen_protobuf.py new file mode 100644 index 0000000..077e013 --- /dev/null +++ b/shell/gen_protobuf.py @@ -0,0 +1,76 @@ +import os +from typing import Dict, List +from jinja2 import Template +import yaml + +sysPath = os.getcwd() +tempPath = f"{sysPath}/pkg/structure/generate" +structurePath = f"{sysPath}/pkg/structure" +protobufPath = f"{sysPath}/protobuf" + +def to_camel(val: str) -> str: + return "".join([k.capitalize() for k in val.split('_')]) + +def load_conf(): + conf_path = f"{tempPath}/tem.yaml" + with open(conf_path, 'r', encoding='utf-8') as f: + cfg = f.read() + + cfg = yaml.load(cfg) + + cfg_camel = {} + + for key, val in cfg.items(): + key = to_camel(key).lower() + cfg_camel[key] = [to_camel(v) for v in val] + +# print(cfg_camel) + + return cfg, cfg_camel + + +# 生成文件,并写入 +def mkdir(cfg_camel): + path = "protobuf" + path = path.strip() + # 判断路径是否存在 + isExists = os.path.exists(path) + # 判断结果 + if not isExists: + os.makedirs(path) + print(path + ' 创建成功') + else: # 如果目录存在则不创建,并提示目录已存在 + # print(path + ' 目录已存在') + for key,value in cfg_camel.items(): +# print(key) + proto_path = path + '/' + key + '.proto' + if not os.path.exists(proto_path): # 如果这个文件不存在 + file = open(proto_path,'w') + file.write('syntax = "proto3";\nimport "base.proto";\noption go_package = "pkg/proto";\n') + for v in value: + file.write('\nmessage ' + v +'Request '+'{\n\tBaseKey key = 1;\n}') + file.close() + else: # 如果这个文件存在 + with open(proto_path) as f: + line = f.readlines() +# print(line) + f.close() + for v in value: + function = v + 'Request' + flag = 0 + for l in line: + if function in l: + flag=1 + break + if flag == 0: + file = open(proto_path,'a') + file.write('\nmessage ' + v +'Request '+'{\n\tBaseKey key = 1;\n}') + file.close() + flag = 0 + + +if __name__ == "__main__": + conf, cfg_camel = load_conf() +# + mkdir(cfg_camel) + diff --git a/shell/gen_struct_interface.py b/shell/gen_struct_interface.py new file mode 100644 index 0000000..4e22bfd --- /dev/null +++ b/shell/gen_struct_interface.py @@ -0,0 +1,109 @@ +import os + + +def DealList(li: list): + # 对列表进行处理 + flag = 0 + InterName = [] + BlankNum = [] + SliceNum = [] + for i in range(len(li)): + if flag == 0: + SliceNum.append(i) + InterName.append(f"{li[i].lower().title()}Interface".replace("_", "").replace(":", "")) + flag = 1 + if li[i] == '': + BlankNum.append(i) + flag = 0 + Dict = {} + for i in range(len(InterName)-1): + Dict[InterName[i]] = li[SliceNum[i]+1:BlankNum[i]] + Dict[InterName[-1]] = li[BlankNum[-1]+2:] + return Dict + + +def SetInterface(): + # 对读取到的yaml进行处理 + Temp = [] + path = os.getcwd() + WirtePath = f"{path}/pkg/structure/interface.go" + ReadPath = f"{path}/pkg/structure/generate/tem.yaml" + with open(ReadPath, "r") as f: + title = f.read() + title = title.split("\n")[2:] + for i in title: + if ' - ' not in i: + Temp.append(i) + if ' - ' in i: + i = i.split("-")[1] + Temp.append(i) + + # 对列表进行处理,便于自动补全 + dic = DealList(Temp) + + # 通过传入的列表对interface.go进行自动补全 + Temp = [] + AppendList = [] + with open(WirtePath, "r") as f: + context = f.read() + context = context.split("\n") + flag = 0 + for i in context: + if "interface" in i: + flag = 1 # 开锁 + if "}" in i: + Temp.append(i) + AppendList.append(Temp) + Temp = [] + flag = 0 # 上锁 + if flag == 1: + Temp.append(i) + + # 拆分成功, 开始匹配 + Del = [] + res = context[0:20] + for i in AppendList[2:]: + x = i[0:2] + for key, values in dic.items(): + if key in i[0]: + Del.append(key) + for val in values: + x.append(f'\t{val.title().lstrip()}(*pr' + f'oto.{val.title().lstrip()}Request) ([]string, error)'.replace("_", "")) + x.append("\n}\n\n") + for j in x: + res.append(j) + for i in Del: + dic.pop(i) + # 添加新增的 + for key, values in dic.items(): + w = [] + w.append(f"type {key} interface" + "{") + w.append('\tKeyBaseInterface') + for val in values: + w.append(f'\t{val.title().lstrip()}(*pr' + f'oto.{val.title().lstrip()}Request) ([]string, error)'.replace("_", "")) + w.append("\n}\n\n") + for j in w: + res.append(j) + f = open(WirtePath, 'w') + judge = "" + for i in res: + if judge == i: + continue + judge = i + if "package" in i: + i = f"{i}\n" + if "import" in i: + i = f"{i}\n" + if i == '': + i = '\n' + if i == '}' or i == '\n{\n': + i = '\n}\n' + i = i.replace('\t', '\n\t') + f.write(i) + f.close() + + +if __name__ == "__main__": + SetInterface() \ 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) diff --git a/storage/server/server.go b/storage/server/server.go index 485d858..0265082 100644 --- a/storage/server/server.go +++ b/storage/server/server.go @@ -2,19 +2,59 @@ package server import ( context "context" + "errors" + "gitee.com/timedb/wheatCache/pkg/proto" + "gitee.com/timedb/wheatCache/pkg/structure/stringx" ) -type server struct{} +type server struct { +} func NewServer() proto.CommServerServer { ser := &server{} return ser } -func (s *server) Commend( - ctx context.Context, - req *proto.CommendRequest, +func (s *server) Get( + cxt context.Context, + req *proto.GetRequest, ) (*proto.CommendResponse, error) { - return nil, nil + + var value interface{} + + switch value.(type) { + case *stringx.StringX: + default: + return nil, errors.New("awdwd") + } + + result, err := value.(*stringx.StringX).Get(req) + if err != nil { + return nil, err + } + return &proto.CommendResponse{ + Result: result, + }, nil +} + +func (s *server) Set( + ctx context.Context, + req *proto.SetRequest, +) (*proto.CommendResponse, error) { + var value interface{} + + switch value.(type) { + case *stringx.StringX: + default: + return nil, errors.New("awdwd") + } + + result, err := value.(*stringx.StringX).Set(req) + if err != nil { + return nil, err + } + return &proto.CommendResponse{ + Result: result, + }, nil }