-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
74 lines (68 loc) · 1.7 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
package env
import (
"fmt"
"strconv"
"strings"
)
func parseBool(value string) (bool, error) {
switch strings.ToLower(value) {
case "true", "1", "yes":
return true, nil
case "false", "0", "no":
return false, nil
default:
return false, fmt.Errorf("invalid boolean value %s", value)
}
}
// parseBoolSlice parses a comma-separated string into a slice of bools
func parseBoolSlice(value string) ([]bool, error) {
values := strings.Split(value, ",")
result := make([]bool, len(values))
for i, v := range values {
boolValue, err := parseBool(v)
if err != nil {
return nil, err
}
result[i] = boolValue
}
return result, nil
}
// parseIntSlice parses a comma-separated string into a slice of ints
func parseIntSlice(value string) ([]int, error) {
values := strings.Split(value, ",")
result := make([]int, len(values))
for i, v := range values {
intValue, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
result[i] = intValue
}
return result, nil
}
// parseUintSlice parses a comma-separated string into a slice of uints
func parseUintSlice(value string) ([]uint, error) {
values := strings.Split(value, ",")
result := make([]uint, len(values))
for i, v := range values {
uintValue, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
result[i] = uint(uintValue)
}
return result, nil
}
// parseFloatSlice parses a comma-separated string into a slice of floats
func parseFloatSlice(value string) ([]float64, error) {
values := strings.Split(value, ",")
result := make([]float64, len(values))
for i, v := range values {
floatValue, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, err
}
result[i] = floatValue
}
return result, nil
}