-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (50 loc) · 1.04 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
package yesql
import (
"github.com/izolate/yesql/bindvar"
"github.com/izolate/yesql/template"
)
// Config stores runtime config for yesql.
type Config struct {
driver string
tpl template.Executer
bvar bindvar.Parser
quiet bool
}
// NewConfig initializes a config with supplied options, or defaults.
func NewConfig(opts ...func(*Config)) *Config {
c := new(Config)
for _, o := range opts {
o(c)
}
if c.bvar == nil {
OptBindvar(bindvar.New(c.driver))(c)
}
if c.tpl == nil {
OptTemplate(template.New())(c)
}
return c
}
// OptDriver sets the driver name.
func OptDriver(s string) func(c *Config) {
return func(c *Config) {
c.driver = s
}
}
// OptTemplate sets the template executer.
func OptTemplate(e template.Executer) func(c *Config) {
return func(c *Config) {
c.tpl = e
}
}
// OptBindvar sets the bindvar parser.
func OptBindvar(p bindvar.Parser) func(c *Config) {
return func(c *Config) {
c.bvar = p
}
}
// OptQuiet disables logging.
func OptQuiet() func(c *Config) {
return func(c *Config) {
c.quiet = true
}
}