-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
49 lines (45 loc) · 1.06 KB
/
util.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
package mongo
import (
"reflect"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/pkg/errors"
)
func structToBsonM(s interface{}) (bson.M, error) {
var err error
r := bson.M{}
v := reflect.ValueOf(s)
switch v.Kind() {
case reflect.Ptr:
default:
return r, errors.New("Query must be struct pointer")
}
if !v.IsNil() {
u := v.Elem()
var key string
var value reflect.Value
for i := 0; i < u.NumField(); i++ {
value = u.Field(i)
if value.IsNil() || !value.CanInterface() {
continue
}
key = u.Type().Field(i).Tag.Get("bson")
// drop support slice field query to support $in.
if value.Elem().Type().Kind() == reflect.Slice {
r[key] = bson.M{"$in": value.Elem().Interface()}
} else {
r[key] = value.Elem().Interface()
}
}
}
return r, err
}
func getCollection(sess *mgo.Session, query interface{}) (c *mgo.Collection, err error) {
if mc, ok := query.(Collection); ok {
c = sess.DB(mc.Database()).C(mc.Collection())
} else {
err = errors.New("Struct not support MongoCollection")
return
}
return
}