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
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/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
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// NewOptions new options struct with filed values
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2020-12-04 02:28:45 +03:00
|
|
|
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
|
|
|
// LoadOptions struct
|
|
|
|
type LoadOptions struct {
|
2021-08-03 00:24:40 +03:00
|
|
|
Struct interface{}
|
2023-09-23 23:43:16 +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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewLoadOptions(opts ...options.Option) LoadOptions {
|
2021-06-20 23:57:13 +03:00
|
|
|
options := LoadOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadOverride override values when load
|
2023-07-29 00:40:58 +03:00
|
|
|
func LoadOverride(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".Override")
|
2021-06-20 23:57:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAppend override values when load
|
2023-07-29 00:40:58 +03:00
|
|
|
func LoadAppend(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".Append")
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:57:13 +03:00
|
|
|
// 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
|
|
|
// NewSaveOptions fill SaveOptions struct
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewSaveOptions(opts ...options.Option) SaveOptions {
|
2021-06-20 23:57:13 +03:00
|
|
|
options := SaveOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// AllowFail allows config source to fail
|
2023-07-29 00:40:58 +03:00
|
|
|
func AllowFail(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".AllowFail")
|
2020-12-19 23:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 10:38:24 +03:00
|
|
|
// BeforeInit run funcs before config Init
|
2023-07-29 00:40:58 +03:00
|
|
|
func BeforeInit(fn ...func(context.Context, Config) error) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fn, ".BeforeInit")
|
2023-03-14 10:38:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterInit run funcs after config Init
|
2023-07-29 00:40:58 +03:00
|
|
|
func AfterInit(fn ...func(context.Context, Config) error) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fn, ".AfterInit")
|
2023-03-14 10:38:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// BeforeLoad run funcs before config load
|
2023-07-29 00:40:58 +03:00
|
|
|
func BeforeLoad(fn ...func(context.Context, Config) error) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fn, ".BeforeLoad")
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// AfterLoad run funcs after config load
|
2023-07-29 00:40:58 +03:00
|
|
|
func AfterLoad(fn ...func(context.Context, Config) error) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fn, ".AfterLoad")
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// BeforeSave run funcs before save
|
2023-07-29 00:40:58 +03:00
|
|
|
func BeforeSave(fn ...func(context.Context, Config) error) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fn, ".BeforeSave")
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// AfterSave run fncs after save
|
2023-07-29 00:40:58 +03:00
|
|
|
func AfterSave(fn ...func(context.Context, Config) error) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, fn, ".AfterSave")
|
2021-01-22 23:32:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:27:08 +03:00
|
|
|
// Struct used as config
|
2023-07-29 00:40:58 +03:00
|
|
|
func Struct(v interface{}) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, v, ".Struct")
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func StructTag(name string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, name, ".StructTag")
|
2021-01-29 14:07:35 +03:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
// NewWatchOptions create WatchOptions struct with provided opts
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewWatchOptions(opts ...options.Option) WatchOptions {
|
2021-08-03 00:24:40 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// Coalesce controls watch event combining
|
2023-08-15 07:55:26 +03:00
|
|
|
func WatchCoalesce(b bool) options.Option {
|
2023-07-29 00:40:58 +03:00
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".Coalesce")
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:37:56 +03:00
|
|
|
// WatchInterval specifies min and max time.Duration for pulling changes
|
2023-07-29 00:40:58 +03:00
|
|
|
func WatchInterval(min, max time.Duration) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
var err error
|
|
|
|
if err = options.Set(src, min, ".MinInterval"); err == nil {
|
|
|
|
err = options.Set(src, max, ".MaxInterval")
|
|
|
|
}
|
|
|
|
return err
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
}
|