forked from MontFerret/ferret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
69 lines (56 loc) · 1.28 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package runtime
import (
"context"
"io"
"os"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type (
Options struct {
params map[string]core.Value
logging *logging.Options
}
Option func(*Options)
)
func NewOptions(setters []Option) *Options {
opts := &Options{
params: make(map[string]core.Value),
logging: &logging.Options{
Writer: os.Stdout,
Level: logging.ErrorLevel,
},
}
for _, setter := range setters {
setter(opts)
}
return opts
}
func WithParam(name string, value interface{}) Option {
return func(options *Options) {
options.params[name] = values.Parse(value)
}
}
func WithParams(params map[string]interface{}) Option {
return func(options *Options) {
for name, value := range params {
options.params[name] = values.Parse(value)
}
}
}
func WithLog(writer io.Writer) Option {
return func(options *Options) {
options.logging.Writer = writer
}
}
func WithLogLevel(lvl logging.Level) Option {
return func(options *Options) {
options.logging.Level = lvl
}
}
func (opts *Options) WithContext(parent context.Context) context.Context {
ctx := core.ParamsWith(parent, opts.params)
ctx = logging.WithContext(ctx, opts.logging)
return ctx
}