forked from tanema/mgorx
/
document.go
120 lines (105 loc) · 2.86 KB
/
document.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
package mgorx
import (
"github.com/revel/revel"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"reflect"
)
type Document struct {
D interface{}
changes interface{}
LastError error
}
func (doc *Document) Id() string {
return reflect.ValueOf(doc.D).Elem().FieldByName("Id").String()
}
func (doc *Document) IsNew() bool {
return !doc.IsPersisted()
}
func (doc *Document) IsPersisted() bool {
return bson.ObjectId(doc.Id()).Valid()
}
func (doc *Document) Validate(v *revel.Validation) {
reflect.ValueOf(doc.D).MethodByName("Validate").Call([]reflect.Value{reflect.ValueOf(v)})
}
func (doc *Document) Get(field_name string) (val interface{}) {
return reflect.ValueOf(doc.D).Elem().FieldByName(field_name).Interface()
}
func (doc *Document) Set(field_name string, v interface{}) {
reflect.ValueOf(doc.D).Elem().FieldByName(field_name).Set(reflect.Value(reflect.ValueOf(v)))
}
func (doc *Document) Save(v *revel.Validation) bool {
return doc.saveChain(v)
}
func (doc *Document) Update(changes interface{}, v *revel.Validation) bool {
doc.changes = changes
return doc.saveChain(v)
}
func (doc *Document) Delete() bool {
doc.callCB("BeforeDestroy")
collection_name := collection_name_from(doc.D)
err := with_collection(collection_name, func(c *mgo.Collection) (err error) {
if doc.IsPersisted() {
err = c.RemoveId(doc.Id())
}
doc.LastError = err
return
})
doc.callCB("AfterDestroy")
return err == nil
}
func (doc *Document) saveChain(v *revel.Validation) bool {
if v != nil {
doc.callCB("BeforeValidation")
doc.Validate(v)
if v.HasErrors() {
return false
}
doc.callCB("AfterValidation")
}
doc.callCB("BeforeSave")
if doc.IsNew() {
doc.callCB("BeforeCreate")
} else {
doc.callCB("BeforeUpdate")
}
saved := doc.save()
if doc.IsNew() {
doc.callCB("AfterCreate")
} else {
doc.callCB("AfterUpdate")
}
doc.callCB("AfterSave")
return saved
}
func (doc *Document) save() bool {
collection_name := collection_name_from(doc.D)
err := with_collection(collection_name, func(c *mgo.Collection) (err error) {
if doc.IsPersisted() {
if doc.changes != nil {
err = c.Update(bson.M{"_id": bson.ObjectId(doc.Id())}, doc.changes)
} else {
err = c.Update(bson.M{"_id": bson.ObjectId(doc.Id())}, doc.D)
}
} else {
err = c.Insert(doc.D)
}
doc.LastError = err
return
})
return err == nil
}
//callbacks
func (doc *Document) callCB(name string) {
reflect.ValueOf(doc.D).MethodByName(name).Call([]reflect.Value{})
}
func (doc *Document) BeforeValidation() {}
func (doc *Document) AfterValidation() {}
func (doc *Document) BeforeSave() {}
func (doc *Document) BeforeCreate() {}
func (doc *Document) BeforeUpdate() {}
func (doc *Document) AfterUpdate() {}
func (doc *Document) AfterCreate() {}
func (doc *Document) AfterSave() {}
func (doc *Document) BeforeDestroy() {}
func (doc *Document) AfterDestroy() {}