forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.go
76 lines (65 loc) · 1.32 KB
/
default.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
package configx
import (
"bytes"
"time"
json "github.com/pydio/cells/x/jsonx"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/spf13/cast"
)
type def struct {
v interface{}
}
func (d *def) Default(i interface{}) Value {
if d.v == nil {
d.v = i
}
return d
}
func (d *def) Nil() bool {
return d.v == nil
}
func (d *def) Bool() bool {
return cast.ToBool(d.v)
}
func (d *def) Bytes() []byte {
return []byte(cast.ToString(d.v))
}
func (d *def) Int() int {
return cast.ToInt(d.v)
}
func (d *def) Int64() int64 {
return cast.ToInt64(d.v)
}
func (d *def) Duration() time.Duration {
return cast.ToDuration(d.v)
}
func (d *def) String() string {
return cast.ToString(d.v)
}
func (d *def) StringMap() map[string]string {
return cast.ToStringMapString(d.v)
}
func (d *def) StringArray() []string {
return cast.ToStringSlice(d.v)
}
func (d *def) Slice() []interface{} {
return cast.ToSlice(d.v)
}
func (d *def) Map() map[string]interface{} {
r, _ := cast.ToStringMapE(d.v)
return r
}
func (d *def) Scan(val interface{}) error {
jsonStr, err := json.Marshal(d.v)
if err != nil {
return err
}
switch v := val.(type) {
case proto.Message:
err = (&jsonpb.Unmarshaler{AllowUnknownFields: true}).Unmarshal(bytes.NewReader(jsonStr), v)
default:
err = json.Unmarshal(jsonStr, v)
}
return err
}