Skip to content

Commit 6398ca7

Browse files
authored
refactor bind functions based on generics (#22055)
1 parent 003b4e2 commit 6398ca7

File tree

4 files changed

+168
-188
lines changed

4 files changed

+168
-188
lines changed

Diff for: modules/web/route.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
goctx "context"
88
"fmt"
99
"net/http"
10-
"reflect"
1110
"strings"
1211

1312
"code.gitea.io/gitea/modules/context"
@@ -18,16 +17,9 @@ import (
1817
)
1918

2019
// Bind binding an obj to a handler
21-
func Bind(obj interface{}) http.HandlerFunc {
22-
tp := reflect.TypeOf(obj)
23-
if tp.Kind() == reflect.Ptr {
24-
tp = tp.Elem()
25-
}
26-
if tp.Kind() != reflect.Struct {
27-
panic("Only structs are allowed to bind")
28-
}
20+
func Bind[T any](obj T) http.HandlerFunc {
2921
return Wrap(func(ctx *context.Context) {
30-
theObj := reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
22+
theObj := new(T) // create a new form obj for every request but not use obj directly
3123
binding.Bind(ctx.Req, theObj)
3224
SetForm(ctx, theObj)
3325
middleware.AssignForm(theObj, ctx.Data)

Diff for: routers/api/v1/api.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import (
6767
gocontext "context"
6868
"fmt"
6969
"net/http"
70-
"reflect"
7170
"strings"
7271

7372
"code.gitea.io/gitea/models/organization"
@@ -575,13 +574,9 @@ func mustEnableAttachments(ctx *context.APIContext) {
575574
}
576575

577576
// bind binding an obj to a func(ctx *context.APIContext)
578-
func bind(obj interface{}) http.HandlerFunc {
579-
tp := reflect.TypeOf(obj)
580-
for tp.Kind() == reflect.Ptr {
581-
tp = tp.Elem()
582-
}
577+
func bind[T any](obj T) http.HandlerFunc {
583578
return web.Wrap(func(ctx *context.APIContext) {
584-
theObj := reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
579+
theObj := new(T) // create a new form obj for every request but not use obj directly
585580
errs := binding.Bind(ctx.Req, theObj)
586581
if len(errs) > 0 {
587582
ctx.Error(http.StatusUnprocessableEntity, "validationError", fmt.Sprintf("%s: %s", errs[0].FieldNames, errs[0].Error()))

Diff for: routers/private/internal.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package private
66

77
import (
88
"net/http"
9-
"reflect"
109
"strings"
1110

1211
"code.gitea.io/gitea/modules/context"
@@ -39,13 +38,9 @@ func CheckInternalToken(next http.Handler) http.Handler {
3938
}
4039

4140
// bind binding an obj to a handler
42-
func bind(obj interface{}) http.HandlerFunc {
43-
tp := reflect.TypeOf(obj)
44-
for tp.Kind() == reflect.Ptr {
45-
tp = tp.Elem()
46-
}
41+
func bind[T any](obj T) http.HandlerFunc {
4742
return web.Wrap(func(ctx *context.PrivateContext) {
48-
theObj := reflect.New(tp).Interface() // create a new form obj for every request but not use obj directly
43+
theObj := new(T) // create a new form obj for every request but not use obj directly
4944
binding.Bind(ctx.Req, theObj)
5045
web.SetForm(ctx, theObj)
5146
})

0 commit comments

Comments
 (0)