Skip to content

Commit

Permalink
fix: deepcopy operator, to fix all request share the same operator ob…
Browse files Browse the repository at this point in the history
…ject problem
  • Loading branch information
tangx committed Dec 14, 2021
1 parent 40bf171 commit 0d72142
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/example/apis/failed/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package failed

import (
"fmt"
"time"

"github.com/gin-gonic/gin"
"github.com/go-jarvis/rum-gonic/pkg/httpx"
)

type CreateUser struct {
httpx.MethodPost `path:"/create-user"`

Data CreateUserParams `body:"body"`
Sleep int `query:"sleep"`
}

type CreateUserParams struct {
Name string `json:"name"`
Password string `json:"password"`
}

func (u *CreateUser) Output(c *gin.Context) (interface{}, error) {

fmt.Println("before:", u)

d := time.Duration(u.Sleep) * time.Second
time.Sleep(d)

fmt.Println("after:", u)

return u, nil
}
33 changes: 33 additions & 0 deletions internal/example/apis/failed/binding.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@



### GET
GET http://127.0.0.1:8080/rum/binding?name=zhangsan&password=zhangsan123




### GET
GET http://127.0.0.1:8080/rum/binding


### create user
POST http://127.0.0.1:8080/rum/create-user?sleep=5
Content-Type: application/json

{
"name":"zhangsan",
"password":"zhangsan123"
}


### create user
POST http://127.0.0.1:8080/rum/create-user?sleep=3
Content-Type: application/json

{
"name":"wangwu",
"password":"wangwu123"
}


2 changes: 2 additions & 0 deletions internal/example/apis/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apis

import (
"github.com/go-jarvis/rum-gonic/internal/example/apis/failed"
"github.com/go-jarvis/rum-gonic/internal/example/apis/index"
"github.com/go-jarvis/rum-gonic/internal/example/apis/pingpong"
"github.com/go-jarvis/rum-gonic/internal/example/auth"
Expand All @@ -15,6 +16,7 @@ var (
func init() {

RouterRoot.Register(RouterV0)
RouterRoot.Register(&failed.CreateUser{})

{
RouterV0.Register(auth.AdminUsersMiddelware)
Expand Down
4 changes: 4 additions & 0 deletions rum/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (r *RouterGroup) register(parent *RouterGroup) {
r.ginRG = r.parent.ginRG.Group(r.path)

for _, op := range r.operators {

// 添加中间件
if r.use(op) {
continue
Expand Down Expand Up @@ -140,6 +141,9 @@ func (r *RouterGroup) handlerfunc(op Operator) HandlerFunc {

return func(c *gin.Context) {

//todo: op deepcopy
op := DeepCopyOperator(op)

err := ginbinder.ShouldBindRequest(c, op)
if err != nil {
err = statuserrors.Wrap(err, http.StatusBadRequest, Err_BindingRequest)
Expand Down
28 changes: 28 additions & 0 deletions rum/operator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rum

import (
"reflect"

"github.com/gin-gonic/gin"
)

Expand All @@ -15,3 +17,29 @@ type PathOperator interface {
type MethodOperator interface {
Method() string
}

func DeepCopyOperator(op Operator) Operator {
return NewOperatorFactory(op).NewOperator()
}

type OperatorFactory struct {
Type reflect.Type
Operator Operator
}

func NewOperatorFactory(op Operator) *OperatorFactory {
fact := &OperatorFactory{}

fact.Type = deReflectType(reflect.TypeOf(op))
fact.Operator = op

return fact
}

func (fact *OperatorFactory) NewOperator() Operator {

opc := reflect.New(fact.Type).Interface().(Operator)

return opc

}

0 comments on commit 0d72142

Please sign in to comment.