-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
141 lines (134 loc) · 3.53 KB
/
utils.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
package orm
import (
"fmt"
"reflect"
"strings"
// "time"
)
func camel2Snake(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(string(data[:]))
}
func snake2Camel(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return string(data[:])
}
func assertType(data interface{}) interface{} {
if valStr, ok := data.([]byte); ok {
return string(valStr)
} else if valInt, ok := data.(int); ok {
return int(valInt)
} else if valInt8, ok := data.(int8); ok {
return int8(valInt8)
} else if valInt16, ok := data.(int16); ok {
return int16(valInt16)
} else if valInt32, ok := data.(int32); ok {
return int32(valInt32)
} else if valInt64, ok := data.(int64); ok {
return int64(valInt64)
} else if valUint, ok := data.(uint); ok {
return uint(valUint)
} else if valUint8, ok := data.(uint8); ok {
return uint8(valUint8)
} else if valUint16, ok := data.(uint16); ok {
return uint16(valUint16)
} else if valUint32, ok := data.(uint32); ok {
return uint32(valUint32)
} else if valUint64, ok := data.(uint64); ok {
return uint64(valUint64)
} else if valFloat32, ok := data.(float32); ok {
return float32(valFloat32)
} else if valFloat64, ok := data.(float64); ok {
return float64(valFloat64)
} else if valBool, ok := data.(bool); ok {
return bool(valBool)
} else {
return data
}
}
func assertWideType(data interface{}) interface{} {
if valStr, ok := data.([]byte); ok {
return string(valStr)
} else if valInt, ok := data.(int); ok {
return int64(valInt)
} else if valInt8, ok := data.(int8); ok {
return int64(valInt8)
} else if valInt16, ok := data.(int16); ok {
return int64(valInt16)
} else if valInt32, ok := data.(int32); ok {
return int64(valInt32)
} else if valInt64, ok := data.(int64); ok {
return int64(valInt64)
} else if valUint, ok := data.(uint); ok {
return uint64(valUint)
} else if valUint8, ok := data.(uint8); ok {
return uint64(valUint8)
} else if valUint16, ok := data.(uint16); ok {
return uint64(valUint16)
} else if valUint32, ok := data.(uint32); ok {
return uint64(valUint32)
} else if valUint64, ok := data.(uint64); ok {
return uint64(valUint64)
} else if valFloat32, ok := data.(float32); ok {
return float64(valFloat32)
} else if valFloat64, ok := data.(float64); ok {
return float64(valFloat64)
} else if valBool, ok := data.(bool); ok {
return bool(valBool)
} else {
return data
}
}
func assignField(f *reflect.Value, value interface{}) {
switch f.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f.SetInt(assertWideType(value).(int64))
case reflect.Float32, reflect.Float64:
f.SetFloat(assertWideType(value).(float64))
case reflect.String:
f.SetString(assertWideType(value).(string))
case reflect.Struct:
f.SetBytes(assertWideType(value).([]byte))
case reflect.Bool:
f.SetBool(assertWideType(value).(bool))
default:
f.SetBytes(assertWideType(value).([]byte))
}
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
panic(err)
}
}