-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
parsers.go
150 lines (125 loc) · 3.98 KB
/
parsers.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package queryparam
import (
"errors"
"reflect"
"strconv"
"strings"
"time"
)
// ErrInvalidBoolValue is returned when an unhandled string is parsed.
var ErrInvalidBoolValue = errors.New("unknown bool value")
// DefaultValueParsers returns a set of default value parsers.
func DefaultValueParsers() map[reflect.Type]ValueParser {
return map[reflect.Type]ValueParser{
reflect.TypeOf(""): StringValueParser,
reflect.TypeOf([]string{}): StringSliceValueParser,
reflect.TypeOf(0): IntValueParser,
reflect.TypeOf(int32(0)): Int32ValueParser,
reflect.TypeOf(int64(0)): Int64ValueParser,
reflect.TypeOf(float32(0)): Float32ValueParser,
reflect.TypeOf(float64(0)): Float64ValueParser,
reflect.TypeOf(time.Time{}): TimeValueParser,
reflect.TypeOf(false): BoolValueParser,
reflect.TypeOf(Present(false)): PresentValueParser,
}
}
// StringValueParser parses a string into a string.
func StringValueParser(value string, _ string) (reflect.Value, error) {
return reflect.ValueOf(value), nil
}
// StringSliceValueParser parses a string into a []string.
func StringSliceValueParser(value string, delimiter string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf([]string{}), nil
}
return reflect.ValueOf(strings.Split(value, delimiter)), nil
}
// IntValueParser parses a string into an int64.
func IntValueParser(value string, _ string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf(int(0)), nil
}
i64, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return reflect.ValueOf(int(0)), err
}
return reflect.ValueOf(int(i64)), nil
}
// Int64ValueParser parses a string into an int64.
func Int64ValueParser(value string, _ string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf(int64(0)), nil
}
i64, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return reflect.ValueOf(int64(0)), err
}
return reflect.ValueOf(i64), nil
}
// Int32ValueParser parses a string into an int32.
func Int32ValueParser(value string, _ string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf(0), nil
}
i64, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return reflect.ValueOf(0), err
}
return reflect.ValueOf(i64), nil
}
// TimeValueParser parses a string into an int64.
func TimeValueParser(value string, _ string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf(time.Time{}), nil
}
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return reflect.ValueOf(time.Time{}), err
}
return reflect.ValueOf(t), nil
}
// BoolValueParser parses a string into a bool.
func BoolValueParser(value string, _ string) (reflect.Value, error) {
switch strings.ToLower(value) {
case "true", "1", "y", "yes":
return reflect.ValueOf(true), nil
case "", "false", "0", "n", "no":
return reflect.ValueOf(false), nil
default:
return reflect.ValueOf(false), ErrInvalidBoolValue
}
}
// PresentValueParser sets the target to true.
// This parser will only be executed if the parameter is present.
func PresentValueParser(value string, _ string) (reflect.Value, error) {
return reflect.ValueOf(Present(value != "")), nil
}
// Float64ValueParser parses a string to a float64.
func Float64ValueParser(value string, _ string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf(float64(0)), nil
}
i64, err := strconv.ParseFloat(value, 10)
if err != nil {
return reflect.ValueOf(float64(0)), err
}
return reflect.ValueOf(i64), nil
}
// Float32ValueParser parses a string to a float32.
func Float32ValueParser(value string, _ string) (reflect.Value, error) {
if value == "" {
// ignore blank values.
return reflect.ValueOf(float32(0)), nil
}
f64, err := strconv.ParseFloat(value, 10)
if err != nil {
return reflect.ValueOf(float32(0)), err
}
return reflect.ValueOf(f64), nil
}