From 0b02b80659f56869a42634501ba80dcfdf65acad Mon Sep 17 00:00:00 2001 From: Muhammed Efe Cetin Date: Mon, 30 Dec 2024 01:21:08 +0300 Subject: [PATCH] reduce code duplication --- binder/cookie.go | 13 +------------ binder/form.go | 14 +------------- binder/header.go | 22 ++++++++++------------ binder/mapping.go | 18 ++++++++++++++++++ binder/query.go | 17 +---------------- binder/resp_header.go | 23 +++++++++++------------ 6 files changed, 42 insertions(+), 65 deletions(-) diff --git a/binder/cookie.go b/binder/cookie.go index 230794f45a..5b9ccf1ed3 100644 --- a/binder/cookie.go +++ b/binder/cookie.go @@ -1,9 +1,6 @@ package binder import ( - "reflect" - "strings" - "github.com/gofiber/utils/v2" "github.com/valyala/fasthttp" ) @@ -30,15 +27,7 @@ func (b *CookieBinding) Bind(req *fasthttp.Request, out any) error { k := utils.UnsafeString(key) v := utils.UnsafeString(val) - - if b.EnableSplitting && strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) { - values := strings.Split(v, ",") - for i := 0; i < len(values); i++ { - data[k] = append(data[k], values[i]) - } - } else { - data[k] = append(data[k], v) - } + err = formatBindData(out, data, k, v, b.EnableSplitting, false) }) if err != nil { diff --git a/binder/form.go b/binder/form.go index 91b0beec95..1e47cf5f96 100644 --- a/binder/form.go +++ b/binder/form.go @@ -37,19 +37,7 @@ func (b *FormBinding) Bind(req *fasthttp.Request, out any) error { k := utils.UnsafeString(key) v := utils.UnsafeString(val) - - if strings.Contains(k, "[") { - k, err = parseParamSquareBrackets(k) - } - - if b.EnableSplitting && strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) { - values := strings.Split(v, ",") - for i := 0; i < len(values); i++ { - data[k] = append(data[k], values[i]) - } - } else { - data[k] = append(data[k], v) - } + err = formatBindData(out, data, k, v, b.EnableSplitting, true) }) if err != nil { diff --git a/binder/header.go b/binder/header.go index b04ce9add3..763be56795 100644 --- a/binder/header.go +++ b/binder/header.go @@ -1,9 +1,6 @@ package binder import ( - "reflect" - "strings" - "github.com/gofiber/utils/v2" "github.com/valyala/fasthttp" ) @@ -21,20 +18,21 @@ func (*HeaderBinding) Name() string { // Bind parses the request header and returns the result. func (b *HeaderBinding) Bind(req *fasthttp.Request, out any) error { data := make(map[string][]string) + var err error req.Header.VisitAll(func(key, val []byte) { + if err != nil { + return + } + k := utils.UnsafeString(key) v := utils.UnsafeString(val) - - if b.EnableSplitting && strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) { - values := strings.Split(v, ",") - for i := 0; i < len(values); i++ { - data[k] = append(data[k], values[i]) - } - } else { - data[k] = append(data[k], v) - } + err = formatBindData(out, data, k, v, b.EnableSplitting, false) }) + if err != nil { + return err + } + return parse(b.Name(), out, data) } diff --git a/binder/mapping.go b/binder/mapping.go index d8b692f7e4..527f4741e9 100644 --- a/binder/mapping.go +++ b/binder/mapping.go @@ -249,3 +249,21 @@ func FilterFlags(content string) string { } return content } + +func formatBindData(out any, data map[string][]string, key, value string, enableSplitting, supportBracketNotation bool) error { //nolint:revive // it's okay + var err error + if supportBracketNotation && strings.Contains(key, "[") { + key, err = parseParamSquareBrackets(key) + } + + if enableSplitting && strings.Contains(value, ",") && equalFieldType(out, reflect.Slice, key) { + values := strings.Split(value, ",") + for i := 0; i < len(values); i++ { + data[key] = append(data[key], values[i]) + } + } else { + data[key] = append(data[key], value) + } + + return err +} diff --git a/binder/query.go b/binder/query.go index 9ee500ba63..d2ac309215 100644 --- a/binder/query.go +++ b/binder/query.go @@ -1,9 +1,6 @@ package binder import ( - "reflect" - "strings" - "github.com/gofiber/utils/v2" "github.com/valyala/fasthttp" ) @@ -30,19 +27,7 @@ func (b *QueryBinding) Bind(reqCtx *fasthttp.Request, out any) error { k := utils.UnsafeString(key) v := utils.UnsafeString(val) - - if strings.Contains(k, "[") { - k, err = parseParamSquareBrackets(k) - } - - if b.EnableSplitting && strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) { - values := strings.Split(v, ",") - for i := 0; i < len(values); i++ { - data[k] = append(data[k], values[i]) - } - } else { - data[k] = append(data[k], v) - } + err = formatBindData(out, data, k, v, b.EnableSplitting, true) }) if err != nil { diff --git a/binder/resp_header.go b/binder/resp_header.go index fc84d01402..cb29e99d6f 100644 --- a/binder/resp_header.go +++ b/binder/resp_header.go @@ -1,9 +1,6 @@ package binder import ( - "reflect" - "strings" - "github.com/gofiber/utils/v2" "github.com/valyala/fasthttp" ) @@ -21,20 +18,22 @@ func (*RespHeaderBinding) Name() string { // Bind parses the response header and returns the result. func (b *RespHeaderBinding) Bind(resp *fasthttp.Response, out any) error { data := make(map[string][]string) + var err error + resp.Header.VisitAll(func(key, val []byte) { + if err != nil { + return + } + k := utils.UnsafeString(key) v := utils.UnsafeString(val) - - if b.EnableSplitting && strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) { - values := strings.Split(v, ",") - for i := 0; i < len(values); i++ { - data[k] = append(data[k], values[i]) - } - } else { - data[k] = append(data[k], v) - } + err = formatBindData(out, data, k, v, b.EnableSplitting, false) }) + if err != nil { + return err + } + return parse(b.Name(), out, data) }