Set a key value pair. It is O(n) over the size of Metadata and allocates.
(key string, val string)
| 165 | // |
| 166 | // It is O(n) over the size of Metadata and allocates. |
| 167 | func (m *Metadata) Set(key string, val string) { |
| 168 | var source strings.Builder |
| 169 | inserted := false |
| 170 | for prevKey, prevVal := range m.Iter() { |
| 171 | if inserted { |
| 172 | writeMetadataKV(&source, prevKey, prevVal) |
| 173 | continue |
| 174 | } |
| 175 | |
| 176 | switch strings.Compare(prevKey, key) { |
| 177 | case -1: // prevKey < key |
| 178 | writeMetadataKV(&source, prevKey, prevVal) |
| 179 | case 0: // prevKey == key, key replaces prevKey |
| 180 | writeMetadataKV(&source, key, val) |
| 181 | inserted = true |
| 182 | case 1: // prevKey > key, insert key before it |
| 183 | writeMetadataKV(&source, key, val) |
| 184 | writeMetadataKV(&source, prevKey, prevVal) |
| 185 | inserted = true |
| 186 | } |
| 187 | } |
| 188 | if !inserted { |
| 189 | writeMetadataKV(&source, key, val) |
| 190 | } |
| 191 | m.source = source.String() |
| 192 | } |
| 193 | |
| 194 | func writeMetadataKV(into *strings.Builder, key, val string) { |
| 195 | _ = into.WriteByte(metadataSeparator) |