-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
51 lines (41 loc) · 1.13 KB
/
options.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
package transaction
var _ TransactionOption = (*funcOption)(nil)
// TransactionOption is a type that defines functional options for transaction settings.
type TransactionOption interface {
apply(*transactionOption)
}
// TransactionOptions holds the configurable options for a transaction.
type transactionOption struct {
usePrimary bool
readOnly bool
}
type funcOption struct {
f func(*transactionOption)
}
func (fo *funcOption) apply(o *transactionOption) {
fo.f(o)
}
func newFuncOption(f func(*transactionOption)) *funcOption {
return &funcOption{
f: f,
}
}
func newOption(opts ...TransactionOption) *transactionOption {
o := &transactionOption{}
for _, opt := range opts {
opt.apply(o)
}
return o
}
// WithReadOnly returns a TransactionOption that sets the transaction to be read-only.
func WithReadOnly() TransactionOption {
return newFuncOption(func(o *transactionOption) {
o.readOnly = true
})
}
// WithUsePrimary returns a TransactionOption that forces the transaction to use the primary database.
func WithUsePrimary() TransactionOption {
return newFuncOption(func(o *transactionOption) {
o.usePrimary = true
})
}