MCPcopy Create free account
hub / github.com/google/go-cloud / decodeMap

Function decodeMap

docstore/driver/codec.go:595–625  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

593// we could in theory populate the existing struct value instead of discarding it.
594// This behavior matches encoding/json.
595func 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.

Callers 2

DecodeMethod · 0.85
decodeFunction · 0.85

Calls 8

decodingErrorFunction · 0.85
unstringifyMapKeyFunction · 0.85
decodeFunction · 0.70
MapLenMethod · 0.65
KeyMethod · 0.65
DecodeMapMethod · 0.65
TypeMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected