-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsettings.go
79 lines (61 loc) · 1.51 KB
/
settings.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
package pgxv4
import (
"github.com/jackc/pgx/v4"
"github.com/avito-tech/go-transaction-manager/trm/v2"
)
// Opt is a type to configure Settings.
type Opt func(*Settings) error
// WithTxOptions sets up pgx.TxOptions for the Settings.
func WithTxOptions(opts pgx.TxOptions) Opt {
return func(s *Settings) error {
*s = s.setTrOpts(opts)
return nil
}
}
// Settings contains settings for pgxv4.Transaction.
type Settings struct {
trm.Settings
txOpts pgx.TxOptions
}
// NewSettings creates Settings.
func NewSettings(trms trm.Settings, oo ...Opt) (Settings, error) {
s := &Settings{Settings: trms, txOpts: pgx.TxOptions{
IsoLevel: "",
AccessMode: "",
DeferrableMode: "",
}}
for _, o := range oo {
if err := o(s); err != nil {
return Settings{}, err
}
}
return *s, nil
}
// MustSettings returns Settings if err is nil and panics otherwise.
func MustSettings(trms trm.Settings, oo ...Opt) Settings {
s, err := NewSettings(trms, oo...)
if err != nil {
panic(err)
}
return s
}
// EnrichBy fills nil properties from external Settings.
func (s Settings) EnrichBy(in trm.Settings) trm.Settings {
external, ok := in.(Settings)
if ok {
var emptyTrOpts pgx.TxOptions
if s.txOpts == emptyTrOpts {
s = s.setTrOpts(external.TxOpts())
}
}
s.Settings = s.Settings.EnrichBy(in)
return s
}
// TxOpts returns trm.CtxKey for the trm.Transaction.
func (s Settings) TxOpts() pgx.TxOptions {
return s.txOpts
}
func (s Settings) setTrOpts(opts pgx.TxOptions) Settings {
s.txOpts = opts
return s
}