form_mapping.go
This file
Path
binding/form_mapping.go
550 lines · 13.3 KiB · Go
Last changed by
refactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc
History of this fileTake it
The walk stopped after 200 commits: lines older than cf32d2d point at it.
chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 1–7why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 8why, one step back8 "encoding"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 9–10why, one step back9 "errors"10 "fmt"refactor(binding): use maps.Copy for cleaner map handling (#4352)russcossbefore adoption39858a0
line 11why, one step back11 "maps"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 12–17why, one step back12 "mime/multipart"13 "reflect"14 "strconv"15 "strings"16 "time"17feat: support custom json codec at runtime (#3391)Timbefore adoption688a429
line 18why, one step back18 "github.com/gin-gonic/gin/codec/json"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 19–121why, one step back19 "github.com/gin-gonic/gin/internal/bytesconv"20)2122var (23 errUnknownType = errors.New("unknown type")2425 // ErrConvertMapStringSlice can not convert to map[string][]string26 ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")2728 // ErrConvertToMapString can not convert to map[string]string29 ErrConvertToMapString = errors.New("can not convert to map of strings")30)3132func mapURI(ptr any, m map[string][]string) error {33 return mapFormByTag(ptr, m, "uri")34}3536func mapForm(ptr any, form map[string][]string) error {37 return mapFormByTag(ptr, form, "form")38}3940func MapFormWithTag(ptr any, form map[string][]string, tag string) error {41 return mapFormByTag(ptr, form, tag)42}4344var emptyField = reflect.StructField{}4546func mapFormByTag(ptr any, form map[string][]string, tag string) error {47 // Check if ptr is a map48 ptrVal := reflect.ValueOf(ptr)49 var pointed any50 if ptrVal.Kind() == reflect.Ptr {51 ptrVal = ptrVal.Elem()52 pointed = ptrVal.Interface()53 }54 if ptrVal.Kind() == reflect.Map &&55 ptrVal.Type().Key().Kind() == reflect.String {56 if pointed != nil {57 ptr = pointed58 }59 return setFormMap(ptr, form)60 }6162 return mappingByPtr(ptr, formSource(form), tag)63}6465// setter tries to set value on a walking by fields of a struct66type setter interface {67 TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSet bool, err error)68}6970type formSource map[string][]string7172var _ setter = formSource(nil)7374// TrySet tries to set a value by request's form source (like map[string][]string)75func (form formSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSet bool, err error) {76 return setByForm(value, field, form, tagValue, opt)77}7879func mappingByPtr(ptr any, setter setter, tag string) error {80 _, err := mapping(reflect.ValueOf(ptr), emptyField, setter, tag)81 return err82}8384func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {85 if field.Tag.Get(tag) == "-" { // just ignoring this field86 return false, nil87 }8889 vKind := value.Kind()9091 if vKind == reflect.Ptr {92 var isNew bool93 vPtr := value94 if value.IsNil() {95 isNew = true96 vPtr = reflect.New(value.Type().Elem())97 }98 isSet, err := mapping(vPtr.Elem(), field, setter, tag)99 if err != nil {100 return false, err101 }102 if isNew && isSet {103 value.Set(vPtr)104 }105 return isSet, nil106 }107108 if vKind != reflect.Struct || !field.Anonymous {109 ok, err := tryToSetValue(value, field, setter, tag)110 if err != nil {111 return false, err112 }113 if ok {114 return true, nil115 }116 }117118 if vKind == reflect.Struct {119 tValue := value.Type()120121 var isSet boolrefactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc
line 122why, one step back122 for i := range value.NumField() {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 123–140why, one step back123 sf := tValue.Field(i)124 if sf.PkgPath != "" && !sf.Anonymous { // unexported125 continue126 }127 ok, err := mapping(value.Field(i), sf, setter, tag)128 if err != nil {129 return false, err130 }131 isSet = isSet || ok132 }133 return isSet, nil134 }135 return false, nil136}137138type setOptions struct {139 isDefaultExists bool140 defaultValue stringfeat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 141–142why, one step back141 // parser specifies what interface to use for reading the request & default values (e.g. `encoding.TextUnmarshaler`)142 parser stringchore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 143–173why, one step back143}144145func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {146 var tagValue string147 var setOpt setOptions148149 tagValue = field.Tag.Get(tag)150 tagValue, opts := head(tagValue, ",")151152 if tagValue == "" { // default value is FieldName153 tagValue = field.Name154 }155 if tagValue == "" { // when field is "emptyField" variable156 return false, nil157 }158159 var opt string160 for len(opts) > 0 {161 opt, opts = head(opts, ",")162163 if k, v := head(opt, "="); k == "default" {164 setOpt.isDefaultExists = true165 setOpt.defaultValue = v166167 // convert semicolon-separated default values to csv-separated values for processing in setByForm168 if field.Type.Kind() == reflect.Slice || field.Type.Kind() == reflect.Array {169 cfTag := field.Tag.Get("collection_format")170 if cfTag == "" || cfTag == "multi" || cfTag == "csv" {171 setOpt.defaultValue = strings.ReplaceAll(v, ";", ",")172 }173 }feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 174–175why, one step back174 } else if k, v = head(opt, "="); k == "parser" {175 setOpt.parser = vchore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 176–183why, one step back176 }177 }178179 return setter.TrySet(value, field, tagValue, setOpt)180}181182// BindUnmarshaler is the interface used to wrap the UnmarshalParam method.183type BindUnmarshaler interface {docs: correct article usage in comments (#4301)Leon capbefore adoptionad4f436
line 184why, one step back184 // UnmarshalParam decodes and assigns a value from a form or query param.chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 185–198why, one step back185 UnmarshalParam(param string) error186}187188// trySetCustom tries to set a custom type value189// If the value implements the BindUnmarshaler interface, it will be used to set the value, we will return `true`190// to skip the default value setting.191func trySetCustom(val string, value reflect.Value) (isSet bool, err error) {192 switch v := value.Addr().Interface().(type) {193 case BindUnmarshaler:194 return true, v.UnmarshalParam(val)195 }196 return false, nil197}198feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 199–212why, one step back199// trySetUsingParser tries to set a custom type value based on the presence of the "parser" tag on the field.200// If the parser tag does not exist or does not match any of the supported parsers, gin will skip over this.201func trySetUsingParser(val string, value reflect.Value, parser string) (isSet bool, err error) {202 switch parser {203 case "encoding.TextUnmarshaler":204 v, ok := value.Addr().Interface().(encoding.TextUnmarshaler)205 if !ok {206 return false, nil207 }208 return true, v.UnmarshalText([]byte(val))209 }210 return false, nil211}212chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 213–229why, one step back213func trySplit(vs []string, field reflect.StructField) (newVs []string, err error) {214 cfTag := field.Tag.Get("collection_format")215 if cfTag == "" || cfTag == "multi" {216 return vs, nil217 }218219 var sep string220 switch cfTag {221 case "csv":222 sep = ","223 case "ssv":224 sep = " "225 case "tsv":226 sep = "\t"227 case "pipes":228 sep = "|"229 default:feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 230why, one step back230 return vs, fmt.Errorf("%s is not supported in the collection_format. (multi, csv, ssv, tsv, pipes)", cfTag)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 231–252why, one step back231 }232233 totalLength := 0234 for _, v := range vs {235 totalLength += strings.Count(v, sep) + 1236 }237 newVs = make([]string, 0, totalLength)238 for _, v := range vs {239 newVs = append(newVs, strings.Split(v, sep)...)240 }241242 return newVs, nil243}244245func setByForm(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions) (isSet bool, err error) {246 vs, ok := form[tagValue]247 if !ok && !opt.isDefaultExists {248 return false, nil249 }250251 switch value.Kind() {252 case reflect.Slice:fix(binding): improve empty slice/array handling in form binding (#4380)Namebefore adoptionc3d1092
lines 253–256why, one step back253 if len(vs) == 0 {254 if !opt.isDefaultExists {255 return false, nil256 }chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 257why, one step back257fix(binding): improve empty slice/array handling in form binding (#4380)Namebefore adoptionc3d1092
line 258why, one step back258 vs = []string{opt.defaultValue}chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 259–265why, one step back259 // pre-process the default value for multi if present260 cfTag := field.Tag.Get("collection_format")261 if cfTag == "" || cfTag == "multi" {262 vs = strings.Split(opt.defaultValue, ",")263 }264 }265feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 266why, one step back266 if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 267why, one step back267 return ok, errfeat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 268–269why, one step back268 } else if ok, err = trySetCustom(vs[0], value); ok {269 return ok, errchore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 270–275why, one step back270 }271272 if vs, err = trySplit(vs, field); err != nil {273 return false, err274 }275feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 276why, one step back276 return true, setSlice(vs, value, field, opt)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 277why, one step back277 case reflect.Array:fix(binding): improve empty slice/array handling in form binding (#4380)Namebefore adoptionc3d1092
lines 278–281why, one step back278 if len(vs) == 0 {279 if !opt.isDefaultExists {280 return false, nil281 }chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 282why, one step back282fix(binding): improve empty slice/array handling in form binding (#4380)Namebefore adoptionc3d1092
line 283why, one step back283 vs = []string{opt.defaultValue}chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 284–290why, one step back284 // pre-process the default value for multi if present285 cfTag := field.Tag.Get("collection_format")286 if cfTag == "" || cfTag == "multi" {287 vs = strings.Split(opt.defaultValue, ",")288 }289 }290feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 291why, one step back291 if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 292why, one step back292 return ok, errfeat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 293–294why, one step back293 } else if ok, err = trySetCustom(vs[0], value); ok {294 return ok, errchore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 295–304why, one step back295 }296297 if vs, err = trySplit(vs, field); err != nil {298 return false, err299 }300301 if len(vs) != value.Len() {302 return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())303 }304feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 305why, one step back305 return true, setArray(vs, value, field, opt)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 306–307why, one step back306 default:307 var val stringfeat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 308why, one step back308 if !ok || len(vs) == 0 || (len(vs) > 0 && vs[0] == "") {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 309why, one step back309 val = opt.defaultValuefeat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 310why, one step back310 } else if len(vs) > 0 {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 311–312why, one step back311 val = vs[0]312 }feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 313–314why, one step back313314 if ok, err = trySetUsingParser(val, value, opt.parser); ok {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 315why, one step back315 return ok, errfeat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 316–317why, one step back316 } else if ok, err = trySetCustom(val, value); ok {317 return ok, errchore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 318why, one step back318 }feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 319why, one step back319 return true, setWithProperType(val, value, field, opt)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 320–322why, one step back320 }321}322feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
lines 323–330why, one step back323func setWithProperType(val string, value reflect.Value, field reflect.StructField, opt setOptions) error {324 // this if-check is required for parsing nested types like []MyId, where MyId is [12]byte325 if ok, err := trySetUsingParser(val, value, opt.parser); ok {326 return err327 } else if ok, err = trySetCustom(val, value); ok {328 return err329 }330fix(binding): empty value error (#2169)guonaihongbefore adoptionb917b14
lines 331–335why, one step back331 // If it is a string type, no spaces are removed, and the user data is not modified here332 if value.Kind() != reflect.String {333 val = strings.TrimSpace(val)334 }335chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 336–375why, one step back336 switch value.Kind() {337 case reflect.Int:338 return setIntField(val, 0, value)339 case reflect.Int8:340 return setIntField(val, 8, value)341 case reflect.Int16:342 return setIntField(val, 16, value)343 case reflect.Int32:344 return setIntField(val, 32, value)345 case reflect.Int64:346 switch value.Interface().(type) {347 case time.Duration:348 return setTimeDuration(val, value)349 }350 return setIntField(val, 64, value)351 case reflect.Uint:352 return setUintField(val, 0, value)353 case reflect.Uint8:354 return setUintField(val, 8, value)355 case reflect.Uint16:356 return setUintField(val, 16, value)357 case reflect.Uint32:358 return setUintField(val, 32, value)359 case reflect.Uint64:360 return setUintField(val, 64, value)361 case reflect.Bool:362 return setBoolField(val, value)363 case reflect.Float32:364 return setFloatField(val, 32, value)365 case reflect.Float64:366 return setFloatField(val, 64, value)367 case reflect.String:368 value.SetString(val)369 case reflect.Struct:370 switch value.Interface().(type) {371 case time.Time:372 return setTimeField(val, field, value)373 case multipart.FileHeader:374 return nil375 }feat: support custom json codec at runtime (#3391)Timbefore adoption688a429
line 376why, one step back376 return json.API.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 377why, one step back377 case reflect.Map:feat: support custom json codec at runtime (#3391)Timbefore adoption688a429
line 378why, one step back378 return json.API.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 379–382why, one step back379 case reflect.Ptr:380 if !value.Elem().IsValid() {381 value.Set(reflect.New(value.Type().Elem()))382 }feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 383why, one step back383 return setWithProperType(val, value.Elem(), field, opt)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 384–439why, one step back384 default:385 return errUnknownType386 }387 return nil388}389390func setIntField(val string, bitSize int, field reflect.Value) error {391 if val == "" {392 val = "0"393 }394 intVal, err := strconv.ParseInt(val, 10, bitSize)395 if err == nil {396 field.SetInt(intVal)397 }398 return err399}400401func setUintField(val string, bitSize int, field reflect.Value) error {402 if val == "" {403 val = "0"404 }405 uintVal, err := strconv.ParseUint(val, 10, bitSize)406 if err == nil {407 field.SetUint(uintVal)408 }409 return err410}411412func setBoolField(val string, field reflect.Value) error {413 if val == "" {414 val = "false"415 }416 boolVal, err := strconv.ParseBool(val)417 if err == nil {418 field.SetBool(boolVal)419 }420 return err421}422423func setFloatField(val string, bitSize int, field reflect.Value) error {424 if val == "" {425 val = "0.0"426 }427 floatVal, err := strconv.ParseFloat(val, bitSize)428 if err == nil {429 field.SetFloat(floatVal)430 }431 return err432}433434func setTimeField(val string, structField reflect.StructField, value reflect.Value) error {435 timeFormat := structField.Tag.Get("time_format")436 if timeFormat == "" {437 timeFormat = time.RFC3339438 }439fix(binding): empty value error (#2169)guonaihongbefore adoptionb917b14
lines 440–444why, one step back440 if val == "" {441 value.Set(reflect.ValueOf(time.Time{}))442 return nil443 }444chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 445–464why, one step back445 switch tf := strings.ToLower(timeFormat); tf {446 case "unix", "unixmilli", "unixmicro", "unixnano":447 tv, err := strconv.ParseInt(val, 10, 64)448 if err != nil {449 return err450 }451452 var t time.Time453 switch tf {454 case "unix":455 t = time.Unix(tv, 0)456 case "unixmilli":457 t = time.UnixMilli(tv)458 case "unixmicro":459 t = time.UnixMicro(tv)460 default:461 t = time.Unix(0, tv)462 }463464 value.Set(reflect.ValueOf(t))Revert "fix(time): binding time with empty value (#4103)" (#4245)Bo-Yi Wubefore adoption8fb3136
lines 465–467why, one step back465 return nil466 }467chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 468–489why, one step back468 l := time.Local469 if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {470 l = time.UTC471 }472473 if locTag := structField.Tag.Get("time_location"); locTag != "" {474 loc, err := time.LoadLocation(locTag)475 if err != nil {476 return err477 }478 l = loc479 }480481 t, err := time.ParseInLocation(timeFormat, val, l)482 if err != nil {483 return err484 }485486 value.Set(reflect.ValueOf(t))487 return nil488}489feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 490why, one step back490func setArray(vals []string, value reflect.Value, field reflect.StructField, opt setOptions) error {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 491why, one step back491 for i, s := range vals {feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 492why, one step back492 err := setWithProperType(s, value.Index(i), field, opt)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 493–499why, one step back493 if err != nil {494 return err495 }496 }497 return nil498}499feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 500why, one step back500func setSlice(vals []string, value reflect.Value, field reflect.StructField, opt setOptions) error {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 501why, one step back501 slice := reflect.MakeSlice(value.Type(), len(vals), len(vals))feat(binding): add support for encoding.UnmarshalText in uri/query binding (#4203)takanuva15before adoption192ac89
line 502why, one step back502 err := setArray(vals, slice, field, opt)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 503–510why, one step back503 if err != nil {504 return err505 }506 value.Set(slice)507 return nil508}509510func setTimeDuration(val string, value reflect.Value) error {fix(binding): empty value error (#2169)guonaihongbefore adoptionb917b14
lines 511–514why, one step back511 if val == "" {512 val = "0"513 }514chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 515–535why, one step back515 d, err := time.ParseDuration(val)516 if err != nil {517 return err518 }519 value.Set(reflect.ValueOf(d))520 return nil521}522523func head(str, sep string) (head string, tail string) {524 head, tail, _ = strings.Cut(str, sep)525 return head, tail526}527528func setFormMap(ptr any, form map[string][]string) error {529 el := reflect.TypeOf(ptr).Elem()530531 if el.Kind() == reflect.Slice {532 ptrMap, ok := ptr.(map[string][]string)533 if !ok {534 return ErrConvertMapStringSlice535 }refactor(binding): use maps.Copy for cleaner map handling (#4352)russcossbefore adoption39858a0
line 536why, one step back536 maps.Copy(ptrMap, form)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 537–550why, one step back537538 return nil539 }540541 ptrMap, ok := ptr.(map[string]string)542 if !ok {543 return ErrConvertToMapString544 }545 for k, v := range form {546 ptrMap[k] = v[len(v)-1] // pick last547 }548549 return nil550}