-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.go
110 lines (103 loc) · 2.66 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package goson
import (
"fmt"
"reflect"
"strconv"
)
const (
missmatchErrorPattern = `parsing value "%v": can't set value type %s to target type %s`
)
var (
// check basic data type (integer, float, string, boolean)
uintCheck = map[reflect.Kind]bool{
reflect.Uint: true, reflect.Uint8: true, reflect.Uint16: true, reflect.Uint32: true, reflect.Uint64: true,
}
intCheck = map[reflect.Kind]bool{
reflect.Int: true, reflect.Int8: true, reflect.Int16: true, reflect.Int32: true, reflect.Int64: true,
}
floatCheck = map[reflect.Kind]bool{
reflect.Float32: true, reflect.Float64: true,
}
stringCheck = map[reflect.Kind]bool{
reflect.String: true,
}
boolCheck = map[reflect.Kind]bool{
reflect.Bool: true,
}
)
func parseFromString(target reflect.Value, str string) (err error) {
targetKind := target.Kind()
switch {
case stringCheck[targetKind]:
target.SetString(str)
case intCheck[targetKind]:
var val int
if val, err = strconv.Atoi(str); err == nil {
target.SetInt(int64(val))
}
case uintCheck[targetKind]:
var val int
if val, err = strconv.Atoi(str); err == nil {
target.SetUint(uint64(val))
}
case floatCheck[targetKind]:
var val float64
if val, err = strconv.ParseFloat(str, -1); err == nil {
target.SetFloat(val)
}
case boolCheck[targetKind]:
var val bool
if val, err = strconv.ParseBool(str); err == nil {
target.SetBool(val)
}
default:
err = fmt.Errorf(missmatchErrorPattern, str, "string", targetKind)
}
return
}
func parseFromFloat(target reflect.Value, fl float64) (err error) {
targetKind := target.Kind()
switch {
case floatCheck[targetKind]:
target.SetFloat(fl)
case intCheck[targetKind]:
target.SetInt(int64(fl))
case uintCheck[targetKind]:
target.SetUint(uint64(fl))
case stringCheck[targetKind]:
target.SetString(strconv.FormatFloat(fl, 'f', -1, 64))
case boolCheck[targetKind]:
var v bool
if v, err = strconv.ParseBool(strconv.FormatFloat(fl, 'f', -1, 64)); err == nil {
target.SetBool(v)
}
default:
err = fmt.Errorf(missmatchErrorPattern, fl, "float64", targetKind)
}
return
}
func parseFromBool(target reflect.Value, bl bool) (err error) {
targetKind := target.Kind()
switch {
case boolCheck[targetKind]:
target.SetBool(bl)
case stringCheck[targetKind]:
target.SetString(strconv.FormatBool(bl))
case uintCheck[targetKind]:
target.SetUint(uint64(toInt(bl)))
case intCheck[targetKind]:
target.SetInt(int64(toInt(bl)))
case floatCheck[targetKind]:
target.SetFloat(float64(toInt(bl)))
default:
err = fmt.Errorf(missmatchErrorPattern, bl, "boolean", targetKind)
}
return
}
// set if true then int is 1, if false then int is 0
func toInt(b bool) (i int) {
if b {
i = 1
}
return
}