ReadMap returns an iterator that can be used to iterate over the elements of a map in the MessagePack data while being read by the provided Reader. The type parameters K and V specify the types of the keys and values in the map. The returned iterator implements the iter.Seq2[K, V] interface, allowin
(m *Reader, readKey func() (K, error), readVal func() (V, error))
| 72 | // The returned function can be used to read any error that |
| 73 | // occurred during iteration when iteration is done. |
| 74 | func ReadMap[K, V any](m *Reader, readKey func() (K, error), readVal func() (V, error)) (iter.Seq2[K, V], func() error) { |
| 75 | var err error |
| 76 | return func(yield func(K, V) bool) { |
| 77 | var sz uint32 |
| 78 | if m.IsNil() { |
| 79 | err = m.ReadNil() |
| 80 | return |
| 81 | } |
| 82 | sz, err = m.ReadMapHeader() |
| 83 | if err != nil { |
| 84 | err = fmt.Errorf("cannot read map header: %w", err) |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | for range sz { |
| 89 | var k K |
| 90 | k, err = readKey() |
| 91 | if err != nil { |
| 92 | err = fmt.Errorf("cannot read key: %w", err) |
| 93 | return |
| 94 | } |
| 95 | var v V |
| 96 | v, err = readVal() |
| 97 | if err != nil { |
| 98 | err = fmt.Errorf("cannot read value: %w", err) |
| 99 | return |
| 100 | } |
| 101 | if !yield(k, v) { |
| 102 | return |
| 103 | } |
| 104 | } |
| 105 | }, func() error { return err } |
| 106 | } |
| 107 | |
| 108 | // WriteMap writes a map to the provided Writer. |
| 109 | // The writeKey and writeVal parameters specify the functions |
searching dependent graphs…