-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
60 lines (51 loc) · 1.46 KB
/
value.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
package accessor
import (
"encoding"
"encoding/json"
"fmt"
)
// ValueAccessor is the Accessor for non-object types.
type ValueAccessor struct {
Value interface{}
}
// Get implements Accessor.
func (a *ValueAccessor) Get(path Path) (Accessor, error) {
if path == thePhantomPath {
return a, nil
}
return nil, NewNoSuchPathError(fmt.Sprintf("%[1]T(%[1]v) has no key", a.Value), path.Key())
}
// Set implements Accessor.
func (a *ValueAccessor) Set(path Path, value interface{}) error {
if path == thePhantomPath {
a.Value = value
return nil
}
return NewNoSuchPathError(fmt.Sprintf("%[1]T(%[1]v) has no key", a.Value), path.Key())
}
// Unwrap implements Accessor.
func (a *ValueAccessor) Unwrap() interface{} {
return a.Value
}
// Foreach implements Accessor.
func (a *ValueAccessor) Foreach(f func(path Path, value interface{}) error) error {
return f(thePhantomPath, a.Value)
}
// MarshalJSON implements encoding/json.Marshaler.
func (a *ValueAccessor) MarshalJSON() ([]byte, error) {
return json.Marshal(a.Value)
}
// MarshalText implements encoding.TextMarshaler.
func (a *ValueAccessor) MarshalText() ([]byte, error) {
if m, ok := a.Value.(encoding.TextMarshaler); ok {
return m.MarshalText()
}
if s, ok := a.Value.(fmt.Stringer); ok {
return []byte(s.String()), nil
}
return []byte(fmt.Sprint(a.Value)), nil
}
// MarshalYAML implements github.com/go-yaml/yaml.Marshaler.
func (a ValueAccessor) MarshalYAML() (interface{}, error) {
return a.Value, nil
}