| 563 | } |
| 564 | |
| 565 | func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error { |
| 566 | dataVal := reflect.Indirect(reflect.ValueOf(data)) |
| 567 | dataKind := getKind(dataVal) |
| 568 | |
| 569 | converted := true |
| 570 | switch { |
| 571 | case dataKind == reflect.String: |
| 572 | val.SetString(dataVal.String()) |
| 573 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: |
| 574 | if dataVal.Bool() { |
| 575 | val.SetString("1") |
| 576 | } else { |
| 577 | val.SetString("0") |
| 578 | } |
| 579 | case dataKind == reflect.Int && d.config.WeaklyTypedInput: |
| 580 | val.SetString(strconv.FormatInt(dataVal.Int(), 10)) |
| 581 | case dataKind == reflect.Uint && d.config.WeaklyTypedInput: |
| 582 | val.SetString(strconv.FormatUint(dataVal.Uint(), 10)) |
| 583 | case dataKind == reflect.Float32 && d.config.WeaklyTypedInput: |
| 584 | val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64)) |
| 585 | case dataKind == reflect.Slice && d.config.WeaklyTypedInput, |
| 586 | dataKind == reflect.Array && d.config.WeaklyTypedInput: |
| 587 | dataType := dataVal.Type() |
| 588 | elemKind := dataType.Elem().Kind() |
| 589 | switch elemKind { |
| 590 | case reflect.Uint8: |
| 591 | var uints []uint8 |
| 592 | if dataKind == reflect.Array { |
| 593 | uints = make([]uint8, dataVal.Len(), dataVal.Len()) |
| 594 | for i := range uints { |
| 595 | uints[i] = dataVal.Index(i).Interface().(uint8) |
| 596 | } |
| 597 | } else { |
| 598 | uints = dataVal.Interface().([]uint8) |
| 599 | } |
| 600 | val.SetString(string(uints)) |
| 601 | default: |
| 602 | converted = false |
| 603 | } |
| 604 | default: |
| 605 | converted = false |
| 606 | } |
| 607 | |
| 608 | if !converted { |
| 609 | return fmt.Errorf( |
| 610 | "'%s' expected type '%s', got unconvertible type '%s', value: '%v'", |
| 611 | name, val.Type(), dataVal.Type(), data) |
| 612 | } |
| 613 | |
| 614 | return nil |
| 615 | } |
| 616 | |
| 617 | func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error { |
| 618 | dataVal := reflect.Indirect(reflect.ValueOf(data)) |