Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-06-21 16:03:21 +03:00
parent bb0d5a94a0
commit a3438bcc2f
3 changed files with 21 additions and 181 deletions

View File

@@ -82,7 +82,7 @@ func (c *consulConfig) Init(opts ...config.Option) error {
return nil
}
func (c *consulConfig) Load(ctx context.Context) error {
func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
for _, fn := range c.opts.BeforeLoad {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err
@@ -97,11 +97,20 @@ func (c *consulConfig) Load(ctx context.Context) error {
}
if err == nil && pair != nil {
options := config.NewLoadOptions(opts...)
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
if options.Override {
mopts = append(mopts, mergo.WithOverride)
}
if options.Append {
mopts = append(mopts, mergo.WithAppendSlice)
}
src, err := rutil.Zero(c.opts.Struct)
if err == nil {
err = c.opts.Codec.Unmarshal(pair.Value, src)
if err == nil {
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
err = mergo.Merge(c.opts.Struct, src, mopts...)
}
}
@@ -120,7 +129,7 @@ func (c *consulConfig) Load(ctx context.Context) error {
return nil
}
func (c *consulConfig) Save(ctx context.Context) error {
func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
for _, fn := range c.opts.BeforeSave {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err