2019-05-31 01:11:13 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-12-03 22:58:05 +03:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
2020-12-03 22:58:05 +03:00
|
|
|
type Options struct {
|
2020-12-04 00:47:59 +03:00
|
|
|
BeforeLoad []func(context.Context, Config) error
|
|
|
|
AfterLoad []func(context.Context, Config) error
|
|
|
|
BeforeSave []func(context.Context, Config) error
|
|
|
|
AfterSave []func(context.Context, Config) error
|
2020-12-03 22:58:05 +03:00
|
|
|
// Struct that holds config data
|
|
|
|
Struct interface{}
|
|
|
|
// struct tag name
|
|
|
|
StructTag string
|
|
|
|
// codec that used for load/save
|
|
|
|
Codec codec.Codec
|
|
|
|
// for alternative data
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:47:59 +03:00
|
|
|
func BeforeLoad(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeLoad = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AfterLoad(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterLoad = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BeforeSave(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeSave = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AfterSave(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterSave= fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-03 22:58:05 +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-03 22:58:05 +03:00
|
|
|
o.Codec = c
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:58:05 +03:00
|
|
|
// Struct
|
|
|
|
func Struct(v interface{}) Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
return func(o *Options) {
|
2020-12-03 22:58:05 +03:00
|
|
|
o.Struct = v
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:58:05 +03:00
|
|
|
// StructTag
|
|
|
|
func StructTag(name string) Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
return func(o *Options) {
|
2020-12-03 22:58:05 +03:00
|
|
|
o.StructTag = name
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|