-
Notifications
You must be signed in to change notification settings - Fork 3
/
mgo.go
77 lines (65 loc) · 1.07 KB
/
mgo.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
package mgo
import "fmt"
func Dial(addr string) *Session {
return &Session{addr}
}
type Session struct {
addr string
}
func (s *Session) DB(name string) *Database {
return &Database{
session: s,
name: name,
}
}
type Database struct {
session *Session
name string
}
func (db *Database) C(name string) *Collection {
return &Collection{
db: db,
name: name,
}
}
type Collection struct {
db *Database
name string
}
func (c *Collection) Find(query interface{}) *Query {
return &Query{
collection: c,
query: query,
}
}
type Query struct {
collection *Collection
query interface{}
}
func (q *Query) Iter() *Iter {
return &Iter{
query: q,
}
}
type Iter struct {
query *Query
item int
}
func (it *Iter) Next(x interface{}) bool {
sp, ok := x.(*string)
if !ok {
panic("unexpected type")
}
if it.item >= 5 {
return false
}
*sp = fmt.Sprintf("%s.%s.%s query %#v; index %d",
it.query.collection.db.session.addr,
it.query.collection.db.name,
it.query.collection.name,
it.query.query,
it.item,
)
it.item++
return true
}