MakeMap examines the key type from a Map and create a map with builtin type if possible. The idea is to avoid generating map[any]any, which cannot be handled by json.Marshal.
(raw map[any]any)
| 596 | // if possible. The idea is to avoid generating map[any]any, |
| 597 | // which cannot be handled by json.Marshal. |
| 598 | func (m *Map) MakeMap(raw map[any]any) any { |
| 599 | ma := reflect.MakeMap(toReflectType(m)) |
| 600 | keys := make([]any, 0, len(raw)) |
| 601 | for key := range raw { |
| 602 | keys = append(keys, key) |
| 603 | } |
| 604 | sort.Slice(keys, func(i, j int) bool { |
| 605 | return reflect.ValueOf(keys[i]).String() < reflect.ValueOf(keys[j]).String() |
| 606 | }) |
| 607 | for _, key := range keys { |
| 608 | ma.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(raw[key])) |
| 609 | } |
| 610 | return ma.Interface() |
| 611 | } |
| 612 | |
| 613 | // ToMap converts a MapVal to a map. |
| 614 | func (m MapVal) ToMap() map[any]any { |
no test coverage detected