-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
79 lines (66 loc) · 1.72 KB
/
map.go
File metadata and controls
79 lines (66 loc) · 1.72 KB
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
// Package orderedmap provides a Map that preserves the order of key value pairs.
package orderedmap
import (
"bytes"
"encoding/json"
"fmt"
"sort"
)
// Map implements a map that keeps the item order.
type Map struct {
// Data holds the map entries in unsorted order.
Data map[string]Entry
// Keys contains the map keys in sorted order.
Keys []string
}
// Len returns the number of elements within the map.
func (m *Map) Len() int {
return len(m.Keys)
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (m *Map) Range(f func(key string, value any) bool) {
for _, key := range m.Keys {
entry := m.Data[key]
if !f(key, entry.Value) {
return
}
}
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (m *Map) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &m.Data); err != nil {
return fmt.Errorf("failed to unmarshal orderedmap: %w", err)
}
m.rebuildKeys()
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (m *Map) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("{")
for i, key := range m.Keys {
buf.WriteString(fmt.Sprintf("%q:", key))
value := m.Data[key].Value
b, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("marshalling entry: %w", err)
}
buf.Write(b)
if i < len(m.Keys)-1 {
buf.WriteString(",")
}
}
buf.WriteString("}")
return buf.Bytes(), nil
}
// rebuildKeys build the sorted keys slice.
func (m *Map) rebuildKeys() {
m.Keys = []string{}
for name := range m.Data {
m.Keys = append(m.Keys, name)
}
sort.SliceStable(m.Keys, func(i, j int) bool {
return m.Data[m.Keys[i]].index < m.Data[m.Keys[j]].index
})
}