diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-12-12 16:09:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-12 16:09:26 +0800 |
commit | 6398ca745aabdc40f10465e71da99c5a3866990e (patch) | |
tree | c990402874826edd10ff6f31cfcdc9709b795e80 /routers/api | |
parent | 003b4e209ce27e21e68b6a48ad2a7f6a1bcb17fb (diff) | |
download | gitea-6398ca745aabdc40f10465e71da99c5a3866990e.tar.gz gitea-6398ca745aabdc40f10465e71da99c5a3866990e.zip |
refactor bind functions based on generics (#22055)
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/api.go | 9 |
1 files changed, 2 insertions, 7 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 14b168c242..82ff7ae0be 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -67,7 +67,6 @@ import ( gocontext "context" "fmt" "net/http" - "reflect" "strings" "code.gitea.io/gitea/models/organization" @@ -575,13 +574,9 @@ func mustEnableAttachments(ctx *context.APIContext) { } // bind binding an obj to a func(ctx *context.APIContext) -func bind(obj interface{}) http.HandlerFunc { - tp := reflect.TypeOf(obj) - for tp.Kind() == reflect.Ptr { - tp = tp.Elem() - } +func bind[T any](obj T) http.HandlerFunc { return web.Wrap(func(ctx *context.APIContext) { - theObj := reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly + theObj := new(T) // create a new form obj for every request but not use obj directly errs := binding.Bind(ctx.Req, theObj) if len(errs) > 0 { ctx.Error(http.StatusUnprocessableEntity, "validationError", fmt.Sprintf("%s: %s", errs[0].FieldNames, errs[0].Error())) |