-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.go
150 lines (119 loc) · 3.35 KB
/
session.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// The package mongofixtures enables to load quickly and easily data into mongo db.
package mongofixtures
import (
"github.com/kylelemons/go-gypsy/yaml"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"regexp"
"strconv"
)
// A session holds the mongo session (based on labix.org/v2/mgo).
type Session struct {
MongoSession *mgo.Session
DatabaseName string
ObjectIds map[string]bson.ObjectId
}
// Builds a new Session with the given host and database name.
func Begin(host string, databaseName string) (Session, error) {
mongoSession, err := mgo.Dial(host)
session := Session{MongoSession: mongoSession, DatabaseName: databaseName}
session.ObjectIds = make(map[string]bson.ObjectId, 0)
return session, err
}
// Removes the collection identified by collectionName.
func (l *Session) Clean(collectionName string) error {
_, err := l.MongoSession.DB(l.DatabaseName).C(collectionName).RemoveAll(nil)
if err != nil && err.Error() == "ns not found" {
err = nil
}
return nil
}
/*
Adds a bson-marshalled version of each documents passed as an argument.
Usage example :
session.Push("collection", Document{Title:"hello world"}, Document{Title:"Hi there!"})
*/
func (l *Session) Push(collectionName string, documents ...interface{}) error {
var err error
for _, document := range documents {
err = l.MongoSession.DB(l.DatabaseName).C(collectionName).Insert(document)
}
return err
}
func (l *Session) ImportYamlFile(path string) {
file, err := yaml.ReadFile(path)
if err != nil {
panic(err)
}
nodes := file.Root.(yaml.Map)
for collectionName, collectionMap := range nodes {
cMap := collectionMap.(yaml.Map)
for _, data := range cMap {
l.Push(collectionName, l.importNode(data))
}
}
}
func (l *Session) importNode(value interface{}) interface{} {
var result interface{}
switch value.(type) {
case yaml.Map:
result = l.importMap(value.(yaml.Map))
case yaml.List:
result = l.importList(value.(yaml.List))
case yaml.Scalar:
result = l.importScalar(value.(yaml.Scalar))
}
return result
}
func (l *Session) importMap(value yaml.Map) interface{} {
m := make(map[string]interface{}, 0)
for key, subvalue := range value {
m[key] = l.importNode(subvalue)
}
return m
}
func (l *Session) importList(value yaml.List) interface{} {
list := make([]interface{}, 0)
for _, subvalue := range value {
list = append(list, l.importNode(subvalue))
}
return list
}
func (l *Session) importScalar(value yaml.Scalar) interface{} {
isHolder, _ := regexp.Match("^__(.*)__$", []byte(value))
var result interface{}
if isHolder {
result = l.getObjectId(string(value))
} else {
resBool, isBool := strconv.ParseBool(string(value))
resFloat, isFloat := strconv.ParseFloat(string(value), 32)
resInt, isInt := strconv.ParseInt(string(value), 10, 32)
if isInt == nil {
return resInt
}
if isFloat == nil {
return resFloat
}
if isBool == nil {
return resBool
}
return value
}
return result
}
func (l *Session) ImportYamlString(yml string) {
}
func (l *Session) getObjectId(placeholder string) bson.ObjectId {
value, exists := l.ObjectIds[placeholder]
if !exists {
value = bson.NewObjectId()
l.ObjectIds[placeholder] = value
}
return value
}
// Ends a session. Should be called with defer right after Begin :
// session := Begin("localhost", "mydatabase")
// defer session.End()
func (l *Session) End() {
l.MongoSession.Close()
}