Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-06-21 16:09:28 +03:00
parent b807ce7996
commit 6f44125bdf
3 changed files with 17 additions and 7 deletions

16
env.go
View File

@@ -32,13 +32,22 @@ func (c *envConfig) Init(opts ...config.Option) error {
return nil
}
func (c *envConfig) Load(ctx context.Context) error {
func (c *envConfig) 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
}
}
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.fillValues(ctx, reflect.ValueOf(src))
@@ -47,7 +56,8 @@ func (c *envConfig) Load(ctx context.Context) error {
return err
}
if err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice); err != nil && !c.opts.AllowFail {
err = mergo.Merge(c.opts.Struct, src, mopts...)
if err != nil && !c.opts.AllowFail {
return err
}
@@ -290,7 +300,7 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error
return nil
}
func (c *envConfig) Save(ctx context.Context) error {
func (c *envConfig) 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