/
float.go
54 lines (50 loc) · 1.41 KB
/
float.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
package cfg
import (
"encoding/json"
"flag"
"fmt"
"os"
"strconv"
)
func (o *Options) defaultFloat(name string, value float64) float64 {
if v := os.Getenv(o.env(name)); v != "" {
if f, err := strconv.ParseFloat(v, 64); err == nil {
value = f
}
}
if v, ok := o.jsonData[name]; ok {
switch t := v.(type) {
case string:
if f, err := strconv.ParseFloat(t, 64); err == nil {
value = f
}
case json.Number:
if val, err := t.Float64(); err == nil {
value = val
}
}
}
if v, ok := o.yamlData[name]; ok {
switch t := v.(type) {
case string:
if f, err := strconv.ParseFloat(t, 64); err == nil {
value = f
}
case float64:
value = t
case int, uint, int32, uint32, int64, uint64:
if val, err := strconv.ParseFloat(fmt.Sprintf("%d", t), 64); err == nil {
value = val
}
}
}
return value
}
// Float64 works mostly like the flag package equivalent except that it will pull in defaults from the environment, and configuratin files
func (o *Options) Float64(name string, value float64, usage string) *float64 {
return flag.Float64(o.flag(name), o.defaultFloat(name, value), usage)
}
// Float64Var works mostly like the flag package equivalent except that it will pull in defaults from the environment, and configuratin files
func (o *Options) Float64Var(p *float64, name string, value float64, usage string) {
flag.Float64Var(p, o.flag(name), o.defaultFloat(name, value), usage)
}