forked from Kamva/mgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
45 lines (38 loc) · 1.34 KB
/
model.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
package mgm
// CollectionGetter interface contains a method to return
// a model's custom collection.
type CollectionGetter interface {
// Collection method return collection
Collection() *Collection
}
// CollectionNameGetter interface contains a method to return
// the collection name of a model.
type CollectionNameGetter interface {
// CollectionName method return model collection's name.
CollectionName() string
}
// Model interface contains base methods that must be implemented by
// each model. If you're using the `DefaultModel` struct in your model,
// you don't need to implement any of these methods.
type Model interface {
// PrepareID converts the id value if needed, then
// returns it (e.g convert string to objectId).
PrepareID(id interface{}) (interface{}, error)
GetID() interface{}
SetID(id interface{})
}
// DefaultModel struct contains a model's default fields.
type DefaultModel struct {
IDField `bson:",inline"`
DateFields `bson:",inline"`
}
// Creating function calls the inner fields' defined hooks
// TODO: get context as param in the next version (4).
func (model *DefaultModel) Creating() error {
return model.DateFields.Creating()
}
// Saving function calls the inner fields' defined hooks
// TODO: get context as param the next version(4).
func (model *DefaultModel) Saving() error {
return model.DateFields.Saving()
}