Decodes an unknown data type into a specific reflection value.
(name string, input interface{}, outVal reflect.Value)
| 419 | |
| 420 | // Decodes an unknown data type into a specific reflection value. |
| 421 | func (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) |
no test coverage detected