-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathparser.go
86 lines (73 loc) · 2.01 KB
/
parser.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
package regroup
import (
"reflect"
"strconv"
"time"
)
type parseFunc func(src string, typ reflect.Type) (reflect.Value, error)
var builtinTypesParsingFuncs = map[reflect.Kind]parseFunc{
reflect.Bool: parseBool,
reflect.String: parseString,
reflect.Int: parseInt,
reflect.Int8: parseInt,
reflect.Int16: parseInt,
reflect.Int32: parseInt,
reflect.Int64: parseInt,
reflect.Uint: parseUInt,
reflect.Uint8: parseUInt,
reflect.Uint16: parseUInt,
reflect.Uint32: parseUInt,
reflect.Uint64: parseUInt,
reflect.Float32: parseFloat,
reflect.Float64: parseFloat,
}
var typesParsingFuncs = map[reflect.Type]parseFunc{
reflect.TypeOf(time.Second): parseDuration,
}
func getParsingFunc(typ reflect.Type) parseFunc {
if parsingFunc, ok := typesParsingFuncs[typ]; ok {
return parsingFunc
}
if parsingFunc, ok := builtinTypesParsingFuncs[typ.Kind()]; ok {
return parsingFunc
}
return nil
}
func parseString(src string, typ reflect.Type) (reflect.Value, error) {
return reflect.ValueOf(src).Convert(typ), nil
}
func parseInt(src string, typ reflect.Type) (reflect.Value, error) {
n, err := strconv.ParseInt(src, 10, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(n).Convert(typ), nil
}
func parseUInt(src string, typ reflect.Type) (reflect.Value, error) {
n, err := strconv.ParseUint(src, 10, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(n).Convert(typ), nil
}
func parseFloat(src string, typ reflect.Type) (reflect.Value, error) {
n, err := strconv.ParseFloat(src, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(n).Convert(typ), nil
}
func parseBool(src string, _ reflect.Type) (reflect.Value, error) {
b, err := strconv.ParseBool(src)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(b), nil
}
func parseDuration(src string, _ reflect.Type) (reflect.Value, error) {
d, err := time.ParseDuration(src)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(d), nil
}