From b060867009994dbf4bdb2cc7b0850b6e9779e4a5 Mon Sep 17 00:00:00 2001 From: songmeizi Date: Tue, 8 Dec 2020 22:36:48 +0800 Subject: [PATCH] Feature bookstore update (#253) * update bookstore * update bookstore --- example/bookstore/api/bookstore.api | 46 ++- .../api/internal/handler/addhandler.go | 2 +- .../api/internal/handler/checkhandler.go | 2 +- .../bookstore/api/internal/handler/routes.go | 24 +- .../api/internal/logic/checklogic.go | 3 +- example/bookstore/go.mod | 1 + example/bookstore/rpc/add/add.go | 2 +- example/bookstore/rpc/add/add/add.pb.go | 305 +++++++++++++++++ example/bookstore/rpc/add/adder/adder.go | 6 +- example/bookstore/rpc/add/adder/adder_mock.go | 49 --- .../rpc/add/internal/config/config.go | 1 - .../rpc/add/internal/logic/addlogic.go | 2 +- .../bookstore/rpc/add/internal/pb/add.pb.go | 167 ---------- .../rpc/add/internal/server/adderserver.go | 2 +- .../rpc/add/internal/svc/servicecontext.go | 4 +- example/bookstore/rpc/check/check.go | 2 +- example/bookstore/rpc/check/check/check.pb.go | 306 ++++++++++++++++++ .../bookstore/rpc/check/checker/checker.go | 6 +- .../rpc/check/checker/checker_mock.go | 49 --- .../rpc/check/internal/config/config.go | 1 - .../rpc/check/internal/logic/checklogic.go | 2 +- .../rpc/check/internal/pb/check.pb.go | 167 ---------- .../check/internal/server/checkerserver.go | 2 +- .../rpc/check/internal/svc/servicecontext.go | 4 +- example/bookstore/rpc/model/bookmodel.go | 53 ++- example/bookstore/rpc/model/vars.go | 0 26 files changed, 702 insertions(+), 506 deletions(-) create mode 100644 example/bookstore/rpc/add/add/add.pb.go delete mode 100644 example/bookstore/rpc/add/adder/adder_mock.go delete mode 100644 example/bookstore/rpc/add/internal/pb/add.pb.go create mode 100644 example/bookstore/rpc/check/check/check.pb.go delete mode 100644 example/bookstore/rpc/check/checker/checker_mock.go delete mode 100644 example/bookstore/rpc/check/internal/pb/check.pb.go mode change 100755 => 100644 example/bookstore/rpc/model/vars.go diff --git a/example/bookstore/api/bookstore.api b/example/bookstore/api/bookstore.api index 009f4531..31764359 100644 --- a/example/bookstore/api/bookstore.api +++ b/example/bookstore/api/bookstore.api @@ -1,33 +1,29 @@ type ( - addReq struct { - book string `form:"book"` - price int64 `form:"price"` - } - - addResp struct { - ok bool `json:"ok"` - } + addReq { + book string `form:"book"` + price int64 `form:"price"` + } + + addResp { + ok bool `json:"ok"` + } ) type ( - checkReq struct { - book string `form:"book"` - } - - checkResp struct { - found bool `json:"found"` - price int64 `json:"price"` - } + checkReq { + book string `form:"book"` + } + + checkResp { + found bool `json:"found"` + price int64 `json:"price"` + } ) service bookstore-api { - @server( - handler: AddHandler - ) - get /add (addReq) returns (addResp) - - @server( - handler: CheckHandler - ) - get /check (checkReq) returns (checkResp) + @handler AddHandler + get /add (addReq) returns (addResp) + + @handler CheckHandler + get /check (checkReq) returns (checkResp) } \ No newline at end of file diff --git a/example/bookstore/api/internal/handler/addhandler.go b/example/bookstore/api/internal/handler/addhandler.go index 4a002f43..5c02e7dc 100644 --- a/example/bookstore/api/internal/handler/addhandler.go +++ b/example/bookstore/api/internal/handler/addhandler.go @@ -10,7 +10,7 @@ import ( "github.com/tal-tech/go-zero/rest/httpx" ) -func addHandler(ctx *svc.ServiceContext) http.HandlerFunc { +func AddHandler(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.AddReq if err := httpx.Parse(r, &req); err != nil { diff --git a/example/bookstore/api/internal/handler/checkhandler.go b/example/bookstore/api/internal/handler/checkhandler.go index 80d3273b..35130b55 100644 --- a/example/bookstore/api/internal/handler/checkhandler.go +++ b/example/bookstore/api/internal/handler/checkhandler.go @@ -10,7 +10,7 @@ import ( "github.com/tal-tech/go-zero/rest/httpx" ) -func checkHandler(ctx *svc.ServiceContext) http.HandlerFunc { +func CheckHandler(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.CheckReq if err := httpx.Parse(r, &req); err != nil { diff --git a/example/bookstore/api/internal/handler/routes.go b/example/bookstore/api/internal/handler/routes.go index 85950536..5f618068 100644 --- a/example/bookstore/api/internal/handler/routes.go +++ b/example/bookstore/api/internal/handler/routes.go @@ -10,16 +10,18 @@ import ( ) func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) { - engine.AddRoutes([]rest.Route{ - { - Method: http.MethodGet, - Path: "/add", - Handler: addHandler(serverCtx), + engine.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/add", + Handler: AddHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/check", + Handler: CheckHandler(serverCtx), + }, }, - { - Method: http.MethodGet, - Path: "/check", - Handler: checkHandler(serverCtx), - }, - }) + ) } diff --git a/example/bookstore/api/internal/logic/checklogic.go b/example/bookstore/api/internal/logic/checklogic.go index 638b2e21..9563a228 100644 --- a/example/bookstore/api/internal/logic/checklogic.go +++ b/example/bookstore/api/internal/logic/checklogic.go @@ -29,7 +29,8 @@ func (l *CheckLogic) Check(req types.CheckReq) (*types.CheckResp, error) { Book: req.Book, }) if err != nil { - return nil, err + logx.Error(err) + return &types.CheckResp{}, err } return &types.CheckResp{ diff --git a/example/bookstore/go.mod b/example/bookstore/go.mod index 2fba785c..6ce1004b 100644 --- a/example/bookstore/go.mod +++ b/example/bookstore/go.mod @@ -8,4 +8,5 @@ require ( github.com/tal-tech/go-zero v1.0.27 golang.org/x/net v0.0.0-20200707034311-ab3426394381 google.golang.org/grpc v1.29.1 + google.golang.org/protobuf v1.25.0 ) diff --git a/example/bookstore/rpc/add/add.go b/example/bookstore/rpc/add/add.go index 94b12dc3..18ea96bb 100755 --- a/example/bookstore/rpc/add/add.go +++ b/example/bookstore/rpc/add/add.go @@ -7,8 +7,8 @@ import ( "flag" "fmt" + "bookstore/rpc/add/add" "bookstore/rpc/add/internal/config" - add "bookstore/rpc/add/internal/pb" "bookstore/rpc/add/internal/server" "bookstore/rpc/add/internal/svc" diff --git a/example/bookstore/rpc/add/add/add.pb.go b/example/bookstore/rpc/add/add/add.pb.go new file mode 100644 index 00000000..ffd8d1b5 --- /dev/null +++ b/example/bookstore/rpc/add/add/add.pb.go @@ -0,0 +1,305 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.25.0 +// protoc v3.14.0 +// source: add.proto + +package add + +import ( + context "context" + proto "github.com/golang/protobuf/proto" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +type AddReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Book string `protobuf:"bytes,1,opt,name=book,proto3" json:"book,omitempty"` + Price int64 `protobuf:"varint,2,opt,name=price,proto3" json:"price,omitempty"` +} + +func (x *AddReq) Reset() { + *x = AddReq{} + if protoimpl.UnsafeEnabled { + mi := &file_add_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AddReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddReq) ProtoMessage() {} + +func (x *AddReq) ProtoReflect() protoreflect.Message { + mi := &file_add_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 AddReq.ProtoReflect.Descriptor instead. +func (*AddReq) Descriptor() ([]byte, []int) { + return file_add_proto_rawDescGZIP(), []int{0} +} + +func (x *AddReq) GetBook() string { + if x != nil { + return x.Book + } + return "" +} + +func (x *AddReq) GetPrice() int64 { + if x != nil { + return x.Price + } + return 0 +} + +type AddResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ok bool `protobuf:"varint,1,opt,name=ok,proto3" json:"ok,omitempty"` +} + +func (x *AddResp) Reset() { + *x = AddResp{} + if protoimpl.UnsafeEnabled { + mi := &file_add_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AddResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddResp) ProtoMessage() {} + +func (x *AddResp) ProtoReflect() protoreflect.Message { + mi := &file_add_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 AddResp.ProtoReflect.Descriptor instead. +func (*AddResp) Descriptor() ([]byte, []int) { + return file_add_proto_rawDescGZIP(), []int{1} +} + +func (x *AddResp) GetOk() bool { + if x != nil { + return x.Ok + } + return false +} + +var File_add_proto protoreflect.FileDescriptor + +var file_add_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x61, 0x64, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x61, 0x64, 0x64, + 0x22, 0x32, 0x0a, 0x06, 0x61, 0x64, 0x64, 0x52, 0x65, 0x71, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, + 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x6f, 0x6b, 0x12, 0x14, + 0x0a, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x70, + 0x72, 0x69, 0x63, 0x65, 0x22, 0x19, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x52, 0x65, 0x73, 0x70, 0x12, + 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x32, + 0x29, 0x0a, 0x05, 0x61, 0x64, 0x64, 0x65, 0x72, 0x12, 0x20, 0x0a, 0x03, 0x61, 0x64, 0x64, 0x12, + 0x0b, 0x2e, 0x61, 0x64, 0x64, 0x2e, 0x61, 0x64, 0x64, 0x52, 0x65, 0x71, 0x1a, 0x0c, 0x2e, 0x61, + 0x64, 0x64, 0x2e, 0x61, 0x64, 0x64, 0x52, 0x65, 0x73, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_add_proto_rawDescOnce sync.Once + file_add_proto_rawDescData = file_add_proto_rawDesc +) + +func file_add_proto_rawDescGZIP() []byte { + file_add_proto_rawDescOnce.Do(func() { + file_add_proto_rawDescData = protoimpl.X.CompressGZIP(file_add_proto_rawDescData) + }) + return file_add_proto_rawDescData +} + +var file_add_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_add_proto_goTypes = []interface{}{ + (*AddReq)(nil), // 0: add.addReq + (*AddResp)(nil), // 1: add.addResp +} +var file_add_proto_depIdxs = []int32{ + 0, // 0: add.adder.add:input_type -> add.addReq + 1, // 1: add.adder.add:output_type -> add.addResp + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] 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_add_proto_init() } +func file_add_proto_init() { + if File_add_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_add_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*AddReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_add_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*AddResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_add_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_add_proto_goTypes, + DependencyIndexes: file_add_proto_depIdxs, + MessageInfos: file_add_proto_msgTypes, + }.Build() + File_add_proto = out.File + file_add_proto_rawDesc = nil + file_add_proto_goTypes = nil + file_add_proto_depIdxs = nil +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// AdderClient is the client API for Adder service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type AdderClient interface { + Add(ctx context.Context, in *AddReq, opts ...grpc.CallOption) (*AddResp, error) +} + +type adderClient struct { + cc grpc.ClientConnInterface +} + +func NewAdderClient(cc grpc.ClientConnInterface) AdderClient { + return &adderClient{cc} +} + +func (c *adderClient) Add(ctx context.Context, in *AddReq, opts ...grpc.CallOption) (*AddResp, error) { + out := new(AddResp) + err := c.cc.Invoke(ctx, "/add.adder/add", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// AdderServer is the server API for Adder service. +type AdderServer interface { + Add(context.Context, *AddReq) (*AddResp, error) +} + +// UnimplementedAdderServer can be embedded to have forward compatible implementations. +type UnimplementedAdderServer struct { +} + +func (*UnimplementedAdderServer) Add(context.Context, *AddReq) (*AddResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method Add not implemented") +} + +func RegisterAdderServer(s *grpc.Server, srv AdderServer) { + s.RegisterService(&_Adder_serviceDesc, srv) +} + +func _Adder_Add_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AddReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AdderServer).Add(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/add.adder/Add", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AdderServer).Add(ctx, req.(*AddReq)) + } + return interceptor(ctx, in, info, handler) +} + +var _Adder_serviceDesc = grpc.ServiceDesc{ + ServiceName: "add.adder", + HandlerType: (*AdderServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "add", + Handler: _Adder_Add_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "add.proto", +} diff --git a/example/bookstore/rpc/add/adder/adder.go b/example/bookstore/rpc/add/adder/adder.go index 78a9daa8..be75b708 100755 --- a/example/bookstore/rpc/add/adder/adder.go +++ b/example/bookstore/rpc/add/adder/adder.go @@ -8,7 +8,7 @@ package adder import ( "context" - add "bookstore/rpc/add/internal/pb" + "bookstore/rpc/add/add" "github.com/tal-tech/go-zero/zrpc" ) @@ -33,6 +33,6 @@ func NewAdder(cli zrpc.Client) Adder { } func (m *defaultAdder) Add(ctx context.Context, in *AddReq) (*AddResp, error) { - adder := add.NewAdderClient(m.cli.Conn()) - return adder.Add(ctx, in) + client := add.NewAdderClient(m.cli.Conn()) + return client.Add(ctx, in) } diff --git a/example/bookstore/rpc/add/adder/adder_mock.go b/example/bookstore/rpc/add/adder/adder_mock.go deleted file mode 100644 index 401228d0..00000000 --- a/example/bookstore/rpc/add/adder/adder_mock.go +++ /dev/null @@ -1,49 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: adder.go - -// Package adder is a generated GoMock package. -package adder - -import ( - context "context" - gomock "github.com/golang/mock/gomock" - reflect "reflect" -) - -// MockAdder is a mock of Adder interface -type MockAdder struct { - ctrl *gomock.Controller - recorder *MockAdderMockRecorder -} - -// MockAdderMockRecorder is the mock recorder for MockAdder -type MockAdderMockRecorder struct { - mock *MockAdder -} - -// NewMockAdder creates a new mock instance -func NewMockAdder(ctrl *gomock.Controller) *MockAdder { - mock := &MockAdder{ctrl: ctrl} - mock.recorder = &MockAdderMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockAdder) EXPECT() *MockAdderMockRecorder { - return m.recorder -} - -// Add mocks base method -func (m *MockAdder) Add(ctx context.Context, in *AddReq) (*AddResp, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Add", ctx, in) - ret0, _ := ret[0].(*AddResp) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Add indicates an expected call of Add -func (mr *MockAdderMockRecorder) Add(ctx, in interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockAdder)(nil).Add), ctx, in) -} diff --git a/example/bookstore/rpc/add/internal/config/config.go b/example/bookstore/rpc/add/internal/config/config.go index 6229ef8c..255f5d02 100755 --- a/example/bookstore/rpc/add/internal/config/config.go +++ b/example/bookstore/rpc/add/internal/config/config.go @@ -8,6 +8,5 @@ import ( type Config struct { zrpc.RpcServerConf DataSource string - Table string Cache cache.CacheConf } diff --git a/example/bookstore/rpc/add/internal/logic/addlogic.go b/example/bookstore/rpc/add/internal/logic/addlogic.go index 49565115..5c34a0b3 100755 --- a/example/bookstore/rpc/add/internal/logic/addlogic.go +++ b/example/bookstore/rpc/add/internal/logic/addlogic.go @@ -3,7 +3,7 @@ package logic import ( "context" - add "bookstore/rpc/add/internal/pb" + add "bookstore/rpc/add/adder" "bookstore/rpc/add/internal/svc" "bookstore/rpc/model" diff --git a/example/bookstore/rpc/add/internal/pb/add.pb.go b/example/bookstore/rpc/add/internal/pb/add.pb.go deleted file mode 100644 index a762ce67..00000000 --- a/example/bookstore/rpc/add/internal/pb/add.pb.go +++ /dev/null @@ -1,167 +0,0 @@ -// Code generated by protoc-gen-go. -// source: add.proto -// DO NOT EDIT! - -/* -Package add is a generated protocol buffer package. - -It is generated from these files: - add.proto - -It has these top-level messages: - AddReq - AddResp -*/ -package add - -import ( - "fmt" - "math" - - "github.com/golang/protobuf/proto" - "golang.org/x/net/context" - "google.golang.org/grpc" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package - -type AddReq struct { - Book string `protobuf:"bytes,1,opt,name=book" json:"book,omitempty"` - Price int64 `protobuf:"varint,2,opt,name=price" json:"price,omitempty"` -} - -func (m *AddReq) Reset() { *m = AddReq{} } -func (m *AddReq) String() string { return proto.CompactTextString(m) } -func (*AddReq) ProtoMessage() {} -func (*AddReq) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } - -func (m *AddReq) GetBook() string { - if m != nil { - return m.Book - } - return "" -} - -func (m *AddReq) GetPrice() int64 { - if m != nil { - return m.Price - } - return 0 -} - -type AddResp struct { - Ok bool `protobuf:"varint,1,opt,name=ok" json:"ok,omitempty"` -} - -func (m *AddResp) Reset() { *m = AddResp{} } -func (m *AddResp) String() string { return proto.CompactTextString(m) } -func (*AddResp) ProtoMessage() {} -func (*AddResp) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } - -func (m *AddResp) GetOk() bool { - if m != nil { - return m.Ok - } - return false -} - -func init() { - proto.RegisterType((*AddReq)(nil), "add.addReq") - proto.RegisterType((*AddResp)(nil), "add.addResp") -} - -// Reference imports to suppress errors if they are not otherwise used. -var _ context.Context -var _ grpc.ClientConn - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -const _ = grpc.SupportPackageIsVersion4 - -// Client API for Adder service - -type AdderClient interface { - Add(ctx context.Context, in *AddReq, opts ...grpc.CallOption) (*AddResp, error) -} - -type adderClient struct { - cc *grpc.ClientConn -} - -func NewAdderClient(cc *grpc.ClientConn) AdderClient { - return &adderClient{cc} -} - -func (c *adderClient) Add(ctx context.Context, in *AddReq, opts ...grpc.CallOption) (*AddResp, error) { - out := new(AddResp) - err := grpc.Invoke(ctx, "/add.adder/add", in, out, c.cc, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// Server API for Adder service - -type AdderServer interface { - Add(context.Context, *AddReq) (*AddResp, error) -} - -func RegisterAdderServer(s *grpc.Server, srv AdderServer) { - s.RegisterService(&_Adder_serviceDesc, srv) -} - -func _Adder_Add_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(AddReq) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(AdderServer).Add(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/add.adder/Add", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(AdderServer).Add(ctx, req.(*AddReq)) - } - return interceptor(ctx, in, info, handler) -} - -var _Adder_serviceDesc = grpc.ServiceDesc{ - ServiceName: "add.adder", - HandlerType: (*AdderServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "add", - Handler: _Adder_Add_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "add.proto", -} - -func init() { proto.RegisterFile("add.proto", fileDescriptor0) } - -var fileDescriptor0 = []byte{ - // 136 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4c, 0x4c, 0x49, 0xd1, - 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4e, 0x4c, 0x49, 0x51, 0x32, 0xe2, 0x62, 0x4b, 0x4c, - 0x49, 0x09, 0x4a, 0x2d, 0x14, 0x12, 0xe2, 0x62, 0x49, 0xca, 0xcf, 0xcf, 0x96, 0x60, 0x54, 0x60, - 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x85, 0x44, 0xb8, 0x58, 0x0b, 0x8a, 0x32, 0x93, 0x53, 0x25, 0x98, - 0x14, 0x18, 0x35, 0x98, 0x83, 0x20, 0x1c, 0x25, 0x49, 0x2e, 0x76, 0xb0, 0x9e, 0xe2, 0x02, 0x21, - 0x3e, 0x2e, 0x26, 0xa8, 0x16, 0x8e, 0x20, 0xa6, 0xfc, 0x6c, 0x23, 0x4d, 0x2e, 0xd6, 0xc4, 0x94, - 0x94, 0xd4, 0x22, 0x21, 0x05, 0x2e, 0x90, 0xf1, 0x42, 0xdc, 0x7a, 0x20, 0xfb, 0x20, 0x36, 0x48, - 0xf1, 0x20, 0x38, 0xc5, 0x05, 0x49, 0x6c, 0x60, 0x57, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, - 0xe2, 0x6d, 0xb5, 0x91, 0x92, 0x00, 0x00, 0x00, -} diff --git a/example/bookstore/rpc/add/internal/server/adderserver.go b/example/bookstore/rpc/add/internal/server/adderserver.go index 12fa9728..fef7497c 100755 --- a/example/bookstore/rpc/add/internal/server/adderserver.go +++ b/example/bookstore/rpc/add/internal/server/adderserver.go @@ -6,8 +6,8 @@ package server import ( "context" + "bookstore/rpc/add/add" "bookstore/rpc/add/internal/logic" - add "bookstore/rpc/add/internal/pb" "bookstore/rpc/add/internal/svc" ) diff --git a/example/bookstore/rpc/add/internal/svc/servicecontext.go b/example/bookstore/rpc/add/internal/svc/servicecontext.go index 32fa9344..730499b5 100755 --- a/example/bookstore/rpc/add/internal/svc/servicecontext.go +++ b/example/bookstore/rpc/add/internal/svc/servicecontext.go @@ -9,12 +9,12 @@ import ( type ServiceContext struct { c config.Config - Model *model.BookModel + Model model.BookModel } func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ c: c, - Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table), + Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache), } } diff --git a/example/bookstore/rpc/check/check.go b/example/bookstore/rpc/check/check.go index 836f2fe9..6ea55525 100755 --- a/example/bookstore/rpc/check/check.go +++ b/example/bookstore/rpc/check/check.go @@ -7,8 +7,8 @@ import ( "flag" "fmt" + "bookstore/rpc/check/check" "bookstore/rpc/check/internal/config" - check "bookstore/rpc/check/internal/pb" "bookstore/rpc/check/internal/server" "bookstore/rpc/check/internal/svc" diff --git a/example/bookstore/rpc/check/check/check.pb.go b/example/bookstore/rpc/check/check/check.pb.go new file mode 100644 index 00000000..eddf2e34 --- /dev/null +++ b/example/bookstore/rpc/check/check/check.pb.go @@ -0,0 +1,306 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.25.0 +// protoc v3.14.0 +// source: check.proto + +package check + +import ( + context "context" + proto "github.com/golang/protobuf/proto" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +type CheckReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Book string `protobuf:"bytes,1,opt,name=book,proto3" json:"book,omitempty"` +} + +func (x *CheckReq) Reset() { + *x = CheckReq{} + if protoimpl.UnsafeEnabled { + mi := &file_check_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CheckReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CheckReq) ProtoMessage() {} + +func (x *CheckReq) ProtoReflect() protoreflect.Message { + mi := &file_check_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 CheckReq.ProtoReflect.Descriptor instead. +func (*CheckReq) Descriptor() ([]byte, []int) { + return file_check_proto_rawDescGZIP(), []int{0} +} + +func (x *CheckReq) GetBook() string { + if x != nil { + return x.Book + } + return "" +} + +type CheckResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Found bool `protobuf:"varint,1,opt,name=found,proto3" json:"found,omitempty"` + Price int64 `protobuf:"varint,2,opt,name=price,proto3" json:"price,omitempty"` +} + +func (x *CheckResp) Reset() { + *x = CheckResp{} + if protoimpl.UnsafeEnabled { + mi := &file_check_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CheckResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CheckResp) ProtoMessage() {} + +func (x *CheckResp) ProtoReflect() protoreflect.Message { + mi := &file_check_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 CheckResp.ProtoReflect.Descriptor instead. +func (*CheckResp) Descriptor() ([]byte, []int) { + return file_check_proto_rawDescGZIP(), []int{1} +} + +func (x *CheckResp) GetFound() bool { + if x != nil { + return x.Found + } + return false +} + +func (x *CheckResp) GetPrice() int64 { + if x != nil { + return x.Price + } + return 0 +} + +var File_check_proto protoreflect.FileDescriptor + +var file_check_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x63, + 0x68, 0x65, 0x63, 0x6b, 0x22, 0x1e, 0x0a, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x71, + 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, + 0x62, 0x6f, 0x6f, 0x6b, 0x22, 0x37, 0x0a, 0x09, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, + 0x70, 0x12, 0x14, 0x0a, 0x05, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x05, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x32, 0x35, 0x0a, + 0x07, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x65, 0x72, 0x12, 0x2a, 0x0a, 0x05, 0x63, 0x68, 0x65, 0x63, + 0x6b, 0x12, 0x0f, 0x2e, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x2e, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x52, + 0x65, 0x71, 0x1a, 0x10, 0x2e, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x2e, 0x63, 0x68, 0x65, 0x63, 0x6b, + 0x52, 0x65, 0x73, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_check_proto_rawDescOnce sync.Once + file_check_proto_rawDescData = file_check_proto_rawDesc +) + +func file_check_proto_rawDescGZIP() []byte { + file_check_proto_rawDescOnce.Do(func() { + file_check_proto_rawDescData = protoimpl.X.CompressGZIP(file_check_proto_rawDescData) + }) + return file_check_proto_rawDescData +} + +var file_check_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_check_proto_goTypes = []interface{}{ + (*CheckReq)(nil), // 0: check.checkReq + (*CheckResp)(nil), // 1: check.checkResp +} +var file_check_proto_depIdxs = []int32{ + 0, // 0: check.checker.check:input_type -> check.checkReq + 1, // 1: check.checker.check:output_type -> check.checkResp + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] 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_check_proto_init() } +func file_check_proto_init() { + if File_check_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_check_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CheckReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_check_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CheckResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_check_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_check_proto_goTypes, + DependencyIndexes: file_check_proto_depIdxs, + MessageInfos: file_check_proto_msgTypes, + }.Build() + File_check_proto = out.File + file_check_proto_rawDesc = nil + file_check_proto_goTypes = nil + file_check_proto_depIdxs = nil +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// CheckerClient is the client API for Checker service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type CheckerClient interface { + Check(ctx context.Context, in *CheckReq, opts ...grpc.CallOption) (*CheckResp, error) +} + +type checkerClient struct { + cc grpc.ClientConnInterface +} + +func NewCheckerClient(cc grpc.ClientConnInterface) CheckerClient { + return &checkerClient{cc} +} + +func (c *checkerClient) Check(ctx context.Context, in *CheckReq, opts ...grpc.CallOption) (*CheckResp, error) { + out := new(CheckResp) + err := c.cc.Invoke(ctx, "/check.checker/check", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// CheckerServer is the server API for Checker service. +type CheckerServer interface { + Check(context.Context, *CheckReq) (*CheckResp, error) +} + +// UnimplementedCheckerServer can be embedded to have forward compatible implementations. +type UnimplementedCheckerServer struct { +} + +func (*UnimplementedCheckerServer) Check(context.Context, *CheckReq) (*CheckResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method Check not implemented") +} + +func RegisterCheckerServer(s *grpc.Server, srv CheckerServer) { + s.RegisterService(&_Checker_serviceDesc, srv) +} + +func _Checker_Check_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CheckReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CheckerServer).Check(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/check.checker/Check", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CheckerServer).Check(ctx, req.(*CheckReq)) + } + return interceptor(ctx, in, info, handler) +} + +var _Checker_serviceDesc = grpc.ServiceDesc{ + ServiceName: "check.checker", + HandlerType: (*CheckerServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "check", + Handler: _Checker_Check_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "check.proto", +} diff --git a/example/bookstore/rpc/check/checker/checker.go b/example/bookstore/rpc/check/checker/checker.go index 8a09712b..a69fd92f 100755 --- a/example/bookstore/rpc/check/checker/checker.go +++ b/example/bookstore/rpc/check/checker/checker.go @@ -8,7 +8,7 @@ package checker import ( "context" - check "bookstore/rpc/check/internal/pb" + "bookstore/rpc/check/check" "github.com/tal-tech/go-zero/zrpc" ) @@ -33,6 +33,6 @@ func NewChecker(cli zrpc.Client) Checker { } func (m *defaultChecker) Check(ctx context.Context, in *CheckReq) (*CheckResp, error) { - checker := check.NewCheckerClient(m.cli.Conn()) - return checker.Check(ctx, in) + client := check.NewCheckerClient(m.cli.Conn()) + return client.Check(ctx, in) } diff --git a/example/bookstore/rpc/check/checker/checker_mock.go b/example/bookstore/rpc/check/checker/checker_mock.go deleted file mode 100644 index cf4429ac..00000000 --- a/example/bookstore/rpc/check/checker/checker_mock.go +++ /dev/null @@ -1,49 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: checker.go - -// Package checker is a generated GoMock package. -package checker - -import ( - context "context" - gomock "github.com/golang/mock/gomock" - reflect "reflect" -) - -// MockChecker is a mock of Checker interface -type MockChecker struct { - ctrl *gomock.Controller - recorder *MockCheckerMockRecorder -} - -// MockCheckerMockRecorder is the mock recorder for MockChecker -type MockCheckerMockRecorder struct { - mock *MockChecker -} - -// NewMockChecker creates a new mock instance -func NewMockChecker(ctrl *gomock.Controller) *MockChecker { - mock := &MockChecker{ctrl: ctrl} - mock.recorder = &MockCheckerMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockChecker) EXPECT() *MockCheckerMockRecorder { - return m.recorder -} - -// Check mocks base method -func (m *MockChecker) Check(ctx context.Context, in *CheckReq) (*CheckResp, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Check", ctx, in) - ret0, _ := ret[0].(*CheckResp) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Check indicates an expected call of Check -func (mr *MockCheckerMockRecorder) Check(ctx, in interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Check", reflect.TypeOf((*MockChecker)(nil).Check), ctx, in) -} diff --git a/example/bookstore/rpc/check/internal/config/config.go b/example/bookstore/rpc/check/internal/config/config.go index 6229ef8c..255f5d02 100755 --- a/example/bookstore/rpc/check/internal/config/config.go +++ b/example/bookstore/rpc/check/internal/config/config.go @@ -8,6 +8,5 @@ import ( type Config struct { zrpc.RpcServerConf DataSource string - Table string Cache cache.CacheConf } diff --git a/example/bookstore/rpc/check/internal/logic/checklogic.go b/example/bookstore/rpc/check/internal/logic/checklogic.go index a314927f..46e1b4b2 100755 --- a/example/bookstore/rpc/check/internal/logic/checklogic.go +++ b/example/bookstore/rpc/check/internal/logic/checklogic.go @@ -3,7 +3,7 @@ package logic import ( "context" - check "bookstore/rpc/check/internal/pb" + check "bookstore/rpc/check/checker" "bookstore/rpc/check/internal/svc" "github.com/tal-tech/go-zero/core/logx" diff --git a/example/bookstore/rpc/check/internal/pb/check.pb.go b/example/bookstore/rpc/check/internal/pb/check.pb.go deleted file mode 100644 index 7cfca79d..00000000 --- a/example/bookstore/rpc/check/internal/pb/check.pb.go +++ /dev/null @@ -1,167 +0,0 @@ -// Code generated by protoc-gen-go. -// source: check.proto -// DO NOT EDIT! - -/* -Package check is a generated protocol buffer package. - -It is generated from these files: - check.proto - -It has these top-level messages: - CheckReq - CheckResp -*/ -package check - -import ( - "fmt" - "math" - - "github.com/golang/protobuf/proto" - "golang.org/x/net/context" - "google.golang.org/grpc" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package - -type CheckReq struct { - Book string `protobuf:"bytes,1,opt,name=book" json:"book,omitempty"` -} - -func (m *CheckReq) Reset() { *m = CheckReq{} } -func (m *CheckReq) String() string { return proto.CompactTextString(m) } -func (*CheckReq) ProtoMessage() {} -func (*CheckReq) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } - -func (m *CheckReq) GetBook() string { - if m != nil { - return m.Book - } - return "" -} - -type CheckResp struct { - Found bool `protobuf:"varint,1,opt,name=found" json:"found,omitempty"` - Price int64 `protobuf:"varint,2,opt,name=price" json:"price,omitempty"` -} - -func (m *CheckResp) Reset() { *m = CheckResp{} } -func (m *CheckResp) String() string { return proto.CompactTextString(m) } -func (*CheckResp) ProtoMessage() {} -func (*CheckResp) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } - -func (m *CheckResp) GetFound() bool { - if m != nil { - return m.Found - } - return false -} - -func (m *CheckResp) GetPrice() int64 { - if m != nil { - return m.Price - } - return 0 -} - -func init() { - proto.RegisterType((*CheckReq)(nil), "check.checkReq") - proto.RegisterType((*CheckResp)(nil), "check.checkResp") -} - -// Reference imports to suppress errors if they are not otherwise used. -var _ context.Context -var _ grpc.ClientConn - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -const _ = grpc.SupportPackageIsVersion4 - -// Client API for Checker service - -type CheckerClient interface { - Check(ctx context.Context, in *CheckReq, opts ...grpc.CallOption) (*CheckResp, error) -} - -type checkerClient struct { - cc *grpc.ClientConn -} - -func NewCheckerClient(cc *grpc.ClientConn) CheckerClient { - return &checkerClient{cc} -} - -func (c *checkerClient) Check(ctx context.Context, in *CheckReq, opts ...grpc.CallOption) (*CheckResp, error) { - out := new(CheckResp) - err := grpc.Invoke(ctx, "/check.checker/check", in, out, c.cc, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// Server API for Checker service - -type CheckerServer interface { - Check(context.Context, *CheckReq) (*CheckResp, error) -} - -func RegisterCheckerServer(s *grpc.Server, srv CheckerServer) { - s.RegisterService(&_Checker_serviceDesc, srv) -} - -func _Checker_Check_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(CheckReq) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(CheckerServer).Check(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/check.checker/Check", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(CheckerServer).Check(ctx, req.(*CheckReq)) - } - return interceptor(ctx, in, info, handler) -} - -var _Checker_serviceDesc = grpc.ServiceDesc{ - ServiceName: "check.checker", - HandlerType: (*CheckerServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "check", - Handler: _Checker_Check_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "check.proto", -} - -func init() { proto.RegisterFile("check.proto", fileDescriptor0) } - -var fileDescriptor0 = []byte{ - // 136 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4e, 0xce, 0x48, 0x4d, - 0xce, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x05, 0x73, 0x94, 0xe4, 0xb8, 0x38, 0xc0, - 0x8c, 0xa0, 0xd4, 0x42, 0x21, 0x21, 0x2e, 0x96, 0xa4, 0xfc, 0xfc, 0x6c, 0x09, 0x46, 0x05, 0x46, - 0x0d, 0xce, 0x20, 0x30, 0x5b, 0xc9, 0x9c, 0x8b, 0x13, 0x2a, 0x5f, 0x5c, 0x20, 0x24, 0xc2, 0xc5, - 0x9a, 0x96, 0x5f, 0x9a, 0x97, 0x02, 0x56, 0xc1, 0x11, 0x04, 0xe1, 0x80, 0x44, 0x0b, 0x8a, 0x32, - 0x93, 0x53, 0x25, 0x98, 0x14, 0x18, 0x35, 0x98, 0x83, 0x20, 0x1c, 0x23, 0x53, 0x2e, 0x76, 0xb0, - 0xc6, 0xd4, 0x22, 0x21, 0x2d, 0x2e, 0x88, 0x65, 0x42, 0xfc, 0x7a, 0x10, 0x17, 0xc0, 0x6c, 0x94, - 0x12, 0x40, 0x15, 0x28, 0x2e, 0x48, 0x62, 0x03, 0xbb, 0xce, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, - 0x6e, 0x6f, 0xa7, 0x1d, 0xac, 0x00, 0x00, 0x00, -} diff --git a/example/bookstore/rpc/check/internal/server/checkerserver.go b/example/bookstore/rpc/check/internal/server/checkerserver.go index 9644fa0e..f2e226f7 100755 --- a/example/bookstore/rpc/check/internal/server/checkerserver.go +++ b/example/bookstore/rpc/check/internal/server/checkerserver.go @@ -6,8 +6,8 @@ package server import ( "context" + "bookstore/rpc/check/check" "bookstore/rpc/check/internal/logic" - check "bookstore/rpc/check/internal/pb" "bookstore/rpc/check/internal/svc" ) diff --git a/example/bookstore/rpc/check/internal/svc/servicecontext.go b/example/bookstore/rpc/check/internal/svc/servicecontext.go index d497c029..61df0ecf 100755 --- a/example/bookstore/rpc/check/internal/svc/servicecontext.go +++ b/example/bookstore/rpc/check/internal/svc/servicecontext.go @@ -9,12 +9,12 @@ import ( type ServiceContext struct { c config.Config - Model *model.BookModel + Model model.BookModel } func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ c: c, - Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table), + Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache), } } diff --git a/example/bookstore/rpc/model/bookmodel.go b/example/bookstore/rpc/model/bookmodel.go index 76f9483f..1a82c286 100755 --- a/example/bookstore/rpc/model/bookmodel.go +++ b/example/bookstore/rpc/model/bookmodel.go @@ -18,11 +18,18 @@ var ( bookRowsExpectAutoSet = strings.Join(stringx.Remove(bookFieldNames, "create_time", "update_time"), ",") bookRowsWithPlaceHolder = strings.Join(stringx.Remove(bookFieldNames, "book", "create_time", "update_time"), "=?,") + "=?" - bookPrefix = "cache#Book#book#" + cacheBookPrefix = "cache#Book#book#" ) type ( - BookModel struct { + BookModel interface { + Insert(data Book) (sql.Result, error) + FindOne(book string) (*Book, error) + Update(data Book) error + Delete(book string) error + } + + defaultBookModel struct { sqlc.CachedConn table string } @@ -33,23 +40,25 @@ type ( } ) -func NewBookModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *BookModel { - return &BookModel{ +func NewBookModel(conn sqlx.SqlConn, c cache.CacheConf) BookModel { + return &defaultBookModel{ CachedConn: sqlc.NewConn(conn, c), - table: table, + table: "book", } } -func (m *BookModel) Insert(data Book) (sql.Result, error) { - query := `insert into ` + m.table + ` (` + bookRowsExpectAutoSet + `) values (?, ?)` - return m.ExecNoCache(query, data.Book, data.Price) +func (m *defaultBookModel) Insert(data Book) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, bookRowsExpectAutoSet) + ret, err := m.ExecNoCache(query, data.Book, data.Price) + + return ret, err } -func (m *BookModel) FindOne(book string) (*Book, error) { - bookKey := fmt.Sprintf("%s%v", bookPrefix, book) +func (m *defaultBookModel) FindOne(book string) (*Book, error) { + bookKey := fmt.Sprintf("%s%v", cacheBookPrefix, book) var resp Book err := m.QueryRow(&resp, bookKey, func(conn sqlx.SqlConn, v interface{}) error { - query := `select ` + bookRows + ` from ` + m.table + ` where book = ? limit 1` + query := fmt.Sprintf("select %s from %s where book = ? limit 1", bookRows, m.table) return conn.QueryRow(v, query, book) }) switch err { @@ -62,20 +71,30 @@ func (m *BookModel) FindOne(book string) (*Book, error) { } } -func (m *BookModel) Update(data Book) error { - bookKey := fmt.Sprintf("%s%v", bookPrefix, data.Book) +func (m *defaultBookModel) Update(data Book) error { + bookKey := fmt.Sprintf("%s%v", cacheBookPrefix, data.Book) _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { - query := `update ` + m.table + ` set ` + bookRowsWithPlaceHolder + ` where book = ?` + query := fmt.Sprintf("update %s set %s where book = ?", m.table, bookRowsWithPlaceHolder) return conn.Exec(query, data.Price, data.Book) }, bookKey) return err } -func (m *BookModel) Delete(book string) error { - bookKey := fmt.Sprintf("%s%v", bookPrefix, book) +func (m *defaultBookModel) Delete(book string) error { + + bookKey := fmt.Sprintf("%s%v", cacheBookPrefix, book) _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { - query := `delete from ` + m.table + ` where book = ?` + query := fmt.Sprintf("delete from %s where book = ?", m.table) return conn.Exec(query, book) }, bookKey) return err } + +func (m *defaultBookModel) formatPrimary(primary interface{}) string { + return fmt.Sprintf("%s%v", cacheBookPrefix, primary) +} + +func (m *defaultBookModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error { + query := fmt.Sprintf("select %s from %s where book = ? limit 1", bookRows, m.table) + return conn.QueryRow(v, query, primary) +} diff --git a/example/bookstore/rpc/model/vars.go b/example/bookstore/rpc/model/vars.go old mode 100755 new mode 100644