allow to override Struct on Load #96

Merged
vtolstov merged 2 commits from fix into v3 2023-03-15 00:08:45 +03:00
Showing only changes of commit 39ae0fa6d3 - Show all commits

18
env.go
View File

@ -53,10 +53,15 @@ func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
mopts = append(mopts, mergo.WithAppendSlice) mopts = append(mopts, mergo.WithAppendSlice)
} }
src, err := rutil.Zero(c.opts.Struct) dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}
src, err := rutil.Zero(dst)
if err == nil { if err == nil {
if err = fillValues(ctx, reflect.ValueOf(src), c.opts.StructTag); err == nil { if err = fillValues(ctx, reflect.ValueOf(src), c.opts.StructTag); err == nil {
err = mergo.Merge(c.opts.Struct, src, mopts...) err = mergo.Merge(dst, src, mopts...)
} }
} }
@ -311,11 +316,18 @@ func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) er
} }
func (c *envConfig) Save(ctx context.Context, opts ...config.SaveOption) error { func (c *envConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
options := config.NewSaveOptions(opts...)
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail { if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
if err := c.setValues(ctx, reflect.ValueOf(c.opts.Struct)); err != nil && !c.opts.AllowFail { dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}
if err := c.setValues(ctx, reflect.ValueOf(dst)); err != nil && !c.opts.AllowFail {
return err return err
} }