forked from Oskang09/goloquent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
331 lines (299 loc) · 6.95 KB
/
encoder.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package goloquent
import (
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
"time"
"cloud.google.com/go/datastore"
)
func flatMap(data map[string]interface{}) map[string]interface{} {
for k, value := range data {
switch vi := value.(type) {
case map[string]interface{}:
fm := flatMap(vi)
for kk, nv := range fm {
data[k+"."+kk] = nv
}
delete(data, k)
default:
data[k] = value
}
}
return data
}
func normalize(f field, it interface{}) ([]Property, error) {
props := make([]Property, 0)
if f.isFlatten() {
if f.isSlice() {
vals := make(map[string][]interface{})
for _, vi := range it.([]interface{}) {
l := flatMap(vi.(map[string]interface{}))
for k, vv := range l {
name := f.name + "." + k
arr := vals[name]
arr = append(arr, vv)
vals[name] = arr
}
}
for k, vv := range vals {
props = append(props, Property{[]string{k}, f.typeOf, vv})
}
return props, nil
}
for k, vv := range flatMap(it.(map[string]interface{})) {
props = append(props, Property{[]string{f.name, k}, f.typeOf, vv})
}
return props, nil
}
props = append(props, Property{[]string{f.name}, f.typeOf, it})
return props, nil
}
func marshal(it interface{}) (interface{}, error) {
switch v := it.(type) {
case []interface{}, map[string]interface{}, json.RawMessage:
b, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("goloquent: unable to marshal the value %v", v)
}
return b2s(b), nil
}
return it, nil
}
// SaveStruct :
func SaveStruct(src interface{}) (map[string]Property, error) {
vi := reflect.Indirect(reflect.ValueOf(src))
vv := reflect.New(vi.Type())
vv.Elem().Set(vi) // copy the value to new struct
ety, err := newEntity(vv.Interface())
if err != nil {
return nil, err
}
data := make(map[string]Property)
for _, f := range ety.codec.fields {
fv := getFieldByIndex(vv.Elem(), f.paths)
var it, err = saveField(f, fv)
if err != nil {
return nil, err
}
props, err := normalize(f, it)
if err != nil {
return nil, err
}
for _, p := range props {
data[p.Name()] = p
}
}
vi.Set(vv.Elem())
return data, nil
}
func mapToValue(data map[string]interface{}) (map[string]interface{}, error) {
for k, val := range data {
var (
it interface{}
err error
)
switch vi := val.(type) {
case map[string]interface{}:
it, err = mapToValue(vi)
if err != nil {
return nil, err
}
default:
it, err = interfaceToValue(val)
if err != nil {
return nil, err
}
}
data[k] = it
}
return data, nil
}
// LoadStruct :
func LoadStruct(src interface{}, data map[string]interface{}) error {
v := reflect.ValueOf(src)
if v.Type().Kind() != reflect.Ptr {
return fmt.Errorf("goloquent: struct is not addressable")
}
codec, err := getStructCodec(src)
if err != nil {
return err
}
nv := reflect.New(v.Type().Elem())
for _, f := range codec.fields {
fv := getField(nv.Elem(), f.paths)
if err := loadField(fv, data[f.name]); err != nil {
return err
}
}
v.Elem().Set(nv.Elem())
return nil
}
func interfaceToValue(it interface{}) (interface{}, error) {
var value interface{}
switch vi := it.(type) {
case nil:
value = vi
case string:
value = vi
case bool:
value = vi
case int, int8, int16, int32, int64:
value = vi
case uint, uint8, uint16, uint32, uint64:
value = vi
case float32, float64:
value = vi
case json.RawMessage:
value = vi
case []byte:
value = base64.StdEncoding.EncodeToString(vi)
case *datastore.Key:
str := stringifyKey(vi)
if str == "" {
value = nil
} else {
value = str
}
case SoftDelete:
vv := reflect.ValueOf(it)
if vv.IsNil() {
return nil, nil
}
value = (*SoftDelete(vi)).UTC().Format("2006-01-02 15:04:05")
case Date:
value = time.Time(vi).Format("2006-01-02")
case time.Time:
value = vi.UTC().Format("2006-01-02 15:04:05")
case geoLocation:
b, _ := json.Marshal(vi)
value = json.RawMessage(b)
case []interface{}:
slice := make([]interface{}, 0, len(vi))
for _, elem := range vi {
s, err := interfaceToValue(elem)
if err != nil {
return nil, err
}
slice = append(slice, s)
}
value = slice
case map[string]interface{}: // Nested struct
var list, err = mapToValue(vi)
if err != nil {
return nil, err
}
value = list
default:
vv := reflect.ValueOf(it)
if vv.Type().Kind() != reflect.Ptr {
return nil, fmt.Errorf("goloquent: invalid data type %v", vv.Type())
}
if vv.IsNil() {
return nil, nil
}
it, err := interfaceToValue(vv.Elem().Interface())
if err != nil {
return nil, err
}
return it, nil
}
return value, nil
}
func saveSliceField(f field, v reflect.Value) ([]interface{}, error) {
// if it's struct, f.StructCodec is not nil
if v.Len() <= 0 {
return make([]interface{}, 0), nil
}
slice := make([]interface{}, 0)
for i := 0; i < v.Len(); i++ {
var it, err = saveField(f, v.Index(i))
if err != nil {
return nil, err
}
slice = append(slice, it)
}
return slice, nil
}
func saveStructField(sc *StructCodec, v reflect.Value) (map[string]interface{}, error) {
data := make(map[string]interface{})
for _, f := range sc.fields {
fv := getFieldByIndex(v, f.paths)
if !fv.IsValid() {
continue
}
var it, err = saveField(f, fv)
if err != nil {
return nil, err
}
data[f.name] = it
}
return data, nil
}
func saveField(f field, v reflect.Value) (interface{}, error) {
var it interface{}
t := v.Type()
switch vi := v.Interface().(type) {
case *datastore.Key, time.Time:
it = vi
case json.RawMessage:
if vi == nil {
return json.RawMessage("null"), nil
}
it = vi
case Date:
it = vi
case datastore.GeoPoint:
it = geoLocation{vi.Lat, vi.Lng}
case SoftDelete:
if v.IsNil() {
return reflect.Zero(typeOfSoftDelete).Interface(), nil
}
it = vi
default:
switch t.Kind() {
case reflect.String:
it = v.String()
case reflect.Bool:
it = v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
it = v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
it = v.Uint()
case reflect.Float32, reflect.Float64:
it = v.Float()
case reflect.Slice, reflect.Array:
if t.Elem().Kind() == reflect.Uint8 {
it = v.Bytes()
} else {
v = initSlice(v) // initialize the slice if it's nil
return saveSliceField(f, v)
}
case reflect.Ptr:
elem := t.Elem()
if isBaseType(elem) {
if v.IsNil() { // return zero which has datatype
return reflect.Zero(t).Interface(), nil
}
return saveField(f, v.Elem())
}
if elem.Kind() != reflect.Struct {
return nil, fmt.Errorf("goloquent: unsupported struct field data type %q", t.String())
}
if v.IsNil() {
return reflect.Zero(t).Interface(), nil
}
v = v.Elem()
fallthrough
case reflect.Struct:
data, err := saveStructField(f.StructCodec, v)
if err != nil {
return nil, err
}
it = data
default:
return nil, fmt.Errorf("goloquent: unsupported struct field data type %q", t.String())
}
}
return it, nil
}