-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoptions.go
58 lines (49 loc) · 976 Bytes
/
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
52
53
54
55
56
57
58
package tqla
import (
"fmt"
"text/template"
)
type options struct {
placeholder Placeholder
funcs template.FuncMap
}
type Option interface {
Apply(*options) error
}
type funcOption struct {
f func(*options) error
}
func (flo *funcOption) Apply(con *options) error {
return flo.f(con)
}
func newFuncOption(f func(*options) error) *funcOption {
return &funcOption{
f: f,
}
}
func WithPlaceHolder(p Placeholder) Option {
return newFuncOption(func(o *options) error {
o.placeholder = p
return nil
})
}
func WithFuncMap(funcs template.FuncMap) Option {
return newFuncOption(func(o *options) error {
if o.funcs == nil {
o.funcs = template.FuncMap{}
}
for k, v := range funcs {
if k == "_sql_parser_" {
return fmt.Errorf("invalid function name, _sql_parser_ is reserved")
}
o.funcs[k] = v
}
return nil
})
}
func defaultOptions() *options {
return &options{
placeholder: Question,
funcs: template.FuncMap{},
}
}