2019-05-31 01:11:13 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-12-04 02:28:45 +03:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2020-12-07 19:27:08 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Options hold the config options
|
2020-12-04 02:28:45 +03:00
|
|
|
type Options struct {
|
2021-01-29 14:07:35 +03:00
|
|
|
Name string
|
2020-12-19 23:22:05 +03:00
|
|
|
AllowFail bool
|
2020-12-04 02:28:45 +03:00
|
|
|
BeforeLoad []func(context.Context, Config) error
|
2020-12-07 19:27:08 +03:00
|
|
|
AfterLoad []func(context.Context, Config) error
|
2020-12-04 02:28:45 +03:00
|
|
|
BeforeSave []func(context.Context, Config) error
|
2020-12-07 19:27:08 +03:00
|
|
|
AfterSave []func(context.Context, Config) error
|
2020-12-04 02:28:45 +03:00
|
|
|
// Struct that holds config data
|
|
|
|
Struct interface{}
|
2021-01-22 23:32:33 +03:00
|
|
|
// StructTag name
|
2020-12-04 02:28:45 +03:00
|
|
|
StructTag string
|
2021-01-22 23:32:33 +03:00
|
|
|
// Logger that will be used
|
2020-12-07 19:27:08 +03:00
|
|
|
Logger logger.Logger
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter that will be used
|
|
|
|
Meter meter.Meter
|
|
|
|
// Tracer used for trace
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Codec that used for load/save
|
2020-12-04 02:28:45 +03:00
|
|
|
Codec codec.Codec
|
2021-01-22 23:32:33 +03:00
|
|
|
// Context for alternative data
|
2020-12-04 02:28:45 +03:00
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Option function signature
|
2020-12-04 02:28:45 +03:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// NewOptions new options struct with filed values
|
2020-12-04 02:28:45 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2020-12-09 12:10:25 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
2020-12-04 02:28:45 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// AllowFail allows config source to fail
|
2020-12-19 23:22:05 +03:00
|
|
|
func AllowFail(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AllowFail = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// BeforeLoad run funcs before config load
|
2020-12-04 02:28:45 +03:00
|
|
|
func BeforeLoad(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeLoad = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// AfterLoad run funcs after config load
|
2020-12-04 02:28:45 +03:00
|
|
|
func AfterLoad(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterLoad = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// BeforeSave run funcs before save
|
2020-12-04 02:28:45 +03:00
|
|
|
func BeforeSave(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeSave = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// AfterSave run fncs after save
|
2020-12-04 02:28:45 +03:00
|
|
|
func AfterSave(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
2020-12-07 19:27:08 +03:00
|
|
|
o.AfterSave = fn
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Context pass context
|
2020-12-04 02:28:45 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Codec sets the source codec
|
|
|
|
func Codec(c codec.Codec) Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
return func(o *Options) {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.Codec = c
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// Logger sets the logger
|
2020-12-07 19:27:08 +03:00
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:27:08 +03:00
|
|
|
// Struct used as config
|
2020-12-04 02:28:45 +03:00
|
|
|
func Struct(v interface{}) Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
return func(o *Options) {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.Struct = v
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// StructTag sets the struct tag that used for filling
|
2020-12-04 02:28:45 +03:00
|
|
|
func StructTag(name string) Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
return func(o *Options) {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.StructTag = name
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|