add bookstore example
This commit is contained in:
62
example/bookstore/rpc/add/adder/adder.go
Executable file
62
example/bookstore/rpc/add/adder/adder.go
Executable file
@@ -0,0 +1,62 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: add.proto
|
||||
|
||||
//go:generate mockgen -destination ./adder_mock.go -package adder -source $GOFILE
|
||||
|
||||
package adder
|
||||
|
||||
import (
|
||||
add "bookstore/rpc/add/pb"
|
||||
"context"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/jsonx"
|
||||
"github.com/tal-tech/go-zero/rpcx"
|
||||
)
|
||||
|
||||
type (
|
||||
Adder interface {
|
||||
Add(ctx context.Context, in *AddReq) (*AddResp, error)
|
||||
}
|
||||
|
||||
defaultAdder struct {
|
||||
cli rpcx.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewAdder(cli rpcx.Client) Adder {
|
||||
return &defaultAdder{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAdder) Add(ctx context.Context, in *AddReq) (*AddResp, error) {
|
||||
var request add.AddReq
|
||||
bts, err := jsonx.Marshal(in)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
err = jsonx.Unmarshal(bts, &request)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
client := add.NewAdderClient(m.cli.Conn())
|
||||
resp, err := client.Add(ctx, &request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ret AddResp
|
||||
bts, err = jsonx.Marshal(resp)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
err = jsonx.Unmarshal(bts, &ret)
|
||||
if err != nil {
|
||||
return nil, errJsonConvert
|
||||
}
|
||||
|
||||
return &ret, nil
|
||||
}
|
||||
49
example/bookstore/rpc/add/adder/adder_mock.go
Normal file
49
example/bookstore/rpc/add/adder/adder_mock.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// 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)
|
||||
}
|
||||
19
example/bookstore/rpc/add/adder/types.go
Executable file
19
example/bookstore/rpc/add/adder/types.go
Executable file
@@ -0,0 +1,19 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: add.proto
|
||||
|
||||
package adder
|
||||
|
||||
import "errors"
|
||||
|
||||
var errJsonConvert = errors.New("json convert error")
|
||||
|
||||
type (
|
||||
AddReq struct {
|
||||
Book string `json:"book,omitempty"`
|
||||
Price int64 `json:"price,omitempty"`
|
||||
}
|
||||
|
||||
AddResp struct {
|
||||
Ok bool `json:"ok,omitempty"`
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user