From d9f1ca7474eb91fdcff5d64ce1dd100018227ceb Mon Sep 17 00:00:00 2001 From: andrea Date: Thu, 24 Jan 2019 20:02:01 +0100 Subject: [PATCH 1/6] See session.go --- session.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/session.go b/session.go index a541b3483..d10dea3fa 100644 --- a/session.go +++ b/session.go @@ -93,6 +93,53 @@ type Session struct { bypassValidation bool } +// FROM HERE +type Auth struct { + Username string + Password string + Url string + DatabaseName string + Mode mgo.Mode + Refresh bool +} + +func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database) { + session, _ := mgo.Dial(credential.Url) + + db := session.DB(credential.DatabaseName) + err := db.Login(credential.Username, credential.Password) + if err != nil { + log.Fatalf("CreateSession: %s\n", err) + } else { + fmt.Println("Success") + } + session.SetMode(credential.Mode, credential.Refresh) + return session, db +} + +func UseScheme(db *mgo.Database, nameScheme string) *mgo.Collection { + + return db.C(nameScheme) +} + + +// UP TO HERE + + +/* + READ ME +To use this wrapper you have to create a structure, which will be the subject of the MongoConnection function, + + +MongoConnection returns two values, the first is the connection session to the * mgo.Session type database, the second is just a * mgo.Database. + + +The UseScheme function takes an mgo.Database type (the second argument returned by MongoConnection) and returns a * mgo.Collection + + +*/ + + type Database struct { Session *Session Name string From 86918aa750fa086645cb1e8b568a98c12fc206c6 Mon Sep 17 00:00:00 2001 From: Andrea Bacciu <36055796+andreabac3@users.noreply.github.com> Date: Sat, 9 Feb 2019 19:35:35 +0100 Subject: [PATCH 2/6] Update session.go --- session.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/session.go b/session.go index d10dea3fa..c86be3217 100644 --- a/session.go +++ b/session.go @@ -105,7 +105,6 @@ type Auth struct { func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database) { session, _ := mgo.Dial(credential.Url) - db := session.DB(credential.DatabaseName) err := db.Login(credential.Username, credential.Password) if err != nil { @@ -118,7 +117,6 @@ func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database) { } func UseScheme(db *mgo.Database, nameScheme string) *mgo.Collection { - return db.C(nameScheme) } From 0434d46383e76e8ded49c4e0f3f3f78c5d986ff1 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 19 Feb 2019 10:50:41 +0100 Subject: [PATCH 3/6] fix --- session.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/session.go b/session.go index c86be3217..2a9231c06 100644 --- a/session.go +++ b/session.go @@ -31,6 +31,7 @@ import ( "encoding/hex" "errors" "fmt" + "gopkg.in/mgo.v2" "math" "net" "net/url" @@ -103,27 +104,23 @@ type Auth struct { Refresh bool } -func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database) { +func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database, error) { session, _ := mgo.Dial(credential.Url) db := session.DB(credential.DatabaseName) err := db.Login(credential.Username, credential.Password) if err != nil { - log.Fatalf("CreateSession: %s\n", err) - } else { - fmt.Println("Success") + return nil, nil, err } session.SetMode(credential.Mode, credential.Refresh) - return session, db + return session, db, nil } func UseScheme(db *mgo.Database, nameScheme string) *mgo.Collection { return db.C(nameScheme) } - // UP TO HERE - /* READ ME To use this wrapper you have to create a structure, which will be the subject of the MongoConnection function, @@ -137,7 +134,6 @@ The UseScheme function takes an mgo.Database type (the second argument returned */ - type Database struct { Session *Session Name string From 59e4da3c118179ddf42b0a9b4be3ddea32f437b2 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 19 Feb 2019 10:51:29 +0100 Subject: [PATCH 4/6] fix --- session.go | 1 - 1 file changed, 1 deletion(-) diff --git a/session.go b/session.go index 2a9231c06..8ab20e6b9 100644 --- a/session.go +++ b/session.go @@ -1098,7 +1098,6 @@ type Index struct { } type Collation struct { - // Locale defines the collation locale. Locale string `bson:"locale"` From f3fef6cf90674b17d05bee3a2e060ce8f21f877c Mon Sep 17 00:00:00 2001 From: Andrea Bacciu <36055796+andreabac3@users.noreply.github.com> Date: Wed, 20 Feb 2019 11:02:19 +0100 Subject: [PATCH 5/6] Update session.go --- session.go | 42 +----------------------------------------- 1 file changed, 1 insertion(+), 41 deletions(-) diff --git a/session.go b/session.go index 8ab20e6b9..a541b3483 100644 --- a/session.go +++ b/session.go @@ -31,7 +31,6 @@ import ( "encoding/hex" "errors" "fmt" - "gopkg.in/mgo.v2" "math" "net" "net/url" @@ -94,46 +93,6 @@ type Session struct { bypassValidation bool } -// FROM HERE -type Auth struct { - Username string - Password string - Url string - DatabaseName string - Mode mgo.Mode - Refresh bool -} - -func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database, error) { - session, _ := mgo.Dial(credential.Url) - db := session.DB(credential.DatabaseName) - err := db.Login(credential.Username, credential.Password) - if err != nil { - return nil, nil, err - } - session.SetMode(credential.Mode, credential.Refresh) - return session, db, nil -} - -func UseScheme(db *mgo.Database, nameScheme string) *mgo.Collection { - return db.C(nameScheme) -} - -// UP TO HERE - -/* - READ ME -To use this wrapper you have to create a structure, which will be the subject of the MongoConnection function, - - -MongoConnection returns two values, the first is the connection session to the * mgo.Session type database, the second is just a * mgo.Database. - - -The UseScheme function takes an mgo.Database type (the second argument returned by MongoConnection) and returns a * mgo.Collection - - -*/ - type Database struct { Session *Session Name string @@ -1098,6 +1057,7 @@ type Index struct { } type Collation struct { + // Locale defines the collation locale. Locale string `bson:"locale"` From 37316e79d54d5bb552e063ee0e1e49a07bb9306b Mon Sep 17 00:00:00 2001 From: Andrea Bacciu <36055796+andreabac3@users.noreply.github.com> Date: Wed, 20 Feb 2019 11:03:10 +0100 Subject: [PATCH 6/6] Add files via upload --- session_wrapper.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 session_wrapper.go diff --git a/session_wrapper.go b/session_wrapper.go new file mode 100644 index 000000000..3c5c6a90f --- /dev/null +++ b/session_wrapper.go @@ -0,0 +1,45 @@ +package mgo + +import ( + "gopkg.in/mgo.v2" +) + +type Auth struct { + Username string + Password string + Url string + DatabaseName string + Mode mgo.Mode + Refresh bool +} + +func MongoConnection(credential Auth) (*mgo.Session, *mgo.Database, error) { + session, _ := mgo.Dial(credential.Url) + + db := session.DB(credential.DatabaseName) + err := db.Login(credential.Username, credential.Password) + if err != nil { + return nil, nil, err + } + session.SetMode(credential.Mode, credential.Refresh) + return session, db, nil +} + +func UseScheme(db *mgo.Database, nameScheme string) *mgo.Collection { + + return db.C(nameScheme) +} + + +/* + READ ME +To use this wrapper, you have to create a structure, which will be the subject of the MongoConnection function, + + +MongoConnection returns three values; the first one is the connection session to the * mgo.Session type database, the second is a * mgo.Database, and third will not be nil if you have got an error. + + +The UseScheme function takes an mgo.Database type (the second argument returned by MongoConnection) and returns a * mgo.Collection + + +*/