MCPcopy
hub / github.com/mitchellh/mapstructure / decode

Method decode

mapstructure.go:421–505  ·  view source on GitHub ↗

Decodes an unknown data type into a specific reflection value.

(name string, input interface{}, outVal reflect.Value)

Source from the content-addressed store, hash-verified

419
420// Decodes an unknown data type into a specific reflection value.
421func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error {
422 var inputVal reflect.Value
423 if input != nil {
424 inputVal = reflect.ValueOf(input)
425
426 // We need to check here if input is a typed nil. Typed nils won't
427 // match the "input == nil" below so we check that here.
428 if inputVal.Kind() == reflect.Ptr && inputVal.IsNil() {
429 input = nil
430 }
431 }
432
433 if input == nil {
434 // If the data is nil, then we don't set anything, unless ZeroFields is set
435 // to true.
436 if d.config.ZeroFields {
437 outVal.Set(reflect.Zero(outVal.Type()))
438
439 if d.config.Metadata != nil && name != "" {
440 d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
441 }
442 }
443 return nil
444 }
445
446 if !inputVal.IsValid() {
447 // If the input value is invalid, then we just set the value
448 // to be the zero value.
449 outVal.Set(reflect.Zero(outVal.Type()))
450 if d.config.Metadata != nil && name != "" {
451 d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
452 }
453 return nil
454 }
455
456 if d.config.DecodeHook != nil {
457 // We have a DecodeHook, so let's pre-process the input.
458 var err error
459 input, err = DecodeHookExec(d.config.DecodeHook, inputVal, outVal)
460 if err != nil {
461 return fmt.Errorf("error decoding '%s': %s", name, err)
462 }
463 }
464
465 var err error
466 outputKind := getKind(outVal)
467 addMetaKey := true
468 switch outputKind {
469 case reflect.Bool:
470 err = d.decodeBool(name, input, outVal)
471 case reflect.Interface:
472 err = d.decodeBasic(name, input, outVal)
473 case reflect.String:
474 err = d.decodeString(name, input, outVal)
475 case reflect.Int:
476 err = d.decodeInt(name, input, outVal)
477 case reflect.Uint:
478 err = d.decodeUint(name, input, outVal)

Callers 9

DecodeMethod · 0.95
decodeBasicMethod · 0.95
decodeMapFromSliceMethod · 0.95
decodeMapFromMapMethod · 0.95
decodeMapFromStructMethod · 0.95
decodePtrMethod · 0.95
decodeSliceMethod · 0.95
decodeArrayMethod · 0.95
decodeStructFromMapMethod · 0.95

Calls 14

decodeBoolMethod · 0.95
decodeBasicMethod · 0.95
decodeStringMethod · 0.95
decodeIntMethod · 0.95
decodeUintMethod · 0.95
decodeFloatMethod · 0.95
decodeStructMethod · 0.95
decodeMapMethod · 0.95
decodePtrMethod · 0.95
decodeSliceMethod · 0.95
decodeArrayMethod · 0.95
decodeFuncMethod · 0.95

Tested by

no test coverage detected