-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
151 lines (147 loc) · 4.44 KB
/
string.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
151
package xconv
import (
"errors"
"reflect"
"strconv"
)
func StringInt(s string) (i int, err error) {
return strconv.Atoi(s)
}
func StringInt64 (s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
func StringFloat64 (s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}
func StringFloat32 (s string) (float32, error) {
f64, err := strconv.ParseFloat(s, 32)
return float32(f64), err
}
func StringBool(s string) (bool, error) {
switch s {
case "true",
"True",
"t",
"T",
"1":
return true, nil
case "false",
"False",
"f",
"F",
"0":
return false, nil
}
return false, errors.New("goclub/conv: " + s + " can't conv to bool")
}
func ReflectToString(rValue reflect.Value) (value string, err error) {
rType := rValue.Type()
switch rType.Kind() {
case reflect.String:
return rValue.String(), nil
case reflect.Int,reflect.Int8,reflect.Int16,reflect.Int32,reflect.Int64:
return strconv.FormatInt(rValue.Int(), 10), nil
case reflect.Uint,reflect.Uint8,reflect.Uint16,reflect.Uint32,reflect.Uint64:
return strconv.FormatUint(rValue.Uint(), 10), nil
case reflect.Bool:
// 采用1 0 尽可能的让数据量小
if rValue.Bool() {
return "1", nil
} else {
return "0", nil
}
case reflect.Float32:
return strconv.FormatFloat(rValue.Float(),'g', -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(rValue.Float(),'g', -1, 64), nil
case reflect.Array, reflect.Slice:
elemType := rType.Elem()
switch elemType.Kind() {
case reflect.Uint8: // []byte
bytes := rValue.Bytes()
return string(bytes), nil
default:
return "", errors.New("goclub/conv: field(name:" + rType.Name() + "kind:" + rType.Kind().String() + ")" + "can not conv to string " + rType.String())
}
default:
return "", errors.New("goclub/conv: field(name:" + rType.Name() + "kind:" + rType.Kind().String() + ")" + "can not conv to string " + rType.String())
}
return
}
func StringToReflect(s string, rValue reflect.Value) error {
rType := rValue.Type()
if !rValue.CanSet() {
if rValue.Kind() == reflect.Ptr {
rValue = rValue.Elem()
rType = rType.Elem()
}
if !rValue.CanSet() {
return errors.New("goclub/conv: StringReflect(s, rValue) rValue must can set, mu be you should use reflect.ValueOf(pointer)")
}
}
return coreStringToReflect(s, rValue, rType)
}
func coreStringToReflect (s string, rValue reflect.Value, rType reflect.Type) error {
switch rType.Kind() {
case reflect.String:
rValue.SetString(s)
case reflect.Int:
i, err := StringInt64(s) ; if err != nil { return err}
rValue.SetInt(i)
case reflect.Int8:
i, err := strconv.ParseInt(s, 10, 8) ; if err != nil { return err}
rValue.SetInt(i)
case reflect.Int16:
i, err := strconv.ParseInt(s, 10, 16) ; if err != nil { return err}
rValue.SetInt(i)
case reflect.Int32:
i, err := strconv.ParseInt(s, 10, 32) ; if err != nil { return err}
rValue.SetInt(i)
case reflect.Int64:
i, err := strconv.ParseInt(s, 10, 64) ; if err != nil { return err}
rValue.SetInt(i)
case reflect.Uint:
i, err := strconv.ParseUint(s, 10, 64) ; if err != nil { return err}
rValue.SetUint(i)
case reflect.Uint8:
i, err := strconv.ParseUint(s, 10, 8) ; if err != nil { return err}
rValue.SetUint(i)
case reflect.Uint16:
i, err := strconv.ParseUint(s, 10, 16) ; if err != nil { return err}
rValue.SetUint(i)
case reflect.Uint32:
i, err := strconv.ParseUint(s, 10, 32) ; if err != nil { return err}
rValue.SetUint(i)
case reflect.Uint64:
i, err := strconv.ParseUint(s, 10, 64) ; if err != nil { return err}
rValue.SetUint(i)
case reflect.Bool:
b, err := StringBool(s) ; if err != nil {panic(err)}
rValue.SetBool(b)
case reflect.Float32:
f, err := StringFloat32(s) ; if err != nil {panic(err)}
rValue.SetFloat(float64(f))
case reflect.Float64:
f, err := StringFloat64(s) ; if err != nil {panic(err)}
rValue.SetFloat(f)
case reflect.Array, reflect.Slice:
elemType := rType.Elem()
switch elemType.Kind() {
case reflect.Uint8: // []byte
bytes := []byte(s)
rValue.SetBytes(bytes)
default:
return errors.New("goclub/conv: field(name:" + rType.Name() + "kind:" + rType.Kind().String() + ")" + "can not string conv type " + rType.String())
}
default:
return errors.New("goclub/conv: field(name:" + rType.Name() + "kind:" + rType.Kind().String() + ")" + "can not string conv type " + rType.String())
}
return nil
}
func StringPointerString(stringPointer *string) string {
if stringPointer == nil {
return ""
} else {
return *stringPointer
}
}