MCPcopy
hub / github.com/gin-gonic/gin / setByForm

Function setByForm

binding/form_mapping.go:245–321  ·  view source on GitHub ↗
(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions)

Source from the content-addressed store, hash-verified

243}
244
245func 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, nil
249 }
250
251 switch value.Kind() {
252 case reflect.Slice:
253 if len(vs) == 0 {
254 if !opt.isDefaultExists {
255 return false, nil
256 }
257
258 vs = []string{opt.defaultValue}
259 // pre-process the default value for multi if present
260 cfTag := field.Tag.Get("collection_format")
261 if cfTag == "" || cfTag == "multi" {
262 vs = strings.Split(opt.defaultValue, ",")
263 }
264 }
265
266 if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
267 return ok, err
268 } else if ok, err = trySetCustom(vs[0], value); ok {
269 return ok, err
270 }
271
272 if vs, err = trySplit(vs, field); err != nil {
273 return false, err
274 }
275
276 return true, setSlice(vs, value, field, opt)
277 case reflect.Array:
278 if len(vs) == 0 {
279 if !opt.isDefaultExists {
280 return false, nil
281 }
282
283 vs = []string{opt.defaultValue}
284 // pre-process the default value for multi if present
285 cfTag := field.Tag.Get("collection_format")
286 if cfTag == "" || cfTag == "multi" {
287 vs = strings.Split(opt.defaultValue, ",")
288 }
289 }
290
291 if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
292 return ok, err
293 } else if ok, err = trySetCustom(vs[0], value); ok {
294 return ok, err
295 }
296
297 if vs, err = trySplit(vs, field); err != nil {
298 return false, err
299 }
300
301 if len(vs) != value.Len() {
302 return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())

Callers 3

TrySetMethod · 0.85
TrySetMethod · 0.85
TrySetMethod · 0.85

Calls 9

trySetUsingParserFunction · 0.85
trySetCustomFunction · 0.85
trySplitFunction · 0.85
setSliceFunction · 0.85
setArrayFunction · 0.85
setWithProperTypeFunction · 0.85
TypeMethod · 0.80
StringMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected