github.com/MontFerret/ferret@v0.18.0/pkg/runtime/options.go (about) 1 package runtime 2 3 import ( 4 "context" 5 "io" 6 "os" 7 8 "github.com/MontFerret/ferret/pkg/runtime/core" 9 "github.com/MontFerret/ferret/pkg/runtime/logging" 10 "github.com/MontFerret/ferret/pkg/runtime/values" 11 ) 12 13 type ( 14 Options struct { 15 params map[string]core.Value 16 logging logging.Options 17 } 18 19 Option func(*Options) 20 ) 21 22 func NewOptions(setters []Option) *Options { 23 opts := &Options{ 24 params: make(map[string]core.Value), 25 logging: logging.Options{ 26 Writer: os.Stdout, 27 Level: logging.ErrorLevel, 28 }, 29 } 30 31 for _, setter := range setters { 32 setter(opts) 33 } 34 35 return opts 36 } 37 38 func WithParam(name string, value interface{}) Option { 39 return func(options *Options) { 40 options.params[name] = values.Parse(value) 41 } 42 } 43 44 func WithParams(params map[string]interface{}) Option { 45 return func(options *Options) { 46 for name, value := range params { 47 options.params[name] = values.Parse(value) 48 } 49 } 50 } 51 52 func WithLog(writer io.Writer) Option { 53 return func(options *Options) { 54 options.logging.Writer = writer 55 } 56 } 57 58 func WithLogLevel(lvl logging.Level) Option { 59 return func(options *Options) { 60 options.logging.Level = lvl 61 } 62 } 63 64 func WithLogFields(fields map[string]interface{}) Option { 65 return func(options *Options) { 66 options.logging.Fields = fields 67 } 68 } 69 70 func (opts *Options) WithContext(parent context.Context) context.Context { 71 ctx := core.ParamsWith(parent, opts.params) 72 ctx = logging.WithContext(ctx, opts.logging) 73 74 return ctx 75 }