WriteMap writes a map to the provided Writer. The writeKey and writeVal parameters specify the functions to use to write each key and value of the map.
(w *Writer, m map[K]V, writeKey func(K) error, writeVal func(V) error)
| 109 | // The writeKey and writeVal parameters specify the functions |
| 110 | // to use to write each key and value of the map. |
| 111 | func WriteMap[K comparable, V any](w *Writer, m map[K]V, writeKey func(K) error, writeVal func(V) error) error { |
| 112 | if m == nil { |
| 113 | return w.WriteNil() |
| 114 | } |
| 115 | if uint64(len(m)) > math.MaxUint32 { |
| 116 | return fmt.Errorf("map too large to encode: %d elements", len(m)) |
| 117 | } |
| 118 | |
| 119 | // Write map header |
| 120 | err := w.WriteMapHeader(uint32(len(m))) |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | // Write elements |
| 125 | for k, v := range m { |
| 126 | err = writeKey(k) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | err = writeVal(v) |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | } |
| 135 | return nil |
| 136 | } |
| 137 | |
| 138 | // WriteMapSorted writes a map to the provided Writer. |
| 139 | // The keys of the map are sorted before writing. |
searching dependent graphs…