DecodeTypedMap decodes a typed map. Typed map is a map that has a fixed type for keys and values. Key and value types may be different.
()
| 206 | // DecodeTypedMap decodes a typed map. Typed map is a map that has a fixed type for keys and values. |
| 207 | // Key and value types may be different. |
| 208 | func (d *Decoder) DecodeTypedMap() (interface{}, error) { |
| 209 | n, err := d.DecodeMapLen() |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | if n <= 0 { |
| 214 | return nil, nil |
| 215 | } |
| 216 | |
| 217 | key, err := d.decodeInterfaceCond() |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | |
| 222 | value, err := d.decodeInterfaceCond() |
| 223 | if err != nil { |
| 224 | return nil, err |
| 225 | } |
| 226 | |
| 227 | keyType := reflect.TypeOf(key) |
| 228 | valueType := reflect.TypeOf(value) |
| 229 | |
| 230 | if !keyType.Comparable() { |
| 231 | return nil, fmt.Errorf("msgpack: unsupported map key: %s", keyType.String()) |
| 232 | } |
| 233 | |
| 234 | mapType := reflect.MapOf(keyType, valueType) |
| 235 | |
| 236 | ln := n |
| 237 | if d.flags&disableAllocLimitFlag == 0 { |
| 238 | ln = min(ln, maxMapSize) |
| 239 | } |
| 240 | |
| 241 | mapValue := reflect.MakeMapWithSize(mapType, ln) |
| 242 | mapValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value)) |
| 243 | |
| 244 | n-- |
| 245 | if err := d.decodeTypedMapValue(mapValue, n); err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | |
| 249 | return mapValue.Interface(), nil |
| 250 | } |
| 251 | |
| 252 | func (d *Decoder) decodeTypedMapValue(v reflect.Value, n int) error { |
| 253 | var ( |