-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_mapper.go
More file actions
257 lines (245 loc) · 4.89 KB
/
Copy pathstruct_mapper.go
File metadata and controls
257 lines (245 loc) · 4.89 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package dbx
import (
"fmt"
"reflect"
"strconv"
"strings"
"sync"
"time"
)
var structIndexCache sync.Map
func buildStructIndex(t reflect.Type) map[string]int {
t = indirectType(t)
out := make(map[string]int, t.NumField())
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.PkgPath != "" { // unexported
continue
}
tag := f.Tag.Get("db")
if tag == "-" {
continue
}
if tag != "" {
name := strings.Split(tag, ",")[0]
if name != "" {
out[strings.ToLower(name)] = i
continue
}
}
out[strings.ToLower(toSnakeCase(f.Name))] = i
out[strings.ToLower(f.Name)] = i
}
return out
}
func buildStructIndexCached(t reflect.Type) map[string]int {
t = indirectType(t)
if v, ok := structIndexCache.Load(t); ok {
return v.(map[string]int)
}
idx := buildStructIndex(t)
actual, _ := structIndexCache.LoadOrStore(t, idx)
return actual.(map[string]int)
}
func assignValue(dst reflect.Value, src any) {
if !dst.CanSet() {
return
}
if src == nil {
dst.Set(reflect.Zero(dst.Type()))
return
}
if b, ok := src.([]byte); ok && dst.Kind() != reflect.Slice {
src = string(b)
}
if dst.Kind() == reflect.Ptr {
if dst.IsNil() {
dst.Set(reflect.New(dst.Type().Elem()))
}
assignValue(dst.Elem(), src)
return
}
sv := reflect.ValueOf(src)
if sv.IsValid() && sv.Type().AssignableTo(dst.Type()) {
dst.Set(sv)
return
}
if sv.IsValid() && sv.Type().ConvertibleTo(dst.Type()) {
dst.Set(sv.Convert(dst.Type()))
return
}
switch dst.Kind() {
case reflect.String:
dst.SetString(fmt.Sprint(src))
case reflect.Bool:
switch v := src.(type) {
case bool:
dst.SetBool(v)
case int:
dst.SetBool(v != 0)
case int8:
dst.SetBool(v != 0)
case int16:
dst.SetBool(v != 0)
case int32:
dst.SetBool(v != 0)
case int64:
dst.SetBool(v != 0)
case uint:
dst.SetBool(v != 0)
case uint8:
dst.SetBool(v != 0)
case uint16:
dst.SetBool(v != 0)
case uint32:
dst.SetBool(v != 0)
case uint64:
dst.SetBool(v != 0)
case []byte:
p, _ := strconv.ParseBool(strings.TrimSpace(string(v)))
if len(v) == 1 && (v[0] == '1' || v[0] == '0') {
dst.SetBool(v[0] == '1')
return
}
dst.SetBool(p)
case string:
p, _ := strconv.ParseBool(v)
if strings.TrimSpace(v) == "1" {
dst.SetBool(true)
return
}
if strings.TrimSpace(v) == "0" {
dst.SetBool(false)
return
}
dst.SetBool(p)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if dst.Type().PkgPath() == "time" && dst.Type().Name() == "Duration" {
switch v := src.(type) {
case int64:
dst.SetInt(v)
return
case string:
d, err := time.ParseDuration(v)
if err == nil {
dst.SetInt(int64(d))
}
return
}
}
i, ok := toInt64(src)
if ok {
dst.SetInt(i)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
u, ok := toUint64(src)
if ok {
dst.SetUint(u)
}
case reflect.Float32, reflect.Float64:
f, ok := toFloat64(src)
if ok {
dst.SetFloat(f)
}
}
}
func toInt64(v any) (int64, bool) {
switch n := v.(type) {
case int:
return int64(n), true
case int8:
return int64(n), true
case int16:
return int64(n), true
case int32:
return int64(n), true
case int64:
return n, true
case uint:
return int64(n), true
case uint8:
return int64(n), true
case uint16:
return int64(n), true
case uint32:
return int64(n), true
case uint64:
return int64(n), true
case float32:
return int64(n), true
case float64:
return int64(n), true
case string:
i, err := strconv.ParseInt(strings.TrimSpace(n), 10, 64)
return i, err == nil
default:
return 0, false
}
}
func toUint64(v any) (uint64, bool) {
switch n := v.(type) {
case int:
return uint64(n), true
case int8:
return uint64(n), true
case int16:
return uint64(n), true
case int32:
return uint64(n), true
case int64:
return uint64(n), true
case uint:
return uint64(n), true
case uint8:
return uint64(n), true
case uint16:
return uint64(n), true
case uint32:
return uint64(n), true
case uint64:
return n, true
case float32:
return uint64(n), true
case float64:
return uint64(n), true
case string:
i, err := strconv.ParseUint(strings.TrimSpace(n), 10, 64)
return i, err == nil
default:
return 0, false
}
}
func toFloat64(v any) (float64, bool) {
switch n := v.(type) {
case float32:
return float64(n), true
case float64:
return n, true
case int:
return float64(n), true
case int8:
return float64(n), true
case int16:
return float64(n), true
case int32:
return float64(n), true
case int64:
return float64(n), true
case uint:
return float64(n), true
case uint8:
return float64(n), true
case uint16:
return float64(n), true
case uint32:
return float64(n), true
case uint64:
return float64(n), true
case string:
f, err := strconv.ParseFloat(strings.TrimSpace(n), 64)
return f, err == nil
default:
return 0, false
}
}