-
Notifications
You must be signed in to change notification settings - Fork 166
/
util.go
303 lines (258 loc) · 6.52 KB
/
util.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package beedb
import (
"errors"
"reflect"
"strconv"
"strings"
"time"
)
func getTypeName(obj interface{}) (typestr string) {
typ := reflect.TypeOf(obj)
typestr = typ.String()
lastDotIndex := strings.LastIndex(typestr, ".")
if lastDotIndex != -1 {
typestr = typestr[lastDotIndex+1:]
}
return
}
func snakeCasedName(name string) string {
newstr := make([]rune, 0)
firstTime := true
for _, chr := range name {
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
if firstTime == true {
firstTime = false
} else {
newstr = append(newstr, '_')
}
chr -= ('A' - 'a')
}
newstr = append(newstr, chr)
}
return string(newstr)
}
func titleCasedName(name string) string {
newstr := make([]rune, 0)
upNextChar := true
for _, chr := range name {
switch {
case upNextChar:
upNextChar = false
chr -= ('a' - 'A')
case chr == '_':
upNextChar = true
continue
}
newstr = append(newstr, chr)
}
return string(newstr)
}
func pluralizeString(str string) string {
if strings.HasSuffix(str, "data") {
return str
}
if strings.HasSuffix(str, "y") {
str = str[:len(str)-1] + "ie"
}
return str + "s"
}
func scanMapIntoStruct(obj interface{}, objMap map[string][]byte) error {
dataStruct := reflect.Indirect(reflect.ValueOf(obj))
if dataStruct.Kind() != reflect.Struct {
return errors.New("expected a pointer to a struct")
}
dataStructType := dataStruct.Type()
for i := 0; i < dataStructType.NumField(); i++ {
field := dataStructType.Field(i)
fieldv := dataStruct.Field(i)
err := scanMapElement(fieldv, field, objMap)
if err != nil {
return err
}
}
return nil
}
func scanMapElement(fieldv reflect.Value, field reflect.StructField, objMap map[string][]byte) error {
objFieldName := field.Name
bb := field.Tag
sqlTag := bb.Get("sql")
if bb.Get("beedb") == "-" || sqlTag == "-" || reflect.ValueOf(bb).String() == "-" {
return nil
}
sqlTags := strings.Split(sqlTag, ",")
sqlFieldName := objFieldName
if len(sqlTags[0]) > 0 {
sqlFieldName = sqlTags[0]
}
inline := false
//omitempty := false //TODO!
// CHECK INLINE
if len(sqlTags) > 1 {
if stringArrayContains("inline", sqlTags[1:]) {
inline = true
}
}
if inline {
if field.Type.Kind() == reflect.Struct && field.Type.String() != "time.Time" {
for i := 0; i < field.Type.NumField(); i++ {
err := scanMapElement(fieldv.Field(i), field.Type.Field(i), objMap)
if err != nil {
return err
}
}
} else {
return errors.New("A non struct type can't be inline.")
}
}
// not inline
data, ok := objMap[sqlFieldName]
if !ok {
return nil
}
var v interface{}
switch field.Type.Kind() {
case reflect.Slice:
v = data
case reflect.String:
v = string(data)
case reflect.Bool:
v = string(data) == "1"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
x, err := strconv.Atoi(string(data))
if err != nil {
return errors.New("arg " + sqlFieldName + " as int: " + err.Error())
}
v = x
case reflect.Int64:
x, err := strconv.ParseInt(string(data), 10, 64)
if err != nil {
return errors.New("arg " + sqlFieldName + " as int: " + err.Error())
}
v = x
case reflect.Float32, reflect.Float64:
x, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return errors.New("arg " + sqlFieldName + " as float64: " + err.Error())
}
v = x
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return errors.New("arg " + sqlFieldName + " as int: " + err.Error())
}
v = x
//Supports Time type only (for now)
case reflect.Struct:
if fieldv.Type().String() != "time.Time" {
return errors.New("unsupported struct type in Scan: " + fieldv.Type().String())
}
x, err := time.Parse("2006-01-02 15:04:05", string(data))
if err != nil {
x, err = time.Parse("2006-01-02 15:04:05.000 -0700", string(data))
if err != nil {
return errors.New("unsupported time format: " + string(data))
}
}
v = x
default:
return errors.New("unsupported type in Scan: " + reflect.TypeOf(v).String())
}
fieldv.Set(reflect.ValueOf(v))
return nil
}
func scanStructIntoMap(obj interface{}) (map[string]interface{}, error) {
dataStruct := reflect.Indirect(reflect.ValueOf(obj))
if dataStruct.Kind() != reflect.Struct {
return nil, errors.New("expected a pointer to a struct")
}
dataStructType := dataStruct.Type()
mapped := make(map[string]interface{})
for i := 0; i < dataStructType.NumField(); i++ {
field := dataStructType.Field(i)
fieldv := dataStruct.Field(i)
fieldName := field.Name
bb := field.Tag
sqlTag := bb.Get("sql")
sqlTags := strings.Split(sqlTag, ",")
var mapKey string
inline := false
if bb.Get("beedb") == "-" || sqlTag == "-" || reflect.ValueOf(bb).String() == "-" {
continue
} else if len(sqlTag) > 0 {
//TODO: support tags that are common in json like omitempty
if sqlTags[0] == "-" {
continue
}
mapKey = sqlTags[0]
} else {
mapKey = fieldName
}
if len(sqlTags) > 1 {
if stringArrayContains("inline", sqlTags[1:]) {
inline = true
}
}
if inline {
// get an inner map and then put it inside the outer map
map2, err2 := scanStructIntoMap(fieldv.Interface())
if err2 != nil {
return mapped, err2
}
for k, v := range map2 {
mapped[k] = v
}
} else {
value := dataStruct.FieldByName(fieldName).Interface()
mapped[mapKey] = value
}
}
return mapped, nil
}
func StructName(s interface{}) string {
v := reflect.TypeOf(s)
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v.Name()
}
func getTableName(s interface{}) string {
v := reflect.TypeOf(s)
if v.Kind() == reflect.String {
s2, _ := s.(string)
return snakeCasedName(s2)
}
tn := scanTableName(s)
if len(tn) > 0 {
return tn
}
return getTableName(StructName(s))
}
func scanTableName(s interface{}) string {
if reflect.TypeOf(reflect.Indirect(reflect.ValueOf(s)).Interface()).Kind() == reflect.Slice {
sliceValue := reflect.Indirect(reflect.ValueOf(s))
sliceElementType := sliceValue.Type().Elem()
for i := 0; i < sliceElementType.NumField(); i++ {
bb := sliceElementType.Field(i).Tag
if len(bb.Get("tname")) > 0 {
return bb.Get("tname")
}
}
} else {
tt := reflect.TypeOf(reflect.Indirect(reflect.ValueOf(s)).Interface())
for i := 0; i < tt.NumField(); i++ {
bb := tt.Field(i).Tag
if len(bb.Get("tname")) > 0 {
return bb.Get("tname")
}
}
}
return ""
}
func stringArrayContains(needle string, haystack []string) bool {
for _, v := range haystack {
if needle == v {
return true
}
}
return false
}