Compare commits
8 Commits
v3.10.14
...
2e5e102719
| Author | SHA1 | Date | |
|---|---|---|---|
| 2e5e102719 | |||
| 36e492314d | |||
| 0c78873277 | |||
| 7f57dc09d3 | |||
| 447206d256 | |||
| 33a7feb970 | |||
| 3950f2ed86 | |||
| 68c1048a7d |
@@ -124,33 +124,12 @@ func Validate(ctx context.Context, cfg interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultAfterLoad default func that runs after config load
|
// DefaultBeforeLoad default func that runs before config Load
|
||||||
DefaultAfterLoad = func(ctx context.Context, c Config) error {
|
|
||||||
for _, fn := range c.Options().AfterLoad {
|
|
||||||
if err := fn(ctx, c); err != nil {
|
|
||||||
c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err)
|
|
||||||
if !c.Options().AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// DefaultAfterSave default func that runs after config save
|
|
||||||
DefaultAfterSave = func(ctx context.Context, c Config) error {
|
|
||||||
for _, fn := range c.Options().AfterSave {
|
|
||||||
if err := fn(ctx, c); err != nil {
|
|
||||||
c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err)
|
|
||||||
if !c.Options().AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// DefaultBeforeLoad default func that runs before config load
|
|
||||||
DefaultBeforeLoad = func(ctx context.Context, c Config) error {
|
DefaultBeforeLoad = func(ctx context.Context, c Config) error {
|
||||||
for _, fn := range c.Options().BeforeLoad {
|
for _, fn := range c.Options().BeforeLoad {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if err := fn(ctx, c); err != nil {
|
if err := fn(ctx, c); err != nil {
|
||||||
c.Options().Logger.Errorf(ctx, "%s BeforeLoad err: %v", c.String(), err)
|
c.Options().Logger.Errorf(ctx, "%s BeforeLoad err: %v", c.String(), err)
|
||||||
if !c.Options().AllowFail {
|
if !c.Options().AllowFail {
|
||||||
@@ -160,9 +139,27 @@ var (
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// DefaultBeforeSave default func that runs befora config save
|
// DefaultAfterLoad default func that runs after config Load
|
||||||
|
DefaultAfterLoad = func(ctx context.Context, c Config) error {
|
||||||
|
for _, fn := range c.Options().AfterLoad {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := fn(ctx, c); err != nil {
|
||||||
|
c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err)
|
||||||
|
if !c.Options().AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// DefaultBeforeSave default func that runs befora config Save
|
||||||
DefaultBeforeSave = func(ctx context.Context, c Config) error {
|
DefaultBeforeSave = func(ctx context.Context, c Config) error {
|
||||||
for _, fn := range c.Options().BeforeSave {
|
for _, fn := range c.Options().BeforeSave {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if err := fn(ctx, c); err != nil {
|
if err := fn(ctx, c); err != nil {
|
||||||
c.Options().Logger.Errorf(ctx, "%s BeforeSave err: %v", c.String(), err)
|
c.Options().Logger.Errorf(ctx, "%s BeforeSave err: %v", c.String(), err)
|
||||||
if !c.Options().AllowFail {
|
if !c.Options().AllowFail {
|
||||||
@@ -172,4 +169,49 @@ var (
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
// DefaultAfterSave default func that runs after config Save
|
||||||
|
DefaultAfterSave = func(ctx context.Context, c Config) error {
|
||||||
|
for _, fn := range c.Options().AfterSave {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := fn(ctx, c); err != nil {
|
||||||
|
c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err)
|
||||||
|
if !c.Options().AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// DefaultBeforeInit default func that runs befora config Init
|
||||||
|
DefaultBeforeInit = func(ctx context.Context, c Config) error {
|
||||||
|
for _, fn := range c.Options().BeforeInit {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := fn(ctx, c); err != nil {
|
||||||
|
c.Options().Logger.Errorf(ctx, "%s BeforeInit err: %v", c.String(), err)
|
||||||
|
if !c.Options().AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// DefaultAfterInit default func that runs after config Init
|
||||||
|
DefaultAfterInit = func(ctx context.Context, c Config) error {
|
||||||
|
for _, fn := range c.Options().AfterSave {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := fn(ctx, c); err != nil {
|
||||||
|
c.Options().Logger.Errorf(ctx, "%s AfterInit err: %v", c.String(), err)
|
||||||
|
if !c.Options().AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,11 +24,20 @@ func (c *defaultConfig) Init(opts ...Option) error {
|
|||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&c.opts)
|
o(&c.opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
|
func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
|
||||||
if err := DefaultBeforeLoad(ctx, c); err != nil {
|
if err := DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,21 +60,20 @@ func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
|
|||||||
if !c.opts.AllowFail {
|
if !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return DefaultAfterLoad(ctx, c)
|
if err = DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = fillValues(reflect.ValueOf(src), c.opts.StructTag); err == nil {
|
if err = fillValues(reflect.ValueOf(src), c.opts.StructTag); err == nil {
|
||||||
err = mergo.Merge(dst, src, mopts...)
|
err = mergo.Merge(dst, src, mopts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && !c.opts.AllowFail {
|
||||||
c.opts.Logger.Errorf(ctx, "default load error: %v", err)
|
return err
|
||||||
if !c.opts.AllowFail {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := DefaultAfterLoad(ctx, c); err != nil {
|
if err := DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,14 +28,18 @@ type Options struct {
|
|||||||
Name string
|
Name string
|
||||||
// StructTag name
|
// StructTag name
|
||||||
StructTag string
|
StructTag string
|
||||||
// BeforeSave contains slice of funcs that runs before save
|
// BeforeSave contains slice of funcs that runs before Save
|
||||||
BeforeSave []func(context.Context, Config) error
|
BeforeSave []func(context.Context, Config) error
|
||||||
// AfterLoad contains slice of funcs that runs after load
|
// AfterSave contains slice of funcs that runs after Save
|
||||||
AfterLoad []func(context.Context, Config) error
|
|
||||||
// BeforeLoad contains slice of funcs that runs before load
|
|
||||||
BeforeLoad []func(context.Context, Config) error
|
|
||||||
// AfterSave contains slice of funcs that runs after save
|
|
||||||
AfterSave []func(context.Context, Config) error
|
AfterSave []func(context.Context, Config) error
|
||||||
|
// 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
|
||||||
// AllowFail flag to allow fail in config source
|
// AllowFail flag to allow fail in config source
|
||||||
AllowFail bool
|
AllowFail bool
|
||||||
}
|
}
|
||||||
@@ -131,6 +135,20 @@ func AllowFail(b bool) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BeforeLoad run funcs before config load
|
// BeforeLoad run funcs before config load
|
||||||
func BeforeLoad(fn ...func(context.Context, Config) error) Option {
|
func BeforeLoad(fn ...func(context.Context, Config) error) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
|
|||||||
Reference in New Issue
Block a user