Skip to content

Commit 4603cc1

Browse files
committed
Changed connection arguments to use ConnectOpts instead of using
a map
1 parent d735431 commit 4603cc1

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

example_query_select_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func ExampleRqlTerm_Get() {
1515
Gender string `gorethink:"gender"`
1616
}
1717

18-
sess, err := r.Connect(map[string]interface{}{
19-
"address": url,
18+
sess, err := r.Connect(r.ConnectOpts{
19+
Address: url,
2020
})
2121

2222
// Setup table
@@ -54,8 +54,8 @@ func ExampleRqlTerm_GetAll_compound() {
5454
Gender string `gorethink:"gender"`
5555
}
5656

57-
sess, err := r.Connect(map[string]interface{}{
58-
"address": url,
57+
sess, err := r.Connect(r.ConnectOpts{
58+
Address: url,
5959
})
6060

6161
// Setup table

example_query_table_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package gorethink_test
22

33
import (
44
"fmt"
5-
r "github.com/dancannon/gorethink"
65
"log"
6+
7+
r "github.com/dancannon/gorethink"
78
)
89

910
func ExampleRqlTerm_TableCreate() {
10-
sess, err := r.Connect(map[string]interface{}{
11-
"address": url,
11+
sess, err := r.Connect(r.ConnectOpts{
12+
Address: url,
1213
})
1314

1415
// Setup database
@@ -26,8 +27,8 @@ func ExampleRqlTerm_TableCreate() {
2627
}
2728

2829
func ExampleRqlTerm_IndexCreate() {
29-
sess, err := r.Connect(map[string]interface{}{
30-
"address": url,
30+
sess, err := r.Connect(r.ConnectOpts{
31+
Address: url,
3132
})
3233

3334
// Setup database
@@ -46,8 +47,8 @@ func ExampleRqlTerm_IndexCreate() {
4647
}
4748

4849
func ExampleRqlTerm_IndexCreate_compound() {
49-
sess, err := r.Connect(map[string]interface{}{
50-
"address": url,
50+
sess, err := r.Connect(r.ConnectOpts{
51+
Address: url,
5152
})
5253

5354
// Setup database

example_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package gorethink_test
22

33
import (
44
"fmt"
5-
r "github.com/dancannon/gorethink"
65
"log"
76
"os"
7+
8+
r "github.com/dancannon/gorethink"
89
)
910

1011
var session *r.Session
@@ -19,8 +20,8 @@ func init() {
1920
}
2021

2122
func Example() {
22-
session, err := r.Connect(map[string]interface{}{
23-
"address": url,
23+
session, err := r.Connect(r.ConnectOpts{
24+
Address: url,
2425
})
2526
if err != nil {
2627
log.Fatalln(err.Error())

gorethink_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package gorethink
33
import (
44
"encoding/json"
55
"flag"
6-
test "launchpad.net/gocheck"
76
"os"
87
"testing"
98
"time"
9+
10+
test "launchpad.net/gocheck"
1011
)
1112

1213
var sess *Session
@@ -37,10 +38,10 @@ var _ = test.Suite(&RethinkSuite{})
3738

3839
func (s *RethinkSuite) SetUpSuite(c *test.C) {
3940
var err error
40-
sess, err = Connect(map[string]interface{}{
41-
"address": url,
42-
"maxIdle": 3,
43-
"maxActive": 3,
41+
sess, err = Connect(ConnectOpts{
42+
Address: url,
43+
MaxIdle: 3,
44+
MaxActive: 3,
4445
})
4546
c.Assert(err, test.IsNil)
4647
}

session.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package gorethink
22

33
import (
4-
"code.google.com/p/goprotobuf/proto"
5-
p "github.com/dancannon/gorethink/ql2"
64
"sync/atomic"
75
"time"
6+
7+
"code.google.com/p/goprotobuf/proto"
8+
p "github.com/dancannon/gorethink/ql2"
89
)
910

1011
type Session struct {
@@ -64,6 +65,21 @@ func newSession(args map[string]interface{}) *Session {
6465
return s
6566
}
6667

68+
type ConnectOpts struct {
69+
Token int64 `gorethink:"token,omitempty"`
70+
Address string `gorethink:"address,omitempty"`
71+
Database string `gorethink:"database,omitempty"`
72+
Timeout time.Duration `gorethink:"timeout,omitempty"`
73+
AuthKey string `gorethink:"authkey,omitempty"`
74+
MaxIdle int `gorethink:"max_idle,omitempty"`
75+
MaxActive int `gorethink:"max_active,omitempty"`
76+
IdleTimeout time.Duration `gorethink:"idle_timeout,omitempty"`
77+
}
78+
79+
func (o *ConnectOpts) toMap() map[string]interface{} {
80+
return optArgsToMap(o)
81+
}
82+
6783
// Connect creates a new database session.
6884
//
6985
// Supported arguments include token, address, database, timeout, authkey,
@@ -76,13 +92,13 @@ func newSession(args map[string]interface{}) *Session {
7692
// Basic connection example:
7793
//
7894
// var session *r.Session
79-
// session, err := r.Connect(map[string]interface{}{
80-
// "address": "localhost:28015",
81-
// "database": "test",
82-
// "authkey": "14daak1cad13dj",
95+
// session, err := r.Connect(r.ConncetOpts{
96+
// Address: "localhost:28015",
97+
// Database: "test",
98+
// AuthKey: "14daak1cad13dj",
8399
// })
84-
func Connect(args map[string]interface{}) (*Session, error) {
85-
s := newSession(args)
100+
func Connect(args ConnectOpts) (*Session, error) {
101+
s := newSession(args.toMap())
86102
err := s.Reconnect()
87103

88104
return s, err

session_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66

77
func (s *RethinkSuite) TestSessionConnectError(c *test.C) {
88
var err error
9-
_, err = Connect(map[string]interface{}{
10-
"address": "nonexistanturl",
11-
"maxIdle": 3,
12-
"maxActive": 3,
9+
_, err = Connect(ConnectOpts{
10+
Address: "nonexistanturl",
11+
MaxIdle: 3,
12+
MaxActive: 3,
1313
})
1414
c.Assert(err, test.NotNil)
1515
}

0 commit comments

Comments
 (0)