Since a map value is not settable via reflection, this function always creates a new element for each corresponding map key. Existing values of v are overwritten. This happens even if the map value is something like a pointer to a struct, where we could in theory populate the existing struct value i
(v reflect.Value, d Decoder)
| 593 | // we could in theory populate the existing struct value instead of discarding it. |
| 594 | // This behavior matches encoding/json. |
| 595 | func decodeMap(v reflect.Value, d Decoder) error { |
| 596 | mapLen, ok := d.MapLen() |
| 597 | if !ok { |
| 598 | return decodingError(v, d) |
| 599 | } |
| 600 | t := v.Type() |
| 601 | if v.IsNil() { |
| 602 | v.Set(reflect.MakeMapWithSize(t, mapLen)) |
| 603 | } |
| 604 | et := t.Elem() |
| 605 | var err error |
| 606 | kt := v.Type().Key() |
| 607 | d.DecodeMap(func(key string, vd Decoder, _ bool) bool { |
| 608 | if err != nil { |
| 609 | return false |
| 610 | } |
| 611 | el := reflect.New(et).Elem() |
| 612 | err = decode(el, vd) |
| 613 | if err != nil { |
| 614 | return false |
| 615 | } |
| 616 | vk, e := unstringifyMapKey(key, kt) |
| 617 | if e != nil { |
| 618 | err = e |
| 619 | return false |
| 620 | } |
| 621 | v.SetMapIndex(vk, el) |
| 622 | return err == nil |
| 623 | }) |
| 624 | return err |
| 625 | } |
| 626 | |
| 627 | // Given a map key encoded as a string, and the type of the map key, convert the key |
| 628 | // into the type. |
no test coverage detected