-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderedmap.go
393 lines (358 loc) · 9.74 KB
/
orderedmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// The MIT License (MIT)
//
// Copyright (c) 2023 Cypherpunk LLC and contributors
// Copyright (c) 2017 Ian Coleman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, Subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or Substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package orderedmap
import (
"bytes"
"encoding/json"
"fmt"
"sort"
)
type pair struct {
key string
value any
}
func (kv *pair) Key() string {
return kv.key
}
func (kv *pair) Value() any {
return kv.value
}
type byPair struct {
Pairs []*pair
LessFunc func(a *pair, j *pair) bool
}
func (a byPair) Len() int { return len(a.Pairs) }
func (a byPair) Swap(i, j int) { a.Pairs[i], a.Pairs[j] = a.Pairs[j], a.Pairs[i] }
func (a byPair) Less(i, j int) bool { return a.LessFunc(a.Pairs[i], a.Pairs[j]) }
type OrderedMap struct {
keys []string
values map[string]any
}
func New() *OrderedMap {
o := OrderedMap{}
o.keys = []string{}
o.values = map[string]any{}
return &o
}
func (o *OrderedMap) Get(key string) any {
return o.values[key]
}
func (o *OrderedMap) Set(key string, value any) {
_, ok := o.values[key]
if !ok {
o.keys = append(o.keys, key)
}
o.values[key] = value
}
func (o *OrderedMap) Delete(key string) {
// check key is in use
_, ok := o.values[key]
if !ok {
return
}
// remove from keys
for i, k := range o.keys {
if k == key {
o.keys = append(o.keys[:i], o.keys[i+1:]...)
break
}
}
// remove from values
delete(o.values, key)
}
func (o *OrderedMap) Keys() []string {
return o.keys
}
func (o *OrderedMap) Values() []any {
v := make([]any, len(o.keys))
for i, k := range o.keys {
v[i] = o.values[k]
}
return v
}
func (o *OrderedMap) KeysValues() map[string]any {
return o.values
}
func (o *OrderedMap) Len() int {
return len(o.keys)
}
func (o *OrderedMap) GetValueAt(pos int) any {
k := o.keys[pos]
return o.values[k]
}
func (o *OrderedMap) GetKeyAt(pos int) string {
return o.keys[pos]
}
// SortKeys sorts the map keys using the provided sort func.
func (o *OrderedMap) SortKeys(sortFunc func(keys []string)) {
sortFunc(o.keys)
}
// Sort sorts the map using the provided less func.
func (o *OrderedMap) Sort(lessFunc func(a *pair, b *pair) bool) {
pairs := make([]*pair, len(o.keys))
for i, key := range o.keys {
pairs[i] = &pair{key, o.values[key]}
}
sort.Sort(byPair{pairs, lessFunc})
for i, pair := range pairs {
o.keys[i] = pair.key
}
}
// MarshalJSON must return no duplicates, and should since orderedMap keys are
// unique.
func (o OrderedMap) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('{')
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
for i, k := range o.keys {
if i > 0 {
buf.WriteByte(',')
}
// add key
if err := encoder.Encode(k); err != nil {
return nil, err
}
buf.WriteByte(':')
// add value
if err := encoder.Encode(o.values[k]); err != nil {
return nil, err
}
}
buf.WriteByte('}')
return buf.Bytes(), nil
}
func (o *OrderedMap) UnmarshalJSON(b []byte) error {
err := CheckDuplicate(json.NewDecoder(bytes.NewReader(b)))
if err != nil {
return err
}
if o.values == nil {
o.values = map[string]any{}
}
err = json.Unmarshal(b, &o.values)
if err != nil {
return err
}
dec := json.NewDecoder(bytes.NewReader(b))
if _, err = dec.Token(); err != nil { // skip '{'
return err
}
o.keys = make([]string, 0, len(o.values))
return decode(dec, o)
}
// decodeOrderedMap
func decode(dec *json.Decoder, o *OrderedMap) error {
hasKey := make(map[string]bool, len(o.values))
for {
token, err := dec.Token()
if err != nil {
return err
}
if delim, ok := token.(json.Delim); ok && delim == '}' {
return nil
}
key := token.(string)
if hasKey[key] {
// duplicate key
for j, k := range o.keys {
if k == key {
copy(o.keys[j:], o.keys[j+1:])
break
}
}
o.keys[len(o.keys)-1] = key
} else {
hasKey[key] = true
o.keys = append(o.keys, key)
}
token, err = dec.Token()
if err != nil {
return err
}
if delim, ok := token.(json.Delim); ok {
switch delim {
case '{':
if values, ok := o.values[key].(map[string]any); ok {
newMap := OrderedMap{
keys: make([]string, 0, len(values)),
values: values,
}
if err = decode(dec, &newMap); err != nil {
return err
}
o.values[key] = newMap
} else if oldMap, ok := o.values[key].(OrderedMap); ok {
newMap := OrderedMap{
keys: make([]string, 0, len(oldMap.values)),
values: oldMap.values,
}
if err = decode(dec, &newMap); err != nil {
return err
}
o.values[key] = newMap
} else if err = decode(dec, &OrderedMap{}); err != nil {
return err
}
case '[':
if values, ok := o.values[key].([]any); ok {
if err = decodeSlice(dec, values); err != nil {
return err
}
} else if err = decodeSlice(dec, []any{}); err != nil {
return err
}
}
}
}
}
func decodeSlice(dec *json.Decoder, s []any) error {
for index := 0; ; index++ {
token, err := dec.Token()
if err != nil {
return err
}
if delim, ok := token.(json.Delim); ok {
switch delim {
case '{':
if index < len(s) {
if values, ok := s[index].(map[string]any); ok {
newMap := OrderedMap{
keys: make([]string, 0, len(values)),
values: values,
}
if err = decode(dec, &newMap); err != nil {
return err
}
s[index] = newMap
} else if oldMap, ok := s[index].(OrderedMap); ok {
newMap := OrderedMap{
keys: make([]string, 0, len(oldMap.values)),
values: oldMap.values,
}
if err = decode(dec, &newMap); err != nil {
return err
}
s[index] = newMap
} else if err = decode(dec, &OrderedMap{}); err != nil {
return err
}
} else if err = decode(dec, &OrderedMap{}); err != nil {
return err
}
case '[':
if index < len(s) {
if values, ok := s[index].([]any); ok {
if err = decodeSlice(dec, values); err != nil {
return err
}
} else if err = decodeSlice(dec, []any{}); err != nil {
return err
}
} else if err = decodeSlice(dec, []any{}); err != nil {
return err
}
case ']':
return nil
}
}
}
}
// ErrJSONDuplicate allows applications to check for JSON duplicate error.
type ErrJSONDuplicate error
// CheckDuplicate checks for JSON duplicates on ingest (unmarshal). Note that
// Go maps and structs and Javascript objects (ES6) already require unique
// JSON names. See the Coze FAQ on duplicates.
//
// Duplicate JSON fields are a security issue that wasn't addressed by the
// original spec that results in surprising behavior and is a source of bugs.
// See the article, "[An Exploration of JSON Interoperability
// Vulnerabilities](https://bishopfox.com/blog/json-interoperability-vulnerabilities)"
// and control-f "duplicate".
//
// Until Go releases the planned revision to the JSON package (See
// https://github.com/go-json-experiment/json), or adds support for erroring on
// duplicates to the current package, this function is needed.
//
// After JSON was widely adopted, Douglas Crockford (JSON's inventor), tried to
// fix this by updating JSON to define "must error on duplicates" as the correct
// behavior, but it was decided it was too late
// (https://esdiscuss.org/topic/json-duplicate-keys).
//
// Although Douglas Crockford couldn't change the JSON spec to force
// implementations to error on duplicate, his Java JSON implementation errors on
// duplicates. Others implementations behaviors are `last-value-wins`, support
// duplicate keys, or other non-standard behavior. The [JSON
// RFC](https://datatracker.ietf.org/doc/html/rfc8259#section-4) states that
// implementations should not allow duplicate keys. It then notes the varying behavior
// of existing implementations.
//
// Disallowing duplicates conforms to the small I-JSON RFC. The author of
// I-JSON, Tim Bray, is also the author of current JSON specification (RFC
// 8259). See also https://github.com/json5/json5-spec/issues/38.
func CheckDuplicate(d *json.Decoder) error {
t, err := d.Token()
if err != nil {
return err
}
// Is it a delimiter?
delim, ok := t.(json.Delim)
if !ok {
return nil // scaler type, nothing to do
}
switch delim {
case '{':
keys := make(map[string]bool)
for d.More() {
t, err := d.Token() // Get field key.
if err != nil {
return err
}
key := t.(string)
if keys[key] { // Check for duplicates.
return ErrJSONDuplicate(fmt.Errorf("Coze: JSON duplicate field %q", key))
}
keys[key] = true
// Recursive, Check value in case value is object.
err = CheckDuplicate(d)
if err != nil {
return err
}
}
// consume trailing }
if _, err := d.Token(); err != nil {
return err
}
case '[':
for d.More() {
if err := CheckDuplicate(d); err != nil {
return err
}
}
// consume trailing ]
if _, err := d.Token(); err != nil {
return err
}
}
return nil
}