This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
value.go
275 lines (242 loc) · 6.58 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
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
package cc
import (
"fmt"
"reflect"
"time"
)
// TODO(damnever): cache used value
// Value implements the Valuer interface.
type Value struct {
v interface{}
}
// NewValue creates a new Value.
func NewValue(v interface{}) *Value {
return &Value{v: v}
}
// Exist returns true is value is a valid value, otherwise false.
func (v *Value) Exist() bool {
if v.v == nil {
return false
}
return reflect.ValueOf(v.v).IsValid()
}
// Pattern returns a Patterner.
func (v *Value) Pattern() Patterner {
return NewPattern(v.String())
}
// Raw returns the raw value.
func (v *Value) Raw() interface{} {
return v.v
}
// Config returns the value as a Configer, the modification on returned
// Configer has no affect to the origin value.
func (v *Value) Config() Configer {
switch x := v.v.(type) {
case Configer:
val := newConfig()
for kx, kv := range x.KV() {
val.kv[kx] = kv
}
return val
case map[string]interface{}:
val := newConfig()
for kx, kv := range x {
val.kv[kx] = kv
}
return val
case map[interface{}]interface{}:
val := newConfig()
val.kv = unknownMapToStringMap(x)
return val
}
return newConfig()
}
// Map returns the value as a map, the modification on returned
// map has no affect to the origin value.
func (v *Value) Map() map[string]Valuer {
switch x := v.v.(type) {
case Configer:
val := x.KV()
ms := make(map[string]Valuer, len(val))
for kx, vx := range val {
ms[kx] = NewValue(vx)
}
return ms
case map[string]interface{}:
ms := make(map[string]Valuer, len(x))
for kx, vx := range x {
ms[kx] = NewValue(vx)
}
return ms
case map[interface{}]interface{}:
ms := make(map[string]Valuer, len(x))
for kx, kv := range x {
ms[fmt.Sprintf("%v", kx)] = NewValue(kv)
}
return ms
}
return map[string]Valuer{}
}
// List returns the value as slice, the modification on returned
// slice has no affect to the origin value.
func (v *Value) List() []Valuer {
if x, ok := v.v.([]interface{}); ok {
vs := make([]Valuer, len(x))
for i, e := range x {
vs[i] = NewValue(e)
}
return vs
}
return []Valuer{}
}
// String returns the string value, returns "" if not exists.
func (v *Value) String() string {
return v.StringOr("")
}
// StringOr returns the string value, returns the deflt if not exists.
func (v *Value) StringOr(deflt string) string {
return toString(v.v, deflt)
}
// StringAnd returns the (string value, true) if pattern matched,
// otherwise returns ("", false).
func (v *Value) StringAnd(pattern string) (string, bool) {
if !v.Exist() {
return "", false
}
p := NewPattern(pattern)
if s := v.String(); p.ValidateString(s) {
return s, true
}
return "", false
}
// StringAndOr returns the string value if pattern matched,
// otherwise returns the deflt.
func (v *Value) StringAndOr(pattern string, deflt string) string {
if s, ok := v.StringAnd(pattern); ok {
return s
}
return deflt
}
// Bool returns the bool value, returns false if not exists.
func (v *Value) Bool() bool {
return v.BoolOr(false)
}
// BoolOr returns the bool value, returns the deflt if not exists.
func (v *Value) BoolOr(deflt bool) bool {
return toBool(v.v, deflt)
}
// Int returns the int value, returns 0 if not exists.
func (v *Value) Int() int {
return v.IntOr(0)
}
// IntOr returns the int value, returns the deflt if not exists.
func (v *Value) IntOr(deflt int) int {
return toInt(v.v, deflt)
}
// IntAnd returns the (int value, true) if pattern matched,
// otherwise returns (0, false).
func (v *Value) IntAnd(pattern string) (int, bool) {
if !v.Exist() {
return 0, false
}
p := NewPattern(pattern)
if n := v.Int(); p.ValidateInt(n) {
return n, true
}
return 0, false
}
// IntAndOr returns the int value if pattern matched,
// otherwise returns the deflt.
func (v *Value) IntAndOr(pattern string, deflt int) int {
if n, ok := v.IntAnd(pattern); ok {
return n
}
return deflt
}
// Int64 returns the int64 value, returns 0 if not exists.
func (v *Value) Int64() int64 {
return v.Int64Or(0)
}
// Int64Or returns the int64 value, returns the deflt if not exists.
func (v *Value) Int64Or(deflt int64) int64 {
return toInt64(v.v, deflt)
}
// Int64And returns the (int64 value, true) if pattern matched,
// otherwise returns (0, false). NOTE: we convert all numbers into
// float64 then validate.
func (v *Value) Int64And(pattern string) (int64, bool) {
if !v.Exist() {
return 0, false
}
p := NewPattern(pattern)
if n := v.Int64(); p.ValidateFloat(float64(n)) {
return n, true
}
return 0, false
}
// Int64AndOr returns the int value if pattern matched,
// otherwise returns the deflt. NOTE: we convert all numbers into
// float64 then validate.
func (v *Value) Int64AndOr(pattern string, deflt int64) int64 {
if n, ok := v.Int64And(pattern); ok {
return n
}
return deflt
}
// Float returns the float64 value, returns 0.0 if not exists.
func (v *Value) Float() float64 {
return v.FloatOr(0.0)
}
// FloatOr returns the float64 value, return the deflt if not exists.
func (v *Value) FloatOr(deflt float64) float64 {
return toFloat64(v.v, deflt)
}
// FloatAnd returns the (float64 value, true) if pattern matched,
// otherwise returns (0.0, false).
func (v *Value) FloatAnd(pattern string) (float64, bool) {
if !v.Exist() {
return 0.0, false
}
p := NewPattern(pattern)
if n := v.Float(); p.ValidateFloat(n) {
return n, true
}
return 0.0, false
}
// FloatAndOr returns the float64 value if pattern matched,
// otherwise returns the deflt.
func (v *Value) FloatAndOr(pattern string, deflt float64) float64 {
if n, ok := v.FloatAnd(pattern); ok {
return n
}
return deflt
}
// Duration returns the time.Duration value, returns time.Duration(0) if not exists.
func (v *Value) Duration() time.Duration {
return v.DurationOr(0)
}
// DurationOr returns the time.Duration value, returns time.Duration(deflt)
// if not exists.
func (v *Value) DurationOr(deflt int64) time.Duration {
return time.Duration(v.Int64Or(deflt))
}
// DurationAnd returns the (time.Duration(value), true) if pattern matched,
// otherwise (time.Duration(0), false) returned. NOTE: we convert all numbers
// into float64 then validate.
func (v *Value) DurationAnd(pattern string) (time.Duration, bool) {
n, ok := v.Int64And(pattern)
return time.Duration(n), ok
}
// DurationAndOr returns the time.Duration value if pattern matched,
// otherwise returns the deflt. NOTE: we convert all numbers into
// float64 then validate.
func (v *Value) DurationAndOr(pattern string, deflt int64) time.Duration {
if d, ok := v.DurationAnd(pattern); ok {
return d
}
return time.Duration(deflt)
}
// GoString implements the native format for Value
func (v *Value) GoString() string {
return fmt.Sprintf("%v", v.v)
}