Skip to content

Commit ebb881e

Browse files
authored
feat: Add support for custom DialInfo (#12, #15)
* Add support for custom DialInfo Lets users provide their own DialInfo if they need to specify more configuration than mongodm's config allows * Fix minor syntax error * chore: Update readme for custom dialinfo and add test * chore: Add reference link to mgo.DialInfo
1 parent e159d08 commit ebb881e

File tree

3 files changed

+80
-10
lines changed

3 files changed

+80
-10
lines changed

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,42 @@ Load your localisation file and parse it until you get a `map[string]string` typ
8989
DatabaseName: "mongodm_sample",
9090
DatabaseUser: "admin",
9191
DatabasePassword: "admin",
92+
// The option `DatabaseSource` is the database used to establish
93+
// credentials and privileges with a MongoDB server. Defaults to the value
94+
// of `DatabaseName`, if that is set, or "admin" otherwise.
95+
DatabaseSource: "admin",
96+
Locals: localMap["en-US"],
97+
}
98+
99+
connection, err := mongodm.Connect(dbConfig)
100+
101+
if err != nil {
102+
fmt.Println("Database connection error: %v", err)
103+
}
104+
```
105+
106+
You can also pass a custom DialInfo from mgo ([`*mgo.DialInfo`](https://godoc.org/labix.org/v2/mgo#DialInfo)). If used, all config attributes starting with `Database` will be ignored:
107+
108+
```go
109+
file, err := ioutil.ReadFile("locals.json")
110+
111+
if err != nil {
112+
fmt.Printf("File error: %v\n", err)
113+
os.Exit(1)
114+
}
115+
116+
var localMap map[string]map[string]string
117+
json.Unmarshal(file, &localMap)
118+
119+
dbConfig := &mongodm.Config{
120+
DialInfo: &mgo.DialInfo{
121+
Addrs: []string{"127.0.0.1"},
122+
Timeout: 3 * time.Second,
123+
Database: "mongodm_sample",
124+
Username: "admin",
125+
Password: "admin",
126+
Source: "admin",
127+
},
92128
Locals: localMap["en-US"],
93129
}
94130

mongodm.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ type (
102102
DatabaseName string
103103
DatabaseUser string
104104
DatabasePassword string
105-
Source string
105+
DatabaseSource string
106+
DialInfo *mgo.DialInfo
106107
Locals map[string]string
107108
}
108109

@@ -295,13 +296,19 @@ func (self *Connection) Open() (err error) {
295296
}
296297
}()
297298

298-
info := &mgo.DialInfo{
299-
Addrs: self.Config.DatabaseHosts,
300-
Timeout: 3 * time.Second,
301-
Database: self.Config.DatabaseName,
302-
Username: self.Config.DatabaseUser,
303-
Password: self.Config.DatabasePassword,
304-
Source: self.Config.Source,
299+
var info *mgo.DialInfo
300+
301+
if self.Config.DialInfo == info {
302+
info = &mgo.DialInfo{
303+
Addrs: self.Config.DatabaseHosts,
304+
Timeout: 3 * time.Second,
305+
Database: self.Config.DatabaseName,
306+
Username: self.Config.DatabaseUser,
307+
Password: self.Config.DatabasePassword,
308+
Source: self.Config.DatabaseSource,
309+
}
310+
} else {
311+
info = self.Config.DialInfo
305312
}
306313

307314
session, err := mgo.DialWithInfo(info)

mongodm_test.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"testing"
99

10+
mgo "gopkg.in/mgo.v2"
1011
"gopkg.in/mgo.v2/bson"
1112
)
1213

@@ -16,6 +17,7 @@ const (
1617
DBName string = "mongodm_test"
1718
DBUser string = "admin"
1819
DBPass string = "admin"
20+
DBSource string = "admin"
1921
DBTestCollection string = "_testCollection"
2022
DBTestRelCollection string = "_testRelationCollection"
2123
)
@@ -76,8 +78,8 @@ func TestConnection(t *testing.T) {
7678
// with a MongoDB server. Defaults to the value of Database, if that is
7779
// set, or "admin" otherwise.
7880
// see https://godoc.org/labix.org/v2/mgo#DialInfo
79-
Source: "admin",
80-
Locals: localMap["en-US"],
81+
DatabaseSource: "admin",
82+
Locals: localMap["en-US"],
8183
}
8284

8385
db, err := Connect(dbConfig)
@@ -101,6 +103,31 @@ func TestConnection(t *testing.T) {
101103
}
102104
}
103105

106+
func TestConnectionWithCustomDialInfo(t *testing.T) {
107+
108+
var localMap map[string]map[string]string
109+
json.Unmarshal(localsFile, &localMap)
110+
111+
dbConfig := &Config{
112+
DialInfo: &mgo.DialInfo{
113+
Addrs: []string{DBHost},
114+
Database: DBName,
115+
Username: DBUser,
116+
Password: DBPass,
117+
Source: DBSource,
118+
},
119+
Locals: localMap["en-US"],
120+
}
121+
122+
_, err := Connect(dbConfig)
123+
124+
if err != nil {
125+
126+
t.Error("DB: Connection error", err)
127+
128+
}
129+
}
130+
104131
func TestConnectionWithoutExtendedConfig(t *testing.T) {
105132

106133
dbConfig := &Config{

0 commit comments

Comments
 (0)