-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast_nodes_gen.go
214 lines (170 loc) · 3.63 KB
/
ast_nodes_gen.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
// Autogenerated code!
// DO NOT EDIT!
//
// Generated by OrderedMap generator from the internal/cmd/generator command.
package jschema
import (
"bytes"
"encoding/json"
)
// Set sets a value with specified key.
func (m *ASTNodes) Set(k string, v ASTNode) {
m.mx.Lock()
defer m.mx.Unlock()
if m.data == nil {
m.data = map[string]ASTNode{}
}
if !m.has(k) {
m.order = append(m.order, k)
}
m.data[k] = v
}
// Update updates a value with specified key.
func (m *ASTNodes) Update(k string, fn func(v ASTNode) ASTNode) {
m.mx.Lock()
defer m.mx.Unlock()
if !m.has(k) {
// Prevent from possible nil pointer dereference if map value type is a
// pointer.
return
}
m.data[k] = fn(m.data[k])
}
// GetValue gets a value by key.
func (m *ASTNodes) GetValue(k string) ASTNode {
m.mx.RLock()
defer m.mx.RUnlock()
return m.data[k]
}
// Get gets a value by key.
func (m *ASTNodes) Get(k string) (ASTNode, bool) {
m.mx.RLock()
defer m.mx.RUnlock()
v, ok := m.data[k]
return v, ok
}
// Has checks that specified key is set.
func (m *ASTNodes) Has(k string) bool {
m.mx.RLock()
defer m.mx.RUnlock()
return m.has(k)
}
func (m *ASTNodes) has(k string) bool {
_, ok := m.data[k]
return ok
}
// Len returns count of values.
func (m *ASTNodes) Len() int {
m.mx.RLock()
defer m.mx.RUnlock()
return len(m.data)
}
func (m *ASTNodes) Delete(k string) {
m.mx.Lock()
defer m.mx.Unlock()
m.delete(k)
}
func (m *ASTNodes) delete(k string) {
var kk string
i := -1
for i, kk = range m.order {
if kk == k {
break
}
}
delete(m.data, k)
if i != -1 {
m.order = append(m.order[:i], m.order[i+1:]...)
}
}
// Filter iterates and changes values in the map.
func (m *ASTNodes) Filter(fn filterASTNodesFunc) {
m.mx.Lock()
defer m.mx.Unlock()
for _, k := range m.order {
if !fn(k, m.data[k]) {
m.delete(k)
}
}
}
type filterASTNodesFunc = func(k string, v ASTNode) bool
// Find finds first matched item from the map.
func (m *ASTNodes) Find(fn findASTNodesFunc) (ASTNodesItem, bool) {
m.mx.RLock()
defer m.mx.RUnlock()
for _, k := range m.order {
if fn(k, m.data[k]) {
return ASTNodesItem{
Key: k,
Value: m.data[k],
}, true
}
}
return ASTNodesItem{}, false
}
type findASTNodesFunc = func(k string, v ASTNode) bool
func (m *ASTNodes) Each(fn eachASTNodesFunc) error {
m.mx.RLock()
defer m.mx.RUnlock()
for _, k := range m.order {
if err := fn(k, m.data[k]); err != nil {
return err
}
}
return nil
}
type eachASTNodesFunc = func(k string, v ASTNode) error
func (m *ASTNodes) EachSafe(fn eachSafeASTNodesFunc) {
m.mx.RLock()
defer m.mx.RUnlock()
for _, k := range m.order {
fn(k, m.data[k])
}
}
type eachSafeASTNodesFunc = func(k string, v ASTNode)
// Map iterates and changes values in the map.
func (m *ASTNodes) Map(fn mapASTNodesFunc) error {
m.mx.Lock()
defer m.mx.Unlock()
for _, k := range m.order {
v, err := fn(k, m.data[k])
if err != nil {
return err
}
m.data[k] = v
}
return nil
}
type mapASTNodesFunc = func(k string, v ASTNode) (ASTNode, error)
// ASTNodesItem represent single data from the ASTNodes.
type ASTNodesItem struct {
Key string
Value ASTNode
}
var _ json.Marshaler = &ASTNodes{}
func (m *ASTNodes) MarshalJSON() ([]byte, error) {
m.mx.RLock()
defer m.mx.RUnlock()
var buf bytes.Buffer
buf.WriteRune('{')
for i, k := range m.order {
if i != 0 {
buf.WriteRune(',')
}
// marshal key
key, err := json.Marshal(k)
if err != nil {
return nil, err
}
buf.Write(key)
buf.WriteRune(':')
// marshal value
val, err := json.Marshal(m.data[k])
if err != nil {
return nil, err
}
buf.Write(val)
}
buf.WriteRune('}')
return buf.Bytes(), nil
}