-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbackup_marshal.go
304 lines (274 loc) · 6.83 KB
/
backup_marshal.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
package project
import (
"fmt"
"github.com/semaphoreui/semaphore/db"
"reflect"
)
func marshalValue(v reflect.Value) (interface{}, error) {
// Handle pointers
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return nil, nil
}
return marshalValue(v.Elem())
}
// Handle structs
if v.Kind() == reflect.Struct {
typeOfV := v.Type()
result := make(map[string]interface{})
for i := 0; i < v.NumField(); i++ {
fieldValue := v.Field(i)
fieldType := typeOfV.Field(i)
// Handle anonymous fields (embedded structs)
if fieldType.Anonymous {
embeddedValue, err := marshalValue(fieldValue)
if err != nil {
return nil, err
}
if embeddedMap, ok := embeddedValue.(map[string]interface{}); ok {
// Merge embedded struct fields into parent result map
for k, v := range embeddedMap {
result[k] = v
}
}
continue
}
tag := fieldType.Tag.Get("backup")
if tag == "-" {
continue // Skip fields with backup:"-"
}
// Check if the field should be backed up
if tag == "" {
// Get the field name from the "db" tag
tag = fieldType.Tag.Get("db")
if tag == "" || tag == "-" {
continue // Skip if "db" tag is empty or "-"
}
}
// Recursively process the field value
value, err := marshalValue(fieldValue)
if err != nil {
return nil, err
}
if value == nil {
continue
}
result[tag] = value
}
return result, nil
}
// Handle slices and arrays
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
if v.IsNil() {
return make([]interface{}, 0), nil
}
var result []interface{} = make([]interface{}, 0)
for i := 0; i < v.Len(); i++ {
elemValue, err := marshalValue(v.Index(i))
if err != nil {
return nil, err
}
result = append(result, elemValue)
}
return result, nil
}
// Handle maps
if v.Kind() == reflect.Map {
if v.IsNil() {
return make(map[string]interface{}), nil
}
result := make(map[string]interface{})
for _, key := range v.MapKeys() {
// Assuming the key is a string
mapKey := fmt.Sprintf("%v", key.Interface())
mapValue, err := marshalValue(v.MapIndex(key))
if err != nil {
return nil, err
}
result[mapKey] = mapValue
}
return result, nil
}
// Handle other types (int, string, etc.)
return v.Interface(), nil
}
func setBasicType(data interface{}, v reflect.Value) error {
if !v.CanSet() {
return fmt.Errorf("cannot set value")
}
switch v.Kind() {
case reflect.Bool:
b, ok := data.(bool)
if !ok {
return fmt.Errorf("expected bool for field, got %T", data)
}
v.SetBool(b)
case reflect.String:
s, ok := data.(string)
if !ok {
return fmt.Errorf("expected string for field, got %T", data)
}
v.SetString(s)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, ok := toFloat64(data)
if !ok {
return fmt.Errorf("expected number for field, got %T", data)
}
v.SetInt(int64(n))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, ok := toFloat64(data)
if !ok {
return fmt.Errorf("expected number for field, got %T", data)
}
v.SetUint(uint64(n))
case reflect.Float32, reflect.Float64:
n, ok := toFloat64(data)
if !ok {
return fmt.Errorf("expected number for field, got %T", data)
}
v.SetFloat(n)
default:
return fmt.Errorf("unsupported kind %v", v.Kind())
}
return nil
}
func toFloat64(data interface{}) (float64, bool) {
switch n := data.(type) {
case float64:
return n, true
case float32:
return float64(n), true
case int:
return float64(n), true
case int64:
return float64(n), true
case int32:
return float64(n), true
case int16:
return float64(n), true
case int8:
return float64(n), true
case uint:
return float64(n), true
case uint64:
return float64(n), true
case uint32:
return float64(n), true
case uint16:
return float64(n), true
case uint8:
return float64(n), true
default:
return 0, false
}
}
func unmarshalValueWithBackupTags(data interface{}, v reflect.Value) error {
// Handle pointers
if v.Kind() == reflect.Ptr {
// Initialize pointer if it's nil
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
return unmarshalValueWithBackupTags(data, v.Elem())
}
// Handle structs
if v.Kind() == reflect.Struct {
// Data should be a map
m, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("expected object for struct, got %T", data)
}
return unmarshalStructWithBackupTags(m, v)
}
// Handle slices and arrays
if v.Kind() == reflect.Slice {
dataSlice, ok := data.([]interface{})
if !ok {
return fmt.Errorf("expected array for slice, got %T", data)
}
slice := reflect.MakeSlice(v.Type(), len(dataSlice), len(dataSlice))
for i := 0; i < len(dataSlice); i++ {
elem := slice.Index(i)
if err := unmarshalValueWithBackupTags(dataSlice[i], elem); err != nil {
return err
}
}
v.Set(slice)
return nil
}
if v.Type() == reflect.TypeOf(db.MapStringAnyField{}) {
// Data should be a map
m, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("expected object for map[string]interface{}, got %T", data)
}
v.Set(reflect.ValueOf(db.MapStringAnyField(m)))
return nil
}
// Handle maps
if v.Kind() == reflect.Map {
dataMap, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("expected object for map, got %T", data)
}
mapType := v.Type()
mapValue := reflect.MakeMap(mapType)
for key, value := range dataMap {
keyVal := reflect.ValueOf(key).Convert(mapType.Key())
valVal := reflect.New(mapType.Elem()).Elem()
if err := unmarshalValueWithBackupTags(value, valVal); err != nil {
return err
}
mapValue.SetMapIndex(keyVal, valVal)
}
v.Set(mapValue)
return nil
}
// Handle basic types
return setBasicType(data, v)
}
func unmarshalStructWithBackupTags(data map[string]interface{}, v reflect.Value) error {
t := v.Type()
for i := 0; i < v.NumField(); i++ {
fieldType := t.Field(i)
fieldValue := v.Field(i)
// Handle anonymous fields (embedded structs)
if fieldType.Anonymous {
// Pass the entire data map to the embedded struct
if err := unmarshalStructWithBackupTags(data, fieldValue); err != nil {
return err
}
continue
}
// Skip fields with backup:"-"
backupTag := fieldType.Tag.Get("backup")
if backupTag == "-" {
continue
}
// Determine the JSON key to use
var jsonKey string
if backupTag != "" {
jsonKey = backupTag
} else {
dbTag := fieldType.Tag.Get("db")
if dbTag != "" && dbTag != "-" {
jsonKey = dbTag
} else {
continue // Skip if no backup or db tag
}
}
// Check if the key exists in the data
if value, ok := data[jsonKey]; ok {
if !fieldValue.CanSet() {
continue // Skip fields that cannot be set
}
if value == nil {
continue
}
if err := unmarshalValueWithBackupTags(value, fieldValue); err != nil {
return err
}
}
}
return nil
}