-
Notifications
You must be signed in to change notification settings - Fork 18
/
changer.go
342 lines (302 loc) · 7.58 KB
/
changer.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
// Tideland Go Library - Collections - Changer
//
// Copyright (C) 2015 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package collections
//--------------------
// IMPORTS
//--------------------
import (
"github.com/tideland/golib/errors"
)
//--------------------
// CHANGER
//--------------------
// changer implements the Changer interface.
type changer struct {
node *node
err error
}
// Value implements the Changer interface.
func (c *changer) Value() (interface{}, error) {
if c.err != nil {
return nil, c.err
}
return c.node.content.value(), nil
}
// SetValue implements the Changer interface.
func (c *changer) SetValue(v interface{}) (interface{}, error) {
if c.err != nil {
return nil, c.err
}
oldValue := c.node.content.value()
newValue := justValue{v}
if !c.node.isAllowed(newValue, true) {
return nil, errors.New(ErrDuplicate, errorMessages)
}
c.node.content = newValue
return oldValue, nil
}
// Add implements the Changer interface.
func (c *changer) Add(v interface{}) error {
if c.err != nil {
return c.err
}
_, err := c.node.addChild(justValue{v})
return err
}
// Remove implements the Changer interface.
func (c *changer) Remove() error {
if c.err != nil {
return c.err
}
return c.node.remove()
}
// List implements the Changer interface.
func (c *changer) List() ([]interface{}, error) {
if c.err != nil {
return nil, c.err
}
var list []interface{}
err := c.node.doChildren(func(cn *node) error {
list = append(list, cn.content.value())
return nil
})
if err != nil {
return nil, err
}
return list, nil
}
// Error implements the Changer interface.
func (c *changer) Error() error {
return c.err
}
//--------------------
// STRING CHANGER
//--------------------
// stringChanger implements the StringChanger interface.
type stringChanger struct {
node *node
err error
}
// Value implements the StringChanger interface.
func (c *stringChanger) Value() (string, error) {
if c.err != nil {
return "", c.err
}
if c.node.content.value() == nil {
return "", nil
}
return c.node.content.value().(string), nil
}
// SetValue implements the StringChanger interface.
func (c *stringChanger) SetValue(v string) (string, error) {
if c.err != nil {
return "", c.err
}
oldValue := c.node.content.value().(string)
newValue := justValue{v}
if !c.node.isAllowed(newValue, true) {
return "", errors.New(ErrDuplicate, errorMessages)
}
c.node.content = newValue
return oldValue, nil
}
// Add implements the StringChanger interface.
func (c *stringChanger) Add(v string) error {
if c.err != nil {
return c.err
}
_, err := c.node.addChild(justValue{v})
return err
}
// Remove implements the StringChanger interface.
func (c *stringChanger) Remove() error {
if c.err != nil {
return c.err
}
return c.node.remove()
}
// List implements the StringChanger interface.
func (c *stringChanger) List() ([]string, error) {
if c.err != nil {
return nil, c.err
}
var list []string
err := c.node.doChildren(func(cn *node) error {
list = append(list, cn.content.value().(string))
return nil
})
if err != nil {
return nil, err
}
return list, nil
}
// Error implements the StringChanger interface.
func (c *stringChanger) Error() error {
return c.err
}
//--------------------
// KEY/VALUE CHANGER
//--------------------
// keyValueChanger implements the KeyValueChanger interface.
type keyValueChanger struct {
node *node
err error
}
// Key implements the KeyValueChanger interface.
func (c *keyValueChanger) Key() (string, error) {
if c.err != nil {
return "", c.err
}
return c.node.content.key().(string), nil
}
// SetKey implements the KeyValueChanger interface.
func (c *keyValueChanger) SetKey(key string) (string, error) {
if c.err != nil {
return "", c.err
}
if !c.node.container.duplicates {
if c.node.hasDuplicateSibling(key) {
return "", errors.New(ErrDuplicate, errorMessages)
}
}
current := c.node.content.key().(string)
c.node.content = keyValue{key, c.node.content.value()}
return current, nil
}
// Value implements the KeyValueChanger interface.
func (c *keyValueChanger) Value() (interface{}, error) {
if c.err != nil {
return nil, c.err
}
return c.node.content.value(), nil
}
// SetValue implements the KeyValueChanger interface.
func (c *keyValueChanger) SetValue(value interface{}) (interface{}, error) {
if c.err != nil {
return nil, c.err
}
current := c.node.content.value()
c.node.content = keyValue{c.node.content.key(), value}
return current, nil
}
// Add implements the KeyValueChanger interface.
func (c *keyValueChanger) Add(k string, v interface{}) error {
if c.err != nil {
return c.err
}
_, err := c.node.addChild(keyValue{k, v})
return err
}
// Remove implements the KeyValueChanger interface.
func (c *keyValueChanger) Remove() error {
if c.err != nil {
return c.err
}
return c.node.remove()
}
// List implements the KeyValueChanger interface.
func (c *keyValueChanger) List() ([]KeyValue, error) {
if c.err != nil {
return nil, c.err
}
var list []KeyValue
err := c.node.doChildren(func(cn *node) error {
list = append(list, KeyValue{cn.content.key().(string), cn.content.value()})
return nil
})
if err != nil {
return nil, err
}
return list, nil
}
// Error implements the KeyValueChanger interface.
func (c *keyValueChanger) Error() error {
return c.err
}
//--------------------
// KEY/STRING VALUE CHANGER
//--------------------
// keyStringValueChanger implements the KeyStringValueChanger interface.
type keyStringValueChanger struct {
node *node
err error
}
// Key implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) Key() (string, error) {
if c.err != nil {
return "", c.err
}
return c.node.content.key().(string), nil
}
// SetKey implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) SetKey(key string) (string, error) {
if c.err != nil {
return "", c.err
}
if !c.node.container.duplicates {
if c.node.hasDuplicateSibling(key) {
return "", errors.New(ErrDuplicate, errorMessages)
}
}
current := c.node.content.key().(string)
c.node.content = keyValue{key, c.node.content.value()}
return current, nil
}
// Value implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) Value() (string, error) {
if c.err != nil {
return "", c.err
}
if c.node.content.value() == nil {
return "", nil
}
return c.node.content.value().(string), nil
}
// SetValue implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) SetValue(value string) (string, error) {
if c.err != nil {
return "", c.err
}
current := c.node.content.value().(string)
c.node.content = keyValue{c.node.content.key(), value}
return current, nil
}
// Add implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) Add(k, v string) error {
if c.err != nil {
return c.err
}
_, err := c.node.addChild(keyValue{k, v})
return err
}
// Remove implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) Remove() error {
if c.err != nil {
return c.err
}
return c.node.remove()
}
// List implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) List() ([]KeyStringValue, error) {
if c.err != nil {
return nil, c.err
}
var list []KeyStringValue
err := c.node.doChildren(func(cn *node) error {
list = append(list, KeyStringValue{cn.content.key().(string), cn.content.value().(string)})
return nil
})
if err != nil {
return nil, err
}
return list, nil
}
// Error implements the KeyStringValueChanger interface.
func (c *keyStringValueChanger) Error() error {
return c.err
}
// EOF