-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
54 lines (46 loc) · 1.08 KB
/
map.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
package accessor
// MapAccessor is the Accessor for a map.
type MapAccessor map[string]Accessor
// Get implements Accessor.
func (a MapAccessor) Get(path Path) (Accessor, error) {
child, ok := a[path.Key()]
if !ok {
return nil, NewNoSuchPathError("no such key", path.Key())
}
return getFromChild(child, path)
}
// Set implements Accessor.
func (a MapAccessor) Set(path Path, value interface{}) error {
child, ok := a[path.Key()]
if !ok {
return NewNoSuchPathError("no such key", path.Key())
}
sub, ok := path.SubPath()
if !ok {
acc, err := NewAccessor(value)
if err != nil {
return err
}
a[path.Key()] = acc
return nil
}
return setToChild(child, value, path.Key(), sub)
}
// Unwrap implements Accessor.
func (a MapAccessor) Unwrap() interface{} {
result := map[string]interface{}{}
for k, v := range a {
result[k] = v.Unwrap()
}
return result
}
// Foreach implements Accessor.
func (a MapAccessor) Foreach(f func(path Path, value interface{}) error) error {
for k, child := range a {
err := foreach(child, k, f)
if err != nil {
return err
}
}
return nil
}