2019-05-30 23:11:13 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-12-04 02:28:45 +03:00
|
|
|
"context"
|
2021-08-03 00:24:40 +03:00
|
|
|
"time"
|
2020-12-04 02:28:45 +03:00
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
2024-04-22 08:47:50 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2019-05-30 23:11:13 +01: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-03-06 19:45:13 +03:00
|
|
|
// Struct holds the destination config struct
|
2020-12-04 02:28:45 +03:00
|
|
|
Struct interface{}
|
2021-01-22 23:32:33 +03:00
|
|
|
// Codec that used for load/save
|
2020-12-04 02:28:45 +03:00
|
|
|
Codec codec.Codec
|
2021-03-06 19:45:13 +03:00
|
|
|
// Tracer that will be used
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Meter that will be used
|
|
|
|
Meter meter.Meter
|
|
|
|
// Logger that will be used
|
|
|
|
Logger logger.Logger
|
|
|
|
// Context used for external options
|
2020-12-04 02:28:45 +03:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name of the config
|
|
|
|
Name string
|
|
|
|
// StructTag name
|
|
|
|
StructTag string
|
2023-03-14 10:38:24 +03:00
|
|
|
// BeforeSave contains slice of funcs that runs before Save
|
2021-03-06 19:45:13 +03:00
|
|
|
BeforeSave []func(context.Context, Config) error
|
2023-03-14 10:38:24 +03:00
|
|
|
// AfterSave contains slice of funcs that runs after Save
|
2021-03-06 19:45:13 +03:00
|
|
|
AfterSave []func(context.Context, Config) error
|
2023-03-14 10:38:24 +03:00
|
|
|
// BeforeLoad contains slice of funcs that runs before Load
|
|
|
|
BeforeLoad []func(context.Context, Config) error
|
|
|
|
// AfterLoad contains slice of funcs that runs after Load
|
|
|
|
AfterLoad []func(context.Context, Config) error
|
|
|
|
// BeforeInit contains slice of funcs that runs before Init
|
|
|
|
BeforeInit []func(context.Context, Config) error
|
|
|
|
// AfterInit contains slice of funcs that runs after Init
|
|
|
|
AfterInit []func(context.Context, Config) error
|
2021-03-06 19:45:13 +03:00
|
|
|
// AllowFail flag to allow fail in config source
|
|
|
|
AllowFail bool
|
2024-01-15 00:46:00 +03:00
|
|
|
// SkipLoad runs only if condition returns true
|
|
|
|
SkipLoad func(context.Context, Config) bool
|
|
|
|
// SkipSave runs only if condition returns true
|
|
|
|
SkipSave func(context.Context, Config) bool
|
2024-04-22 08:47:50 +03:00
|
|
|
// Hooks can be run before/after config Save/Load
|
|
|
|
Hooks options.Hooks
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
|
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-06-20 23:57:13 +03:00
|
|
|
// LoadOption function signature
|
|
|
|
type LoadOption func(o *LoadOptions)
|
|
|
|
|
|
|
|
// LoadOptions struct
|
|
|
|
type LoadOptions struct {
|
2021-08-03 00:24:40 +03:00
|
|
|
Struct interface{}
|
2024-02-08 08:17:53 +03:00
|
|
|
Context context.Context
|
2021-06-20 23:57:13 +03:00
|
|
|
Override bool
|
|
|
|
Append bool
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// NewLoadOptions create LoadOptions struct with provided opts
|
2021-06-20 23:57:13 +03:00
|
|
|
func NewLoadOptions(opts ...LoadOption) LoadOptions {
|
|
|
|
options := LoadOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadOverride override values when load
|
|
|
|
func LoadOverride(b bool) LoadOption {
|
|
|
|
return func(o *LoadOptions) {
|
|
|
|
o.Override = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAppend override values when load
|
|
|
|
func LoadAppend(b bool) LoadOption {
|
|
|
|
return func(o *LoadOptions) {
|
|
|
|
o.Append = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:24:40 +03:00
|
|
|
// LoadStruct override struct for loading
|
|
|
|
func LoadStruct(src interface{}) LoadOption {
|
|
|
|
return func(o *LoadOptions) {
|
|
|
|
o.Struct = src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:57:13 +03:00
|
|
|
// SaveOption function signature
|
|
|
|
type SaveOption func(o *SaveOptions)
|
|
|
|
|
|
|
|
// SaveOptions struct
|
|
|
|
type SaveOptions struct {
|
2021-11-18 15:46:30 +03:00
|
|
|
Struct interface{}
|
|
|
|
Context context.Context
|
2021-06-20 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:24:40 +03:00
|
|
|
// SaveStruct override struct for save to config
|
|
|
|
func SaveStruct(src interface{}) SaveOption {
|
|
|
|
return func(o *SaveOptions) {
|
|
|
|
o.Struct = src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSaveOptions fill SaveOptions struct
|
2021-06-20 23:57:13 +03:00
|
|
|
func NewSaveOptions(opts ...SaveOption) SaveOptions {
|
|
|
|
options := SaveOptions{}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 10:38:24 +03:00
|
|
|
// BeforeInit run funcs before config Init
|
|
|
|
func BeforeInit(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeInit = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterInit run funcs after config Init
|
|
|
|
func AfterInit(fn ...func(context.Context, Config) error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterInit = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-30 23:11:13 +01:00
|
|
|
return func(o *Options) {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.Codec = c
|
2019-05-30 23:11:13 +01: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-30 23:11:13 +01:00
|
|
|
return func(o *Options) {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.Struct = v
|
2019-05-30 23:11:13 +01: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-30 23:11:13 +01:00
|
|
|
return func(o *Options) {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.StructTag = name
|
2019-05-30 23:11:13 +01: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
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 00:24:40 +03:00
|
|
|
|
|
|
|
// WatchOptions struuct
|
|
|
|
type WatchOptions struct {
|
|
|
|
// Context used by non default options
|
|
|
|
Context context.Context
|
2021-09-30 20:32:59 +03:00
|
|
|
// Struct for filling
|
|
|
|
Struct interface{}
|
2021-08-04 00:37:56 +03:00
|
|
|
// MinInterval specifies the min time.Duration interval for poll changes
|
|
|
|
MinInterval time.Duration
|
|
|
|
// MaxInterval specifies the max time.Duration interval for poll changes
|
|
|
|
MaxInterval time.Duration
|
2021-09-30 20:32:59 +03:00
|
|
|
// Coalesce multiple events to one
|
|
|
|
Coalesce bool
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// WatchOption func signature
|
2021-08-03 00:24:40 +03:00
|
|
|
type WatchOption func(*WatchOptions)
|
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// NewWatchOptions create WatchOptions struct with provided opts
|
2021-08-03 00:24:40 +03:00
|
|
|
func NewWatchOptions(opts ...WatchOption) WatchOptions {
|
|
|
|
options := WatchOptions{
|
2021-08-04 00:37:56 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
MinInterval: DefaultWatcherMinInterval,
|
|
|
|
MaxInterval: DefaultWatcherMaxInterval,
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchContext pass context
|
|
|
|
func WatchContext(ctx context.Context) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchCoalesce controls watch event combining
|
|
|
|
func WatchCoalesce(b bool) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Coalesce = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:37:56 +03:00
|
|
|
// WatchInterval specifies min and max time.Duration for pulling changes
|
|
|
|
func WatchInterval(min, max time.Duration) WatchOption {
|
2021-08-03 00:24:40 +03:00
|
|
|
return func(o *WatchOptions) {
|
2021-08-04 00:37:56 +03:00
|
|
|
o.MinInterval = min
|
|
|
|
o.MaxInterval = max
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchStruct overrides struct for fill
|
|
|
|
func WatchStruct(src interface{}) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Struct = src
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
// Hooks sets hook runs before action
|
|
|
|
func Hooks(h ...options.Hook) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Hooks = append(o.Hooks, h...)
|
|
|
|
}
|
|
|
|
}
|