-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
41 lines (35 loc) · 855 Bytes
/
data.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
package data
import (
"fmt"
"github.com/clbanning/mxj"
)
type Source struct {
Map mxj.Map
Path string
OnError func(path string, v interface{}, err error) (interface{}, error)
}
func (s Source) Value() (interface{}, error) {
v, err := s.Map.ValueForPath(s.Path)
if err != nil {
if s.OnError != nil { // optional error handler
return s.OnError(s.Path, v, err)
}
return nil, fmt.Errorf("failed to return %s %v", s.Path, err)
}
return v, nil
}
type Target struct {
Map mxj.Map
Path string
OnError func(path string, v interface{}, err error) error
}
func (t Target) SetValue(v interface{}) error {
err := t.Map.SetValueForPath(v, t.Path)
if err != nil {
if t.OnError != nil { // optional error handler
return t.OnError(t.Path, v, err)
}
return fmt.Errorf("failed to set %s %v", t.Path, err)
}
return nil
}