Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor bind functions based on generics #22055

Merged
merged 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions modules/web/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
goctx "context"
"fmt"
"net/http"
"reflect"
"strings"

"code.gitea.io/gitea/modules/context"
Expand All @@ -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 {
lunny marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
9 changes: 2 additions & 7 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import (
gocontext "context"
"fmt"
"net/http"
"reflect"
"strings"

"code.gitea.io/gitea/models/organization"
Expand Down Expand Up @@ -568,13 +567,9 @@ func mustNotBeArchived(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()))
Expand Down
9 changes: 2 additions & 7 deletions routers/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package private

import (
"net/http"
"reflect"
"strings"

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

// bind binding an obj to a handler
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.PrivateContext) {
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)
web.SetForm(ctx, theObj)
})
Expand Down
Loading