Skip to content

Commit

Permalink
reduce code duplications in bindMultipart
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Dec 31, 2024
1 parent 0b02b80 commit 8c28b9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
29 changes: 7 additions & 22 deletions binder/form.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package binder

import (
"reflect"
"strings"

"github.com/gofiber/utils/v2"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -49,32 +46,20 @@ func (b *FormBinding) Bind(req *fasthttp.Request, out any) error {

// bindMultipart parses the request body and returns the result.
func (b *FormBinding) bindMultipart(req *fasthttp.Request, out any) error {
data, err := req.MultipartForm()
multipartForm, err := req.MultipartForm()
if err != nil {
return err
}

temp := make(map[string][]string)
for key, values := range data.Value {
if strings.Contains(key, "[") {
k, err := parseParamSquareBrackets(key)
if err != nil {
return err
}

key = k // We have to update key in case bracket notation and slice type are used at the same time
}

for _, v := range values {
if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, key) {
temp[key] = strings.Split(v, ",")
} else {
temp[key] = append(temp[key], v)
}
data := make(map[string][]string)
for key, values := range multipartForm.Value {
err = formatBindData(out, data, key, values, b.EnableSplitting, true)
if err != nil {
return err
}
}

return parse(b.Name(), out, temp)
return parse(b.Name(), out, data)
}

// Reset resets the FormBinding binder.
Expand Down
25 changes: 21 additions & 4 deletions binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func parseToStruct(aliasTag string, out any, data map[string][]string) error {
func parseToMap(ptr any, data map[string][]string) error {
elem := reflect.TypeOf(ptr).Elem()

//nolint:exhaustive // it's not necessary to check all types
switch elem.Kind() {
case reflect.Slice:
newMap, ok := ptr.(map[string][]string)
Expand All @@ -132,6 +131,8 @@ func parseToMap(ptr any, data map[string][]string) error {

newMap[k] = v[len(v)-1]
}
default:
return nil // it's not necessary to check all types
}

return nil
Expand Down Expand Up @@ -250,12 +251,30 @@ 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
func formatBindData[T any](out any, data map[string][]string, key string, value T, enableSplitting, supportBracketNotation bool) error { //nolint:revive // it's okay
var err error
if supportBracketNotation && strings.Contains(key, "[") {
key, err = parseParamSquareBrackets(key)
if err != nil {
return err
}
}

switch v := any(value).(type) {
case string:
assignBindData(out, data, key, v, enableSplitting)
case []string:
for _, val := range v {
assignBindData(out, data, key, val, enableSplitting)
}
default:
return fmt.Errorf("unsupported value type: %T", value)
}

return err
}

func assignBindData(out any, data map[string][]string, key, value string, enableSplitting bool) { //nolint:revive // it's okay
if enableSplitting && strings.Contains(value, ",") && equalFieldType(out, reflect.Slice, key) {
values := strings.Split(value, ",")
for i := 0; i < len(values); i++ {
Expand All @@ -264,6 +283,4 @@ func formatBindData(out any, data map[string][]string, key, value string, enable
} else {
data[key] = append(data[key], value)
}

return err
}

0 comments on commit 8c28b9a

Please sign in to comment.