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 /modules/web | |
parent | 003b4e209ce27e21e68b6a48ad2a7f6a1bcb17fb (diff) | |
download | gitea-6398ca745aabdc40f10465e71da99c5a3866990e.tar.gz gitea-6398ca745aabdc40f10465e71da99c5a3866990e.zip |
refactor bind functions based on generics (#22055)
Diffstat (limited to 'modules/web')
-rw-r--r-- | modules/web/route.go | 12 |
1 files changed, 2 insertions, 10 deletions
diff --git a/modules/web/route.go b/modules/web/route.go index cd72aabae5..0f2fdc33b5 100644 --- a/modules/web/route.go +++ b/modules/web/route.go @@ -7,7 +7,6 @@ import ( goctx "context" "fmt" "net/http" - "reflect" "strings" "code.gitea.io/gitea/modules/context" @@ -18,16 +17,9 @@ import ( ) // Bind binding an obj to a handler -func Bind(obj interface{}) http.HandlerFunc { - tp := reflect.TypeOf(obj) - if tp.Kind() == reflect.Ptr { - tp = tp.Elem() - } - if tp.Kind() != reflect.Struct { - panic("Only structs are allowed to bind") - } +func Bind[T any](obj T) http.HandlerFunc { return Wrap(func(ctx *context.Context) { - 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 binding.Bind(ctx.Req, theObj) SetForm(ctx, theObj) middleware.AssignForm(theObj, ctx.Data) |