This commit is contained in:
withchao
2023-02-24 19:16:26 +08:00
parent d7640ca864
commit c019dd5f1e
12 changed files with 239 additions and 211 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ import (
// return
// }
// tracelog.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
// etcdConn, err := rpc.GetConn(ctx, rpcName)
// etcdConn, err := rpcFn.GetConn(ctx, rpcName)
// if err != nil {
// WriteErrorResponse(ctx, "GetConn", err)
// return
+8
View File
@@ -13,3 +13,11 @@ type ApiBind[A, B any] interface {
Context() context.Context
Resp(resp *B, err error)
}
type Api interface {
OperationID() string
OpUserID() string
Context() context.Context
Bind(req any) error
Resp(resp any, err error)
}
+34 -3
View File
@@ -10,9 +10,9 @@ import (
// //var api ApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp] = NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c)
// var api ApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp] = nil
// var client func(conn *grpc.ClientConn) group.GroupClient = nil
// var rpc func(ctx context.Context, in *group.KickGroupMemberReq, opts ...grpc.CallOption) (*group.KickGroupMemberResp, error) = nil
// //NewRpc(api, client, rpc).Name("group").Call()
// NewRpc(api, client, rpc).Name("group").Call()
// var rpcFn func(ctx context.Context, in *group.KickGroupMemberReq, opts ...grpc.CallOption) (*group.KickGroupMemberResp, error) = nil
// //NewRpc(api, client, rpcFn).Name("group").Call()
// NewRpc(api, client, rpcFn).Name("group").Call()
//
// // 可以自定义编辑请求和响应
// //a := NewRpc(NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
@@ -65,3 +65,34 @@ func (g *ginApiBind[A, B]) Write(b *B) {
func (g *ginApiBind[A, B]) Context() context.Context {
return g.c
}
func NewGin1(c *gin.Context) Api {
return &ginApi{
c: c,
}
}
type ginApi struct {
c *gin.Context
}
func (g *ginApi) OperationID() string {
return g.c.GetHeader("operationID")
}
func (g *ginApi) OpUserID() string {
return g.c.MustGet("opUserID").(string)
}
func (g *ginApi) Context() context.Context {
return g.c
}
func (g *ginApi) Bind(req any) error {
return g.c.BindJSON(req)
}
func (g *ginApi) Resp(resp any, err error) {
//TODO implement me
panic("implement me")
}
+28 -11
View File
@@ -3,18 +3,26 @@ package api2rpc
import (
"context"
"google.golang.org/grpc"
"reflect"
)
var nameMap = map[string]string{}
func getName[T any]() string {
var t T
return reflect.TypeOf(&t).Elem().Name()
}
// NewRpc A: apiReq B: apiResp C: rpcReq D: rpcResp Z: rpcClient (group.GroupClient)
func NewRpc[A, B any, C, D any, Z any](bind ApiBind[A, B], client func(conn *grpc.ClientConn) Z, rpc func(client Z, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)) *Rpc[A, B, C, D, Z] {
return &Rpc[A, B, C, D, Z]{
func NewRpc[A, B any, C, D any, Z any](bind ApiBind[A, B], client func(conn *grpc.ClientConn) Z, rpc func(client Z, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)) *RpcXXXX[A, B, C, D, Z] {
return &RpcXXXX[A, B, C, D, Z]{
bind: bind,
client: client,
rpc: rpc,
}
}
type Rpc[A, B any, C, D any, Z any] struct {
type RpcXXXX[A, B any, C, D any, Z any] struct {
bind ApiBind[A, B]
name string
client func(conn *grpc.ClientConn) Z
@@ -23,36 +31,45 @@ type Rpc[A, B any, C, D any, Z any] struct {
after func(rpcResp *D, apiResp *B, bind func() error) error
}
func (a *Rpc[A, B, C, D, Z]) Name(name string) *Rpc[A, B, C, D, Z] {
func (a *RpcXXXX[A, B, C, D, Z]) Name(name string) *RpcXXXX[A, B, C, D, Z] {
a.name = name
return a
}
func (a *Rpc[A, B, C, D, Z]) Before(fn func(apiReq *A, rpcReq *C, bind func() error) error) *Rpc[A, B, C, D, Z] {
func (a *RpcXXXX[A, B, C, D, Z]) Before(fn func(apiReq *A, rpcReq *C, bind func() error) error) *RpcXXXX[A, B, C, D, Z] {
a.before = fn
return a
}
func (a *Rpc[A, B, C, D, Z]) After(fn func(rpcResp *D, apiResp *B, bind func() error) error) *Rpc[A, B, C, D, Z] {
func (a *RpcXXXX[A, B, C, D, Z]) After(fn func(rpcResp *D, apiResp *B, bind func() error) error) *RpcXXXX[A, B, C, D, Z] {
a.after = fn
return a
}
func (a *Rpc[A, B, C, D, Z]) defaultCopyReq(apiReq *A, rpcReq *C) error {
func (a *RpcXXXX[A, B, C, D, Z]) defaultCopyReq(apiReq *A, rpcReq *C) error {
CopyAny(apiReq, rpcReq)
return nil
}
func (a *Rpc[A, B, C, D, Z]) defaultCopyResp(rpcResp *D, apiResp *B) error {
func (a *RpcXXXX[A, B, C, D, Z]) defaultCopyResp(rpcResp *D, apiResp *B) error {
CopyAny(rpcResp, apiResp)
return nil
}
func (a *Rpc[A, B, C, D, Z]) GetGrpcConn() (*grpc.ClientConn, error) {
func (a *RpcXXXX[A, B, C, D, Z]) getZtype() string {
return ""
}
func (a *RpcXXXX[A, B, C, D, Z]) GetGrpcConn() (*grpc.ClientConn, error) {
if a.name == "" {
a.name = nameMap[getName[Z]()]
}
// todo 获取连接
return nil, nil // todo
}
func (a *Rpc[A, B, C, D, Z]) execute() (*B, error) {
func (a *RpcXXXX[A, B, C, D, Z]) execute() (*B, error) {
var apiReq A
if err := a.bind.Bind(&apiReq); err != nil {
return nil, err
@@ -92,6 +109,6 @@ func (a *Rpc[A, B, C, D, Z]) execute() (*B, error) {
return &apiResp, nil
}
func (a *Rpc[A, B, C, D, Z]) Call() {
func (a *RpcXXXX[A, B, C, D, Z]) Call() {
a.bind.Resp(a.execute())
}
+98
View File
@@ -0,0 +1,98 @@
package api2rpc
import (
"context"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
)
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] {
return &rpcCall[A, B, C, D, E]{
apiReq: apiReq,
apiResp: apiResp,
rpcFn: rpc,
}
}
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
}
func (r *rpcCall[A, B, C, D, E]) Before(fn func(apiReq *A, rpcReq *C, bind func() error) error) RpcCall[A, B, C, D, E] {
r.before = fn
return r
}
func (r *rpcCall[A, B, C, D, E]) After(fn func(rpcResp *D, apiResp *B, bind func() error) error) RpcCall[A, B, C, D, E] {
r.after = fn
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]) defaultCopyReq(rpcReq *C) error {
if r.apiReq != nil {
CopyAny(r.apiReq, rpcReq)
}
return nil
}
func (r *rpcCall[A, B, C, D, E]) defaultCopyResp(rpcResp *D) error {
if r.apiResp != nil {
CopyAny(rpcResp, r.apiResp)
}
return nil
}
func (r *rpcCall[A, B, C, D, E]) call() error {
if err := r.api.Bind(r.apiReq); err != nil {
return err
}
var err error
var rpcReq C
if r.before == nil {
err = r.defaultCopyReq(&rpcReq)
} else {
err = r.before(r.apiReq, &rpcReq, func() error { return r.defaultCopyReq(&rpcReq) })
}
if err != nil {
return err
}
client, err := r.client()
if err != nil {
return err
}
rpcResp, err := r.rpcFn(client, r.api.Context(), &rpcReq)
if err != nil {
return err
}
var apiResp B
if r.after == nil {
return r.defaultCopyResp(rpcResp)
} else {
return r.after(rpcResp, &apiResp, func() error { return r.defaultCopyResp(rpcResp) })
}
}
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()
}