This commit is contained in:
withchao
2023-02-27 09:53:17 +08:00
parent 0547f25ecb
commit 08fdac83fc
4 changed files with 30 additions and 27 deletions
+23 -17
View File
@@ -1,6 +1,7 @@
package api2rpc
import (
"OpenIM/pkg/errs"
"context"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
@@ -8,7 +9,7 @@ import (
type rpcFunc[E, C, D any] func(client E, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)
func Rpc[A, B, C, D any, E any](apiReq *A, apiResp *B, rpc func(client E, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)) RpcCall[A, B, C, D, E] {
func New[A, B, C, D any, E any](apiReq *A, apiResp *B, rpc func(client E, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)) RpcCall[A, B, C, D, E] {
return &rpcCall[A, B, C, D, E]{
apiReq: apiReq,
apiResp: apiResp,
@@ -19,9 +20,7 @@ func Rpc[A, B, C, D any, E any](apiReq *A, apiResp *B, rpc func(client E, ctx co
type rpcCall[A, B, C, D any, E any] struct {
apiReq *A
apiResp *B
client func() (E, error)
rpcFn func(client E, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)
api Api
before func(apiReq *A, rpcReq *C, bind func() error) error
after func(rpcResp *D, apiResp *B, bind func() error) error
}
@@ -36,14 +35,22 @@ func (r *rpcCall[A, B, C, D, E]) After(fn func(rpcResp *D, apiResp *B, bind func
return r
}
func (r *rpcCall[A, B, C, D, E]) Must(c *gin.Context, client func() (E, error)) RpcCall[A, B, C, D, E] {
r.api = NewGin1(c)
r.client = client
return r
}
func (r *rpcCall[A, B, C, D, E]) Call() {
r.api.Resp(r.apiResp, r.call())
func (r *rpcCall[A, B, C, D, E]) Call(c *gin.Context, client func() (E, error)) {
var resp baseResp
err := r.call(c, client)
if err == nil {
resp.Data = r.apiResp
} else {
cerr, ok := err.(errs.Coderr)
if ok {
resp.ErrCode = cerr.Code()
resp.ErrMsg = cerr.Msg()
resp.ErrDtl = cerr.Detail()
} else {
resp.ErrCode = 10000
resp.ErrMsg = err.Error()
}
}
}
func (r *rpcCall[A, B, C, D, E]) defaultCopyReq(rpcReq *C) error {
@@ -60,8 +67,8 @@ func (r *rpcCall[A, B, C, D, E]) defaultCopyResp(rpcResp *D) error {
return nil
}
func (r *rpcCall[A, B, C, D, E]) call() error {
if err := r.api.Bind(r.apiReq); err != nil {
func (r *rpcCall[A, B, C, D, E]) call(c *gin.Context, client func() (E, error)) error {
if err := c.BindJSON(r.apiReq); err != nil {
return err
}
var err error
@@ -74,11 +81,11 @@ func (r *rpcCall[A, B, C, D, E]) call() error {
if err != nil {
return err
}
client, err := r.client()
cli, err := client()
if err != nil {
return err
}
rpcResp, err := r.rpcFn(client, r.api.Context(), &rpcReq)
rpcResp, err := r.rpcFn(cli, c, &rpcReq)
if err != nil {
return err
}
@@ -93,6 +100,5 @@ func (r *rpcCall[A, B, C, D, E]) call() error {
type RpcCall[A, B, C, D, E any] interface {
Before(fn func(apiReq *A, rpcReq *C, bind func() error) error) RpcCall[A, B, C, D, E]
After(fn func(rpcResp *D, apiResp *B, bind func() error) error) RpcCall[A, B, C, D, E]
Must(c *gin.Context, client func() (E, error)) RpcCall[A, B, C, D, E]
Call()
Call(c *gin.Context, client func() (E, error))
}