options.go
1.4 KB
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
package interpol
import "io"
// Options contains all options supported by an Interpolator.
type Options struct {
Template io.Reader
Format Func
Output io.Writer
}
// Option is an option that can be applied to an Interpolator.
type Option func(OptionSetter)
// OptionSetter is an interface that contains the setters for all options
// supported by Interpolator.
type OptionSetter interface {
SetTemplate(template io.Reader)
SetFormat(format Func)
SetOutput(output io.Writer)
}
// WithTemplate assigns Template to Options.
func WithTemplate(template io.Reader) Option {
return func(setter OptionSetter) {
setter.SetTemplate(template)
}
}
// WithFormat assigns Format to Options.
func WithFormat(format Func) Option {
return func(setter OptionSetter) {
setter.SetFormat(format)
}
}
// WithOutput assigns Output to Options.
func WithOutput(output io.Writer) Option {
return func(setter OptionSetter) {
setter.SetOutput(output)
}
}
type optionSetter struct {
opts *Options
}
func newOptionSetter(opts *Options) *optionSetter {
return &optionSetter{opts: opts}
}
func (s *optionSetter) SetTemplate(template io.Reader) {
s.opts.Template = template
}
func (s *optionSetter) SetFormat(format Func) {
s.opts.Format = format
}
func (s *optionSetter) SetOutput(output io.Writer) {
s.opts.Output = output
}
func setOptions(opts []Option, setter OptionSetter) {
for _, opt := range opts {
opt(setter)
}
}