-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitive-types.go
135 lines (118 loc) · 2.71 KB
/
primitive-types.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
package config
import (
"fmt"
"strconv"
)
// Bool returns a bool according to a dotted path.
func (c *Config) Bool(path string) (bool, error) {
n, err := get(c.Root, path)
if err != nil {
return false, err
}
switch n := n.(type) {
case bool:
return n, nil
case string:
return strconv.ParseBool(n)
}
return false, typeMismatch("bool or string", n)
}
// UBool returns a bool according to a dotted path or default value or false.
func (c *Config) UBool(path string, defaults ...bool) bool {
value, err := c.Bool(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return false
}
// Float64 returns a float64 according to a dotted path.
func (c *Config) Float64(path string) (float64, error) {
n, err := get(c.Root, path)
if err != nil {
return 0, err
}
switch n := n.(type) {
case float64:
return n, nil
case int:
return float64(n), nil
case string:
return strconv.ParseFloat(n, 64)
}
return 0, typeMismatch("float64, int or string", n)
}
// UFloat64 returns a float64 according to a dotted path or default value or 0.
func (c *Config) UFloat64(path string, defaults ...float64) float64 {
value, err := c.Float64(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return float64(0)
}
// Int returns an int according to a dotted path.
func (c *Config) Int(path string) (int, error) {
n, err := get(c.Root, path)
if err != nil {
return 0, err
}
switch n := n.(type) {
case float64:
// encoding/json unmarshals numbers into floats
if i := int(n); float64(i) == n {
return i, nil
} else {
return 0, fmt.Errorf("Value can't be converted to int: %v", n)
}
case int:
return n, nil
case string:
if v, err := strconv.ParseInt(n, 10, 0); err == nil {
return int(v), nil
} else {
return 0, err
}
}
return 0, typeMismatch("float64, int or string", n)
}
// UInt returns an int according to a dotted path or default value or 0.
func (c *Config) UInt(path string, defaults ...int) int {
value, err := c.Int(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return 0
}
// String returns a string according to a dotted path.
func (c *Config) String(path string) (string, error) {
n, err := get(c.Root, path)
if err != nil {
return "", err
}
switch n := n.(type) {
case bool, float64, int:
return fmt.Sprint(n), nil
case string:
return n, nil
}
return "", typeMismatch("bool, float64, int or string", n)
}
// UString returns a string according to a dotted path or default or "".
func (c *Config) UString(path string, defaults ...string) string {
value, err := c.String(path)
if err == nil {
return value
}
for _, def := range defaults {
return def
}
return ""
}