mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-04 22:00:39 +00:00
refactor bind functions based on generics (#22055)
This commit is contained in:
parent
003b4e209c
commit
6398ca745a
4 changed files with 168 additions and 188 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue