forked from lebrains/gomongowrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
46 lines (37 loc) · 898 Bytes
/
config.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
package gomongowrapper
import (
"time"
"emperror.dev/errors"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type ReadPref struct {
Mode readpref.Mode
MaxStaleness *time.Duration
Tags []string
}
// Config holds information necessary for connecting to a database.
type Config struct {
URI string
Hosts []string
ReplicaSet *string
User string
Pass string
Name string
ReadPreference *ReadPref
Params map[string]string
}
// Validate checks that the configuration is valid.
func (c Config) Validate() error {
if c.URI == "" && len(c.Hosts) == 0 {
return errors.New("database hosts or uri is required")
}
if c.URI == "" && len(c.Hosts) > 0 {
if c.User == "" {
return errors.New("database user is required")
}
if c.Name == "" {
return errors.New("database name is required")
}
}
return nil
}