forked from zebresel-com/mongodm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_base.go
670 lines (475 loc) · 16.3 KB
/
document_base.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package mongodm
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// This is the base type each model needs for working with the ODM. Of course you can create your own base type but make sure
// that you implement the IDocumentBase type interface!
type DocumentBase struct {
document IDocumentBase `json:"-" bson:"-"`
collection *mgo.Collection `json:"-" bson:"-"`
connection *Connection `json:"-" bson:"-"`
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
Created time.Time `json:"created" bson:"created"`
Updated time.Time `json:"updated" bson:"updated"`
Deleted bool `json:"-" bson:"deleted"`
}
type m map[string]interface{}
func (self *DocumentBase) SetCollection(collection *mgo.Collection) {
self.collection = collection
}
func (self *DocumentBase) SetDocument(document IDocumentBase) {
self.document = document
}
func (self *DocumentBase) SetConnection(connection *Connection) {
self.connection = connection
}
func (self *DocumentBase) GetId() bson.ObjectId {
return self.Id
}
func (self *DocumentBase) SetId(id bson.ObjectId) {
self.Id = id
}
func (self *DocumentBase) GetCreated() time.Time {
return self.Created
}
func (self *DocumentBase) SetCreated(created time.Time) {
self.Created = created
}
func (self *DocumentBase) SetUpdated(updated time.Time) {
self.Updated = updated
}
func (self *DocumentBase) GetUpdated() time.Time {
return self.Updated
}
func (self *DocumentBase) SetDeleted(deleted bool) {
self.Deleted = deleted
}
func (self *DocumentBase) IsDeleted() bool {
return self.Deleted
}
func (self *DocumentBase) AppendError(errorList *[]error, message string) {
*errorList = append(*errorList, errors.New(message))
}
func (self *DocumentBase) Validate(Values ...interface{}) (bool, []error) {
return self.DefaultValidate()
}
func (self *DocumentBase) DefaultValidate() (bool, []error) {
documentValue := reflect.ValueOf(self.document).Elem()
fieldType := documentValue.Type()
validationErrors := make([]error, 0, 0)
// Iterate all struct fields
for fieldIndex := 0; fieldIndex < documentValue.NumField(); fieldIndex++ {
var minLen int
var maxLen int
var required bool
var err error
var fieldValue reflect.Value
field := fieldType.Field(fieldIndex)
fieldTag := field.Tag
validation := strings.ToLower(fieldTag.Get("validation"))
validationName := fieldTag.Get("json")
minLenTag := fieldTag.Get("minLen")
maxLenTag := fieldTag.Get("maxLen")
requiredTag := fieldTag.Get("required")
modelTag := fieldTag.Get("model")
relationTag := fieldTag.Get("relation") // Reference relation, e.g. one-to-one or one-to-many
fieldName := fieldType.Field(fieldIndex).Name
fieldElem := documentValue.Field(fieldIndex)
// Get element of field by checking if pointer or copy
if fieldElem.Kind() == reflect.Ptr || fieldElem.Kind() == reflect.Interface {
fieldValue = fieldElem.Elem()
} else {
fieldValue = fieldElem
}
if len(minLenTag) > 0 {
minLen, err = strconv.Atoi(minLenTag)
if err != nil {
panic("Check your minLen tag - must be numeric")
}
}
if len(maxLenTag) > 0 {
maxLen, err = strconv.Atoi(maxLenTag)
if err != nil {
panic("Check your maxLen tag - must be numeric")
}
}
if len(requiredTag) > 0 {
required, err = strconv.ParseBool(requiredTag)
if err != nil {
panic("Check your required tag - must be boolean")
}
}
splittedFieldName := strings.Split(validationName, ",")
validationName = splittedFieldName[0]
if validationName == "-" {
validationName = strings.ToLower(fieldName)
}
if len(relationTag) > 0 && fieldValue.Kind() == reflect.Slice && relationTag != REL_1N {
self.AppendError(&validationErrors, L("validation.field_invalid_relation1n", validationName))
} else if fieldValue.Kind() != reflect.Slice && relationTag == REL_1N {
self.AppendError(&validationErrors, L("validation.field_invalid_relation11", validationName))
}
isSet := false
if !fieldValue.IsValid() {
isSet = false
} else if fieldValue.Kind() == reflect.Ptr && !fieldValue.IsNil() {
isSet = true
} else if fieldValue.Kind() == reflect.Slice || fieldValue.Kind() == reflect.Map {
isSet = fieldValue.Len() > 0
} else if fieldValue.Kind() == reflect.Interface {
isSet = fieldValue.Interface() != nil
} else {
isSet = !reflect.DeepEqual(fieldValue.Interface(), reflect.Zero(reflect.TypeOf(fieldValue.Interface())).Interface())
}
if required && !isSet {
self.AppendError(&validationErrors, L("validation.field_required", validationName))
}
if fieldValue.IsValid() {
if stringFieldValue, ok := fieldValue.Interface().(string); ok {
// Regex to match a regex
regex := regexp.MustCompile(`\/((?)(?:[^\r\n\[\/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*\])+)\/((?:g(?:im?|m)?|i(?:gm?|m)?|m(?:gi?|i)?)?)`)
isRegex := regex.MatchString(validation)
if isSet && minLen > 0 && len(stringFieldValue) < minLen {
self.AppendError(&validationErrors, L("validation.field_minlen", validationName, minLen))
} else if isSet && maxLen > 0 && len(stringFieldValue) > maxLen {
self.AppendError(&validationErrors, L("validation.field_maxlen", validationName, maxLen))
}
if isSet && isRegex && !validateRegexp(validation, stringFieldValue) {
self.AppendError(&validationErrors, L("validation.field_invalid", validationName))
}
if isSet && validation == "email" && !validateEmail(stringFieldValue) {
self.AppendError(&validationErrors, L("validation.field_invalid", validationName))
}
if len(modelTag) > 0 {
if !isSet || !bson.IsObjectIdHex(stringFieldValue) {
self.AppendError(&validationErrors, L("validation.field_invalid_id", validationName))
}
}
} else if fieldValue.Kind() == reflect.Interface && fieldValue.Elem().Kind() == reflect.Slice {
slice := fieldValue.Elem()
for index := 0; index < slice.Len(); index++ {
if objectIdString, ok := slice.Index(index).Interface().(string); ok {
if !bson.IsObjectIdHex(objectIdString) {
self.AppendError(&validationErrors, L("validation.field_invalid_id", validationName))
break
}
}
}
}
}
}
return len(validationErrors) == 0, validationErrors
}
func (self *DocumentBase) Update(content interface{}) (error, map[string]interface{}) {
if contentBytes, ok := content.([]byte); ok {
bufferMap := make(map[string]interface{})
err := json.Unmarshal(contentBytes, &bufferMap)
if err != nil {
return err, nil
}
typeName := strings.ToLower(reflect.TypeOf(self.document).Elem().Name())
if mapValue, ok := bufferMap[typeName]; ok {
if typeMap, ok := mapValue.(map[string]interface{}); ok {
delete(typeMap, "created")
delete(typeMap, "updated")
delete(typeMap, "id")
delete(typeMap, "deleted")
}
bytes, err := json.Marshal(mapValue)
if err != nil {
return err, nil
}
err = json.Unmarshal(bytes, self.document)
if err != nil {
return err, nil
}
} else {
return errors.New("object not wrapped in typename"), nil
}
return nil, bufferMap
} else if contentMap, ok := content.(map[string]interface{}); ok {
delete(contentMap, "created")
delete(contentMap, "updated")
delete(contentMap, "id")
delete(contentMap, "deleted")
bytes, err := json.Marshal(contentMap)
if err != nil {
return err, nil
}
err = json.Unmarshal(bytes, self.document)
if err != nil {
return err, nil
}
return nil, nil
}
return nil, nil
}
// Calling this method will not remove the object from the database. Instead the deleted flag is set to true.
// So you can use bson.M{"deleted":false} in your query to filter those documents.
func (self *DocumentBase) Delete() error {
if self.Id.Valid() {
self.SetDeleted(true)
return self.Save()
}
return errors.New("Invalid object id")
}
// Calling this method remove the object from the database.
func (self *DocumentBase) FullDelete() error {
if self.Id.Valid() {
return self.collection.Remove(self)
}
return errors.New("Invalid object id")
}
/*
Populate works exactly like func (*Query) Populate. The only difference is that you call this method
on each model which embeds the DocumentBase type. This means that you can populate single elements or sub-sub-levels.
For example:
User := connection.Model("User")
user := &models.User{}
err := User.Find().Exec(user)
if err != nil {
fmt.Println(err)
}
for _, user := range users {
if user.FirstName == "Max" { //maybe NSA needs some information about Max's messages
err := user.Populate("Messages")
if err != nil {
//some error occured
continue
}
if messages, ok := user.Messages.([]*models.Message); ok {
for _, message := range messages {
fmt.Println(message.text)
}
} else {
fmt.Println("something went wrong during cast. wrong type?")
}
}
}
*/
func (self *DocumentBase) Populate(field ...string) error {
if self.document == nil || self.collection == nil || self.connection == nil {
panic("You have to initialize your document with *Model.New(document IDocumentBase) before using Populate()!")
}
query := &Query{
collection: self.collection,
connection: self.connection,
query: bson.M{},
multiple: false,
populate: field,
}
return query.runPopulation(reflect.ValueOf(self.document))
}
/*
This method saves all changes for a document. Populated relations are getting converted to object ID's / array of object ID's so you dont have to handle this by yourself.
Use this function also when the document was newly created, if it is not existent the method will call insert. During the save process created and updated gets also automatically persisted.
For example:
User := connection.Model("User")
user := &models.User{}
User.New(user) //this sets the connection/collection for this type and is strongly necessary(!) (otherwise panic)
user.FirstName = "Max"
user.LastName = "Mustermann"
err := user.Save()
*/
func (self *DocumentBase) Save() error {
if self.document == nil || self.collection == nil || self.connection == nil {
panic("You have to initialize your document with *Model.New(document IDocumentBase) before using Save()!")
}
// Validate document first
if valid, issues := self.document.Validate(); !valid {
return &ValidationError{&QueryError{"Document could not be validated"}, issues}
}
/*
* "This behavior ensures that writes performed in the old session are necessarily observed
* when using the new session, as long as it was a strong or monotonic session.
* That said, it also means that long operations may cause other goroutines using the
* original session to wait." see: http://godoc.org/labix.org/v2/mgo#Session.Clone
*/
session := self.connection.Session.Clone()
defer session.Close()
collection := session.DB(self.connection.Config.DatabaseName).C(self.collection.Name)
reflectStruct := reflect.ValueOf(self.document).Elem()
fieldType := reflectStruct.Type()
bufferRegistry := make(map[reflect.Value]reflect.Value) //used for restoring after fields got serialized - we only save ids when not embedded
/*
* Iterate over all struct fields and determine
* if there are any relations specified.
*/
for fieldIndex := 0; fieldIndex < reflectStruct.NumField(); fieldIndex++ {
modelTag := fieldType.Field(fieldIndex).Tag.Get("model") //the type which should be referenced
relationTag := fieldType.Field(fieldIndex).Tag.Get("relation") //reference relation, e.g. one-to-one or one-to-many
autoSaveTag := fieldType.Field(fieldIndex).Tag.Get("autosave") //flag if children of relation get automatically saved
/*
* Check if custom model and relation field tag is set,
* otherwise ignore.
*/
if len(modelTag) > 0 {
var fieldValue reflect.Value
var autoSave bool
var relation string
field := reflectStruct.Field(fieldIndex)
// Determine relation type for default initialization
if relationTag == REL_11 {
relation = REL_11
} else if relationTag == REL_1N {
relation = REL_1N
} else {
relation = REL_11 //set one-to-one as default relation
}
// If nil and relation one-to-many -> init field with empty slice of object ids and continue loop
if field.IsNil() {
if relation == REL_1N {
field.Set(reflect.ValueOf(make([]bson.ObjectId, 0, 0)))
}
continue
}
// Determine if relation should be autosaved
if autoSaveTag == "true" {
autoSave = true
} else {
autoSave = false //set autosave default to false
}
// Get element of field by checking if pointer or copy
if field.Kind() == reflect.Ptr || field.Kind() == reflect.Interface {
fieldValue = field.Elem()
} else {
fieldValue = field
}
/*
* Detect if the field is a slice, struct or string
* to handle the different types of relation. Other
* types are not admitted.
*/
// One to many
if fieldValue.Kind() == reflect.Slice {
if relation != REL_1N {
panic("Relation must be '1n' when using slices!")
}
sliceLen := fieldValue.Len()
idBuffer := make([]bson.ObjectId, sliceLen, sliceLen)
// Iterate the slice
for index := 0; index < sliceLen; index++ {
sliceValue := fieldValue.Index(index)
err, objectId := self.persistRelation(sliceValue, autoSave)
if err != nil {
return err
}
idBuffer[index] = objectId
}
/*
* Store the original value and then replace
* it with the generated id list. The value gets
* restored after the model was saved
*/
bufferRegistry[field] = fieldValue
field.Set(reflect.ValueOf(idBuffer))
// One to one
} else if (fieldValue.Kind() == reflect.Ptr && fieldValue.Elem().Kind() == reflect.Struct) || fieldValue.Kind() == reflect.String {
if relation != REL_11 {
panic("Relation must be '11' when using struct or id!")
}
var idBuffer bson.ObjectId
err, objectId := self.persistRelation(fieldValue, autoSave)
if err != nil {
return err
}
idBuffer = objectId
/*
* Store the original value and then replace
* it with the object id. The value gets
* restored after the model was saved
*/
bufferRegistry[field] = fieldValue
field.Set(reflect.ValueOf(idBuffer))
} else {
panic(fmt.Sprintf("DB: Following field kinds are supported for saving relations: slice, struct, string. You used %v", fieldValue.Kind()))
}
}
}
var err error
now := time.Now()
/*
* Check if Object ID is already set.
* If yes -> Update object
* If no -> Create object
*/
if len(self.Id) == 0 {
self.SetCreated(now)
self.SetUpdated(now)
self.SetId(bson.NewObjectId())
err = collection.Insert(self.document)
if err != nil {
if mgo.IsDup(err) {
err = &DuplicateError{&QueryError{fmt.Sprintf("Duplicate key")}}
}
}
} else {
self.SetUpdated(now)
_, errs := collection.UpsertId(self.Id, self.document)
if errs != nil {
if mgo.IsDup(errs) {
errs = &DuplicateError{&QueryError{fmt.Sprintf("Duplicate key")}}
} else {
err = errs
}
}
}
/*
* Restore fields which were changed
* for saving progress (object deserialisation)
*/
for field, oldValue := range bufferRegistry {
field.Set(oldValue)
}
return err
}
func (self *DocumentBase) persistRelation(value reflect.Value, autoSave bool) (error, bson.ObjectId) {
// Detect the type of the value which is stored within the slice
switch typedValue := value.Interface().(type) {
// Deserialize objects to id
case IDocumentBase:
{
// Save children when flag is enabled
if autoSave {
err := typedValue.Save()
if err != nil {
return err, bson.ObjectId("")
}
}
objectId := typedValue.GetId()
if !objectId.Valid() {
panic("DB: Can not persist the relation object because the child was not saved before (invalid id).")
}
return nil, objectId
}
// Only save the id
case bson.ObjectId:
{
if !typedValue.Valid() {
panic("DB: Can not persist the relation object because the child was not saved before (invalid id).")
}
return nil, typedValue
}
case string:
{
if !bson.IsObjectIdHex(typedValue) {
return &InvalidIdError{&QueryError{fmt.Sprintf("Invalid id`s given")}}, bson.ObjectId("")
} else {
return nil, bson.ObjectIdHex(typedValue)
}
}
default:
{
panic(fmt.Sprintf("DB: Only type 'bson.ObjectId' and 'IDocumentBase' can be stored in slices. You used %v", value.Interface()))
}
}
}