Repository navigation
Expand file tree
/
Copy pathparsers.go
More file actions
112 lines (101 loc) · 3.02 KB
/
Copy pathparsers.go
File metadata and controls
112 lines (101 loc) · 3.02 KB
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
package cli
import (
"errors"
"net/url"
"os"
"strconv"
"time"
)
// ParseBool returns the boolean value represented by the given string.
// See [strconv.ParseBool] for more info.
func ParseBool(s string) (any, error) {
b, err := strconv.ParseBool(s)
if err != nil {
return false, errors.New("invalid boolean value")
}
return b, nil
}
// ParseFloat32 returns the float32 value represented by the given string.
// It unwraps any [strconv.NumError] returned by [strconv.ParseFloat] for
// a slightly cleaner error message.
func ParseFloat32(s string) (any, error) {
f64, err := strconv.ParseFloat(s, 32)
if err != nil {
return 0, numError(err)
}
return float32(f64), nil
}
// ParseFloat64 returns the float64 value represented by the given string.
// It unwraps any [strconv.NumError] returned by [strconv.ParseFloat] for
// a slightly cleaner error message.
func ParseFloat64(s string) (any, error) {
f64, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, numError(err)
}
return f64, nil
}
// ParseInt returns the int value represented by the given string.
// It unwraps any [strconv.NumError] returned by [strconv.ParseInt] for
// a slightly cleaner error message.
func ParseInt(s string) (any, error) {
i64, err := strconv.ParseInt(s, 0, 0)
if err != nil {
return 0, numError(err)
}
return int(i64), nil
}
// ParseUint returns the uint value represented by the given string.
// It unwraps any [strconv.NumError] returned by [strconv.ParseUint] for
// a slightly cleaner error message.
func ParseUint(s string) (any, error) {
u64, err := strconv.ParseUint(s, 0, 0)
if err != nil {
return 0, numError(err)
}
return uint(u64), nil
}
func numError(err error) error {
if ne, ok := err.(*strconv.NumError); ok {
return ne.Err
}
return err
}
// ParseDuration uses the standard library [time.ParseDuration] function to
// parse and return the time.Duration value represented by the given string.
func ParseDuration(s string) (any, error) {
return time.ParseDuration(s)
}
// NewTimeParser returns a [ValueParser] that will use the standard library
// [time.Parse] function with the given layout string to parse and return a
// time.Time from a given string.
func NewTimeParser(layout string) ValueParser {
return func(s string) (any, error) {
return time.Parse(layout, s)
}
}
// ParseURL uses the standard library [url.Parse] function to parse
// and return the *url.URL value represented by the given string.
func ParseURL(s string) (any, error) {
return url.Parse(s)
}
// NewFileParser returns a [ValueParser] that will treat an input string as a
// file path and use given parser to parse the content of the file at that path.
func NewFileParser(vp ValueParser) ValueParser {
return func(fileName string) (any, error) {
b, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
s := string(b)
// Drop trailing newline.
if len(s) > 0 && s[len(s)-1] == '\n' {
s = s[:len(s)-1]
}
// If we don't have a value parser, just return the raw string.
if vp == nil {
return s, nil
}
return vp(s)
}
}