-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsleep.go
123 lines (103 loc) · 3.5 KB
/
sleep.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
// Package Sleep provides an intuitive ODM (Object Document Model) library for working
// with MongoDB documents.
// It builds on top of the awesome mgo library
package Sleep
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"reflect"
)
//Convenient access to bson.M
type M bson.M
//Convenient access to bson.D
type D bson.D
type Sleep struct {
Db *mgo.Database
documents map[string]Document
models map[string]*Model
modelTag string
}
// New returns a new intance of the Sleep type
func New(session *mgo.Session, dbName string) *Sleep {
sleep := &Sleep{Db: session.DB(dbName), modelTag: "model"}
sleep.documents = make(map[string]Document)
sleep.models = make(map[string]*Model)
return sleep
}
// SetModelTag changes the default tag key of `model` to an arbitrary key.
// This value is read to make relationships for populting based on ObjectIds
func (z *Sleep) SetModelTag(key string) {
z.modelTag = key
}
// Register registers a given schema and its corresponding collection name with Sleep.
// All schemas MUST be registered using this function.
// Function will return a pointer to the Sleep.Model value for this model
func (z *Sleep) Register(schema interface{}, collectionName string) *Model {
typ := reflect.TypeOf(schema)
structName := typ.Name()
if typ.Kind() == reflect.Ptr {
panic("Expected value, got a pointer")
}
idField := reflect.ValueOf(schema).FieldByName("Id")
if !idField.IsValid() {
panic("Schema `" + structName + "` must have an `Id` field")
}
model := newModel(z.Db.C(collectionName), z)
z.models[structName] = model
z.documents[structName] = Document{C: z.Db.C(collectionName),
isQueried: true, schemaStruct: schema, Model: model,
populated: make(map[string]interface{}), Found: true}
return model
}
// CreateDoc conditions an instance of the model to become a document. Will create an ObjectId for the document.
//
// See Model.CreateDoc. They are the same
func (z *Sleep) CreateDoc(doc interface{}) {
typ := reflect.TypeOf(doc).Elem()
structName := typ.Name()
document := z.documents[structName]
document.schema = doc
document.Model = z.models[structName]
document.Virtual = newVirtual()
val := reflect.ValueOf(doc).Elem()
docVal := val.FieldByName("Document")
docVal.Set(reflect.ValueOf(document))
idField := reflect.ValueOf(doc).Elem().FieldByName("Id")
id := bson.NewObjectId()
idField.Set(reflect.ValueOf(id))
}
// C gives access to the underlying *mgo.Collection value for a model.
// The model name is case sensitive.
func (z *Sleep) C(model string) (*mgo.Collection, bool) {
m, ok := z.documents[model]
c := m.C
return c, ok
}
// Model returns a pointer to the Model of the registered schema
func (z *Sleep) Model(name string) *Model {
return z.models[name]
}
// See ObjectId
func (z *Sleep) ObjectId(id string) bson.ObjectId {
return ObjectId(id)
}
// ObjectId converts a string hex representation of an ObjectId into type bson.ObjectId.
func ObjectId(id string) bson.ObjectId {
return bson.ObjectIdHex(id)
}
//Function will take types string or bson.ObjectId represented by a type interface{} and returns
//a type bson.ObjectId. Will panic if wrong type is passed. Will also panic if the string
//is not a valid representation of an ObjectId
func getObjectId(id interface{}) bson.ObjectId {
var idActual bson.ObjectId
switch id.(type) {
case string:
idActual = bson.ObjectIdHex(id.(string))
break
case bson.ObjectId:
idActual = id.(bson.ObjectId)
default:
panic("Only accepts types `string` and `bson.ObjectId` accepted as Id")
}
return idActual
}