-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcast.go
146 lines (128 loc) · 2.81 KB
/
cast.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
// Package cast is a lightweight casting (type conversion) library.
package cast
import (
"fmt"
"reflect"
"strconv"
"strings"
)
const (
Uint8 = "uint8"
Uint16 = "uint16"
Uint32 = "uint32"
Uint64 = "uint64"
Uint = "uint"
Int8 = "int8"
Int16 = "int16"
Int32 = "int32"
Int64 = "int64"
Int = "int"
Float32 = "float32"
Float64 = "float64"
Bool = "bool"
String = "string"
)
// FromString casts a string value to the given type name.
func FromString(value string, targetType string) (interface{}, error) {
message := "cast: cannot cast `%v` to type `%v`"
switch targetType {
case Int:
v, err := strconv.ParseInt(value, 0, 32)
if err != nil {
return nil, fmt.Errorf(message, value, targetType)
}
return int(v), nil
case Int8:
v, err := strconv.ParseInt(value, 0, 8)
if err != nil {
return nil, err
}
return int8(v), nil
case Int16:
v, err := strconv.ParseInt(value, 0, 16)
if err != nil {
return nil, err
}
return int16(v), nil
case Int32:
v, err := strconv.ParseInt(value, 0, 32)
if err != nil {
return nil, err
}
return int32(v), nil
case Int64:
v, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return nil, err
}
return v, nil
case Uint:
v, err := strconv.ParseUint(value, 0, 32)
if err != nil {
return nil, err
}
return uint(v), nil
case Uint8:
v, err := strconv.ParseUint(value, 0, 8)
if err != nil {
return nil, err
}
return uint8(v), nil
case Uint16:
v, err := strconv.ParseUint(value, 0, 16)
if err != nil {
return nil, err
}
return uint16(v), nil
case Uint32:
v, err := strconv.ParseUint(value, 0, 32)
if err != nil {
return nil, err
}
return uint32(v), nil
case Uint64:
v, err := strconv.ParseUint(value, 0, 64)
if err != nil {
return nil, err
}
return v, nil
case Bool:
v, err := strconv.ParseBool(value)
if err != nil {
return nil, err
}
return v, nil
case Float32:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
return float32(v), nil
case Float64:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
return v, nil
case String:
return value, nil
}
return nil, fmt.Errorf("cast: type %v is not supported", targetType)
}
// FromType casts a string value to the given reflected type.
func FromType(value string, targetType reflect.Type) (interface{}, error) {
var typeName = targetType.String()
if strings.HasPrefix(typeName, "[]") {
itemType := typeName[2:]
array := reflect.New(targetType).Elem()
for _, v := range strings.Split(value, ",") {
if item, err := FromString(strings.Trim(v, " \n\r"), itemType); err != nil {
return array.Interface(), err
} else {
array = reflect.Append(array, reflect.ValueOf(item))
}
}
return array.Interface(), nil
}
return FromString(value, typeName)
}