forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbopt.go
190 lines (158 loc) · 4.16 KB
/
dbopt.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package dbopt
import (
"reflect"
"github.com/mongodb/mongo-go-driver/core/readconcern"
"github.com/mongodb/mongo-go-driver/core/readpref"
"github.com/mongodb/mongo-go-driver/core/session"
"github.com/mongodb/mongo-go-driver/core/writeconcern"
)
var dbBundle = new(DatabaseBundle)
// Option represents a DB option.
type Option interface {
dbOption()
}
// optionFunc adds the option to the DB
type optionFunc func(*Database) error
// Database represents a database.
type Database struct {
ReadConcern *readconcern.ReadConcern
WriteConcern *writeconcern.WriteConcern
ReadPreference *readpref.ReadPref
}
// DatabaseBundle is a bundle of database options.
type DatabaseBundle struct {
option Option
next *DatabaseBundle
}
func (*DatabaseBundle) dbOption() {}
func (optionFunc) dbOption() {}
// DropDB represents all possible params for the dropDatabase() function
type DropDB interface {
dropDB()
}
// DropDBSession is the session for the dropDatabase() function.
type DropDBSession interface {
DropDB
ConvertDropDBSession() *session.Client
}
// DropDBSessionOpt is a dropDatabase session option.
type DropDBSessionOpt struct{}
func (DropDBSessionOpt) dropDB() {}
// ConvertDropDBSession implements the DropDBSession interface.
func (DropDBSessionOpt) ConvertDropDBSession() *session.Client {
return nil
}
// BundleDatabase bundles database options
func BundleDatabase(opts ...Option) *DatabaseBundle {
head := dbBundle
for _, opt := range opts {
newBundle := DatabaseBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// ReadConcern sets the read concern.
func (db *DatabaseBundle) ReadConcern(rc *readconcern.ReadConcern) *DatabaseBundle {
return &DatabaseBundle{
option: ReadConcern(rc),
next: db,
}
}
// WriteConcern sets the write concern.
func (db *DatabaseBundle) WriteConcern(wc *writeconcern.WriteConcern) *DatabaseBundle {
return &DatabaseBundle{
option: WriteConcern(wc),
next: db,
}
}
// ReadPreference sets the read preference.
func (db *DatabaseBundle) ReadPreference(rp *readpref.ReadPref) *DatabaseBundle {
return &DatabaseBundle{
option: ReadPreference(rp),
next: db,
}
}
// Unbundle unbundles the options, returning a collection.
func (db *DatabaseBundle) Unbundle() (*Database, error) {
database := &Database{}
err := db.unbundle(database)
if err != nil {
return nil, err
}
return database, nil
}
// Helper that recursively unwraps the bundle.
func (db *DatabaseBundle) unbundle(database *Database) error {
if db == nil {
return nil
}
for head := db; head != nil && head.option != nil; head = head.next {
var err error
switch opt := head.option.(type) {
case *DatabaseBundle:
err = opt.unbundle(database) // add all bundle's options to database
case optionFunc:
err = opt(database) // add option to database
default:
return nil
}
if err != nil {
return err
}
}
return nil
}
// String implements the Stringer interface
func (db *DatabaseBundle) String() string {
if db == nil {
return ""
}
str := ""
for head := db; head != nil && head.option != nil; head = head.next {
switch opt := head.option.(type) {
case *DatabaseBundle:
str += opt.String()
case optionFunc:
str += reflect.TypeOf(opt).String() + "\n"
}
}
return str
}
// ReadConcern sets the read concern.
func ReadConcern(rc *readconcern.ReadConcern) Option {
return optionFunc(
func(d *Database) error {
if d.ReadConcern == nil {
d.ReadConcern = rc
}
return nil
})
}
// WriteConcern sets the write concern.
func WriteConcern(wc *writeconcern.WriteConcern) Option {
return optionFunc(
func(d *Database) error {
if d.WriteConcern == nil {
d.WriteConcern = wc
}
return nil
})
}
// ReadPreference sets the read preference.
func ReadPreference(rp *readpref.ReadPref) Option {
return optionFunc(
func(d *Database) error {
if d.ReadPreference == nil {
d.ReadPreference = rp
}
return nil
})
}