forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_params.go
193 lines (178 loc) · 3.56 KB
/
proc_params.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
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
package gobatis
import (
"reflect"
"strconv"
"time"
)
// parameters process util
// @params
// param interface{} : sql query params
// @return
// map[string]interface{} : return the convert map
func paramProcess(param interface{}) map[string]interface{} {
v := reflect.ValueOf(param)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
res := make(map[string]interface{})
switch v.Kind() {
case reflect.Array, reflect.Slice:
LOG.Warn("Foreach tag collection element must not be slice or array")
res = listToMap(param)
case reflect.Struct:
res = structToMap(param)
case reflect.Map:
res = param.(map[string]interface{})
default:
res["0"] = param
}
return res
}
// convert list to map
// @params
// arr interface{} : list param
// @return
// map[string]interface{} : return the convert map
func listToMap(arr interface{}) map[string]interface{} {
res := make(map[string]interface{})
objVal := reflect.ValueOf(arr)
if objVal.Kind() != reflect.Array && objVal.Kind() != reflect.Slice {
return res
}
res["list"] = arr
for i := 0; i < objVal.Len(); i++ {
res[strconv.Itoa(i)] = objVal.Index(i).Interface()
}
return res
}
// convert struct to map
// @params
// s interface{} : struct param
// @return
// map[string]interface{} : return the convert map
func structToMap(s interface{}) map[string]interface{} {
objVal := reflect.ValueOf(s)
if objVal.Kind() == reflect.Ptr {
objVal = objVal.Elem()
}
res := make(map[string]interface{})
tp := objVal.Type()
switch tp.Name() {
case "Time":
res["0"] = nil
if nil != s {
res["0"] = s.(time.Time).Format("2006-01-02 15:04:05")
}
case "NullString":
res["0"] = nil
if nil != s {
ns := s.(NullString)
if ns.Valid {
str, _ := ns.Value()
res["0"] = str
}
}
case "NullInt64":
res["0"] = nil
if nil != s {
ns := s.(NullInt64)
if ns.Valid {
str, _ := ns.Value()
res["0"] = str
}
}
case "NullBool":
res["0"] = nil
if nil != s {
ns := s.(NullBool)
if ns.Valid {
str, _ := ns.Value()
res["0"] = str
}
}
case "NullFloat64":
res["0"] = nil
if nil != s {
ns := s.(NullFloat64)
if ns.Valid {
str, _ := ns.Value()
res["0"] = str
}
}
case "NullTime":
res["0"] = nil
if nil != s {
ns := s.(NullTime)
if ns.Valid {
str, _ := ns.Value()
res["0"] = str
}
}
default:
objType := objVal.Type()
for i := 0; i < objVal.NumField(); i++ {
fieldVal := objVal.Field(i)
if fieldVal.CanInterface() {
field := objType.Field(i)
res[field.Name] = fieldToVal(fieldVal.Interface())
}
}
}
return res
}
func fieldToVal(field interface{}) interface{} {
objVal := reflect.ValueOf(field)
if objVal.Kind() == reflect.Ptr {
objVal = objVal.Elem()
}
tp := objVal.Type()
switch tp.Name() {
case "Time":
if nil != field {
return field.(time.Time).Format("2006-01-02 15:04:05")
}
case "NullString":
if nil != field {
ns := field.(NullString)
if ns.Valid {
str, _ := ns.Value()
return str
}
}
case "NullInt64":
if nil != field {
ni64 := field.(NullInt64)
if ni64.Valid {
i, _ := ni64.Value()
return i
}
}
case "NullBool":
if nil != field {
nb := field.(NullBool)
if nb.Valid {
b, _ := nb.Value()
return b
}
}
case "NullFloat64":
if nil != field {
nf := field.(NullFloat64)
if nf.Valid {
f, _ := nf.Value()
return f
}
}
case "NullTime":
if nil != field {
nt := field.(NullTime)
if nt.Valid {
t, _ := nt.Value()
return t.(time.Time).Format("2006-01-02 15:04:05")
}
}
default:
return field
}
return nil
}