Skip to content

Commit 4cb07ef

Browse files
committed
refactor: renamed some interfaces and structs, and exported fields of options-related structs
1 parent 45268ba commit 4cb07ef

File tree

12 files changed

+132
-353
lines changed

12 files changed

+132
-353
lines changed

mongodb/config.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ import (
55
)
66

77
type (
8-
// Config interface
9-
Config interface {
10-
URI() string
11-
Timeout() time.Duration
12-
}
13-
14-
// ConnConfig struct
15-
ConnConfig struct {
16-
uri string
17-
timeout time.Duration
8+
// Config struct
9+
Config struct {
10+
URI string
11+
Timeout time.Duration
1812
}
1913
)
2014

21-
// NewConnConfig creates a new MongoDB connection configuration
15+
// NewConfig creates a new MongoDB connection configuration
2216
//
2317
// Parameters:
2418
//
@@ -27,28 +21,10 @@ type (
2721
//
2822
// Returns:
2923
//
30-
// - *ConnConfig: MongoDB connection configuration
31-
func NewConnConfig(uri string, timeout time.Duration) *ConnConfig {
32-
return &ConnConfig{
24+
// - *Config: MongoDB connection configuration
25+
func NewConfig(uri string, timeout time.Duration) *Config {
26+
return &Config{
3327
uri,
3428
timeout,
3529
}
3630
}
37-
38-
// URI returns the MongoDB URI
39-
//
40-
// Returns:
41-
//
42-
// - string: MongoDB URI
43-
func (c ConnConfig) URI() string {
44-
return c.uri
45-
}
46-
47-
// Timeout returns the MongoDB connection timeout
48-
//
49-
// Returns:
50-
//
51-
// - time.Duration: MongoDB connection timeout
52-
func (c ConnConfig) Timeout() time.Duration {
53-
return c.timeout
54-
}

mongodb/connection.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
type (
13-
// DefaultConnHandler struct
14-
DefaultConnHandler struct {
13+
// DefaultHandler struct
14+
DefaultHandler struct {
1515
ctx context.Context
1616
cancel context.CancelFunc
1717
clientOptions *options.ClientOptions
@@ -20,18 +20,18 @@ type (
2020
}
2121
)
2222

23-
// NewDefaultConnHandler creates a new connection
23+
// NewDefaultHandler creates a new connection
2424
//
2525
// Parameters:
2626
//
27-
// - config: Config interface
27+
// - config *Config: configuration for the connection
2828
//
2929
// Returns:
3030
//
31-
// - *DefaultConnHandler: DefaultConnHandler struct
31+
// - *DefaultHandler: DefaultHandler struct
3232
// - error: error if any
33-
func NewDefaultConnHandler(config Config) (
34-
*DefaultConnHandler,
33+
func NewDefaultHandler(config *Config) (
34+
*DefaultHandler,
3535
error,
3636
) {
3737
// Check if the config is nil
@@ -40,10 +40,10 @@ func NewDefaultConnHandler(config Config) (
4040
}
4141

4242
// Set client options
43-
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout())
44-
clientOptions := options.Client().ApplyURI(config.URI())
43+
ctx, cancel := context.WithTimeout(context.Background(), config.Timeout)
44+
clientOptions := options.Client().ApplyURI(config.URI)
4545

46-
return &DefaultConnHandler{
46+
return &DefaultHandler{
4747
cancel: cancel,
4848
ctx: ctx,
4949
clientOptions: clientOptions,
@@ -56,7 +56,7 @@ func NewDefaultConnHandler(config Config) (
5656
//
5757
// - *mongo.Client: MongoDB client
5858
// - error: error if any
59-
func (d *DefaultConnHandler) Connect() (*mongo.Client, error) {
59+
func (d *DefaultHandler) Connect() (*mongo.Client, error) {
6060
if d == nil {
6161
return nil, godatabases.ErrNilConnHandler
6262
}
@@ -96,7 +96,7 @@ func (d *DefaultConnHandler) Connect() (*mongo.Client, error) {
9696
//
9797
// - *mongo.Client: MongoDB client
9898
// - error: error if any
99-
func (d *DefaultConnHandler) Client() (*mongo.Client, error) {
99+
func (d *DefaultHandler) Client() (*mongo.Client, error) {
100100
if d == nil {
101101
return nil, godatabases.ErrNilConnHandler
102102
}
@@ -118,7 +118,7 @@ func (d *DefaultConnHandler) Client() (*mongo.Client, error) {
118118
// Returns:
119119
//
120120
// - error: error if any
121-
func (d *DefaultConnHandler) Disconnect() error {
121+
func (d *DefaultHandler) Disconnect() error {
122122
if d == nil {
123123
return nil
124124
}

mongodb/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
)
66

77
type (
8-
// ConnHandler interface
9-
ConnHandler interface {
8+
// Handler interface
9+
Handler interface {
1010
Connect() (*mongo.Client, error)
1111
Client() (*mongo.Client, error)
1212
Disconnect() error

redis/config.go

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package redis
22

33
type (
4-
// Config interface
5-
Config interface {
6-
URI() string
7-
Password() string
8-
Database() int
9-
}
10-
11-
// ConnConfig struct
12-
ConnConfig struct {
13-
uri string
14-
password string
15-
database int
4+
// Config struct
5+
Config struct {
6+
URI string
7+
Password string
8+
Database int
169
}
1710
)
1811

19-
// NewConnConfig creates a new Redis config
12+
// NewConfig creates a new Redis config
2013
//
2114
// Parameters:
2215
//
@@ -26,38 +19,11 @@ type (
2619
//
2720
// Returns:
2821
//
29-
// *ConnConfig: the Redis config
30-
func NewConnConfig(uri, password string, database int) *ConnConfig {
31-
return &ConnConfig{
22+
// *Config: the Redis config
23+
func NewConfig(uri, password string, database int) *Config {
24+
return &Config{
3225
uri,
3326
password,
3427
database,
3528
}
3629
}
37-
38-
// URI returns the URI
39-
//
40-
// Returns:
41-
//
42-
// string: the URI
43-
func (c ConnConfig) URI() string {
44-
return c.uri
45-
}
46-
47-
// Password returns the password
48-
//
49-
// Returns:
50-
//
51-
// string: the password
52-
func (c ConnConfig) Password() string {
53-
return c.password
54-
}
55-
56-
// Database returns the database
57-
//
58-
// Returns:
59-
//
60-
// int: the database
61-
func (c ConnConfig) Database() int {
62-
return c.database
63-
}

redis/connection.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ import (
99
)
1010

1111
type (
12-
// DefaultConnHandler struct
13-
DefaultConnHandler struct {
12+
// DefaultHandler struct
13+
DefaultHandler struct {
1414
client *redis.Client
1515
clientOptions *redis.Options
1616
mutex sync.Mutex
1717
}
1818
)
1919

20-
// NewDefaultConnHandler creates a new connection
20+
// NewDefaultHandler creates a new connection
2121
//
2222
// Parameters:
2323
//
24-
// - config Config: configuration for the connection
24+
// - config *Config: configuration for the connection
2525
//
2626
// Returns:
2727
//
28-
// - *DefaultConnHandler: connection handler
28+
// - *DefaultHandler: connection handler
2929
// - error: error if the config is nil
30-
func NewDefaultConnHandler(config Config) (
31-
*DefaultConnHandler,
30+
func NewDefaultHandler(config *Config) (
31+
*DefaultHandler,
3232
error,
3333
) {
3434
// Check if the config is nil
@@ -38,12 +38,12 @@ func NewDefaultConnHandler(config Config) (
3838

3939
// Define the Redis options
4040
clientOptions := &redis.Options{
41-
Addr: config.URI(),
42-
Password: config.Password(),
43-
DB: config.Database(),
41+
Addr: config.URI,
42+
Password: config.Password,
43+
DB: config.Database,
4444
}
4545

46-
return &DefaultConnHandler{
46+
return &DefaultHandler{
4747
clientOptions: clientOptions,
4848
}, nil
4949
}
@@ -54,7 +54,7 @@ func NewDefaultConnHandler(config Config) (
5454
//
5555
// - *redis.Client: Redis client
5656
// - error: error if the connection fails or is already established
57-
func (d *DefaultConnHandler) Connect() (*redis.Client, error) {
57+
func (d *DefaultHandler) Connect() (*redis.Client, error) {
5858
if d == nil {
5959
return nil, godatabases.ErrNilConnHandler
6060
}
@@ -89,7 +89,7 @@ func (d *DefaultConnHandler) Connect() (*redis.Client, error) {
8989
//
9090
// - *redis.Client: Redis client
9191
// - error: error if the connection is not established
92-
func (d *DefaultConnHandler) Client() (*redis.Client, error) {
92+
func (d *DefaultHandler) Client() (*redis.Client, error) {
9393
if d == nil {
9494
return nil, godatabases.ErrNilConnHandler
9595
}
@@ -111,7 +111,7 @@ func (d *DefaultConnHandler) Client() (*redis.Client, error) {
111111
// Returns:
112112
//
113113
// - error: error if the disconnection fails
114-
func (d *DefaultConnHandler) Disconnect() error {
114+
func (d *DefaultHandler) Disconnect() error {
115115
if d == nil {
116116
return godatabases.ErrNilConnHandler
117117
}

redis/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
)
66

77
type (
8-
// ConnHandler interface
9-
ConnHandler interface {
8+
// Handler interface
9+
Handler interface {
1010
Connect() (*redis.Client, error)
1111
Client() (*redis.Client, error)
1212
Disconnect() error

0 commit comments

Comments
 (0)