Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-06-21 16:26:39 +03:00
parent 66ef9a992c
commit c625d497d4
4 changed files with 314 additions and 13 deletions

View File

@@ -101,7 +101,7 @@ func (c *vaultConfig) Init(opts ...config.Option) error {
return nil
}
func (c *vaultConfig) Load(ctx context.Context) error {
func (c *vaultConfig) 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
@@ -123,13 +123,22 @@ func (c *vaultConfig) Load(ctx context.Context) error {
if err == nil && pair != nil && pair.Data != nil {
var data []byte
var src interface{}
data, err = json.Marshal(pair.Data["data"])
if err == nil {
src, err := rutil.Zero(c.opts.Struct)
src, err = rutil.Zero(c.opts.Struct)
if err == nil {
err = c.opts.Codec.Unmarshal(data, src)
if err == nil {
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
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)
}
err = mergo.Merge(c.opts.Struct, src, mopts...)
}
}
}
@@ -147,7 +156,7 @@ func (c *vaultConfig) Load(ctx context.Context) error {
return nil
}
func (c *vaultConfig) Save(ctx context.Context) error {
func (c *vaultConfig) 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