-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathhelpers.go
94 lines (79 loc) · 3.24 KB
/
helpers.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
package firebolt
import (
"fmt"
"strconv"
)
// Nodeconfig holds a Nodes configuration
type Nodeconfig map[string]string
// IntConfig validates and fetches the int-typed optional config value specified by 'name', using the 'defaultValue' if
// no value was provided in the configuration.
func (c Nodeconfig) IntConfig(name string, defaultValue int, minValue int, maxValue int) (int, error) {
// set the default value, if not provided
_, ok := c[name]
if !ok {
c[name] = strconv.Itoa(defaultValue)
}
return c.IntConfigRequired(name, minValue, maxValue)
}
// IntConfigRequired validates and fetches the int-typed required config value specified by 'name', returning an error
// if no value was provided in the configuration.
func (c Nodeconfig) IntConfigRequired(name string, minValue int, maxValue int) (int, error) {
userValue, ok := c[name]
if !ok {
return 0, fmt.Errorf("missing config value [%s]", name)
}
intValue, err := strconv.Atoi(userValue)
if err != nil {
return 0, fmt.Errorf("expected integer value for config [%s]", name)
}
if intValue > maxValue || intValue < minValue {
return 0, fmt.Errorf("config value [%s] requires value between [%d] and [%d]", name, minValue, maxValue)
}
return intValue, nil
}
// StringConfig validates and fetches the string-typed optional config value specified by 'name', using the 'defaultValue' if
// no value was provided in the configuration.
func (c Nodeconfig) StringConfig(name string, defaultValue string) (string, error) {
// set the default value, if not provided
_, ok := c[name]
if !ok {
c[name] = defaultValue
}
return c.StringConfigRequired(name)
}
// StringConfigRequired validates and fetches the string-typed required config value specified by 'name', returning an
// error if no value was provided in the configuration.
func (c Nodeconfig) StringConfigRequired(name string) (string, error) {
userValue, ok := c[name]
if !ok {
return "", fmt.Errorf("missing config value [%s]", name)
}
return userValue, nil
}
// Float64Config validates and fetches the flaot-typed optional config value specified by 'name', using the 'defaultValue' if
// no value was provided in the configuration. The default float64 (if used) is formatted following platform-and-golang
// default precision and width (%f formatting).
func (c Nodeconfig) Float64Config(name string, defaultValue float64, minValue float64, maxValue float64) (float64, error) {
// set the default value, if not provided
_, ok := c[name]
if !ok {
c[name] = fmt.Sprintf("%f", defaultValue)
}
return c.Float64ConfigRequired(name, minValue, maxValue)
}
// Float64ConfigRequired validates and fetches the float64-typed required config value specified by 'name', returning an error
// if no value was provided in the configuration.
func (c Nodeconfig) Float64ConfigRequired(name string, minValue, maxValue float64) (float64, error) {
userValue, ok := c[name]
if !ok {
return 0, fmt.Errorf("missing config value [%s]", name)
}
f64Value, err := strconv.ParseFloat(userValue, 64)
if err != nil {
return 0, fmt.Errorf("expected float64 value for config [%s]", name)
}
if f64Value > maxValue || f64Value < minValue {
return 0, fmt.Errorf("config value [%s] requires value between [%f] and [%f]", name, minValue, maxValue)
}
return f64Value, nil
}