-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseable.go
More file actions
87 lines (67 loc) · 1.92 KB
/
parseable.go
File metadata and controls
87 lines (67 loc) · 1.92 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
package cli
import (
"net/url"
"strconv"
"time"
"github.com/bobg/errors"
)
var NotParseableError = errors.New("type not parseable")
// Parseable is a type that can be parsed from a string.
type Parseable interface {
string | bool | int | float64 | time.Time | time.Duration | *url.URL
}
// StringParser parses a string into a string.
func StringParser(s string) (string, error) {
return s, nil
}
// BoolParser parses a string into a bool.
func BoolParser(s string) (bool, error) {
return strconv.ParseBool(s)
}
// IntParser parses a string into an int.
func IntParser(s string) (int, error) {
return strconv.Atoi(s)
}
// Float64Parser parses a string into a float64.
func Float64Parser(s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}
// TimeParser parses a string into a time.Time.
func TimeParser(s string) (time.Time, error) {
return TimeLayoutParser(time.RFC3339)(s)
}
// TimeLayoutParser parses a string into a time.Time using a specific time layout.
func TimeLayoutParser(timeLayout string) ArgParser[time.Time] {
return func(s string) (time.Time, error) {
return time.Parse(timeLayout, s)
}
}
// DurationParser parses a string into a time.Duration.
func DurationParser(s string) (time.Duration, error) {
return time.ParseDuration(s)
}
// URLParser parses a string into a *url.URL.
func URLParser(s string) (*url.URL, error) {
return url.Parse(s)
}
func argParserFromParseable[T Parseable]() (argParser, error) {
var t T
switch any(t).(type) {
case string:
return NewArgParser(StringParser), nil
case bool:
return NewArgParser(BoolParser), nil
case int:
return NewArgParser(IntParser), nil
case float64:
return NewArgParser(Float64Parser), nil
case time.Time:
return NewArgParser(TimeParser), nil
case time.Duration:
return NewArgParser(DurationParser), nil
case *url.URL:
return NewArgParser(URLParser), nil
default:
return nil, errors.Wrapf(NotParseableError, "type %T", t)
}
}