allow to override Struct on Save #94

Merged
vtolstov merged 1 commits from config-improve into v3 2023-03-15 00:16:41 +03:00
3 changed files with 53 additions and 30 deletions

75
file.go
View File

@ -25,6 +25,10 @@ func (c *fileConfig) Options() config.Options {
} }
func (c *fileConfig) Init(opts ...config.Option) error { func (c *fileConfig) Init(opts ...config.Option) error {
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
for _, o := range opts { for _, o := range opts {
o(&c.opts) o(&c.opts)
} }
@ -37,17 +41,20 @@ func (c *fileConfig) Init(opts ...config.Option) error {
if c.path == "" { if c.path == "" {
err := fmt.Errorf("file path not exists: %v", c.path) err := fmt.Errorf("file path not exists: %v", c.path)
c.opts.Logger.Error(c.opts.Context, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
} }
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil return nil
} }
func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error { func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
if err := config.DefaultBeforeLoad(ctx, c); err != nil { if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@ -61,25 +68,36 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0400)) fp, err := os.OpenFile(path, os.O_RDONLY, os.FileMode(0400))
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return fmt.Errorf("file load path %s error: %w", path, err)
}
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
return config.DefaultAfterLoad(ctx, c)
return nil
} }
defer fp.Close() defer fp.Close()
buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize))) buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
return config.DefaultAfterLoad(ctx, c) if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
return nil
} }
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 {
err = c.opts.Codec.Unmarshal(buf, src) err = c.opts.Codec.Unmarshal(buf, src)
if err == nil { if err == nil {
@ -91,18 +109,15 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
if options.Append { if options.Append {
mopts = append(mopts, mergo.WithAppendSlice) mopts = append(mopts, mergo.WithAppendSlice)
} }
err = mergo.Merge(c.opts.Struct, src, mopts...) err = mergo.Merge(dst, src, mopts...)
} }
} }
if err != nil { if err != nil && !c.opts.AllowFail {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err) return err
if !c.opts.AllowFail {
return err
}
} }
if err := config.DefaultAfterLoad(ctx, c); err != nil { if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@ -110,7 +125,7 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
} }
func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error { func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
if err := config.DefaultBeforeSave(ctx, c); err != nil { if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@ -122,22 +137,33 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
} }
} }
buf, err := c.opts.Codec.Marshal(c.opts.Struct) dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}
buf, err := c.opts.Codec.Marshal(dst)
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
return config.DefaultAfterSave(ctx, c) if err = config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
return nil
} }
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600)) fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.FileMode(0600))
if err != nil { if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
return config.DefaultAfterSave(ctx, c) if err = config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
return nil
} }
defer fp.Close() defer fp.Close()
@ -145,14 +171,11 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
err = fp.Close() err = fp.Close()
} }
if err != nil { if err != nil && !c.opts.AllowFail {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err) return err
if !c.opts.AllowFail {
return err
}
} }
if err := config.DefaultAfterSave(ctx, c); err != nil { if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }

4
go.mod
View File

@ -1,8 +1,8 @@
module go.unistack.org/micro-config-file/v3 module go.unistack.org/micro-config-file/v3
go 1.16 go 1.18
require ( require (
github.com/imdario/mergo v0.3.13 github.com/imdario/mergo v0.3.13
go.unistack.org/micro/v3 v3.10.14 go.unistack.org/micro/v3 v3.10.16
) )

4
go.sum
View File

@ -2,8 +2,8 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
go.unistack.org/micro/v3 v3.10.14 h1:7fgLpwGlCN67twhwtngJDEQvrMkUBDSA5vzZqxIDqNE= go.unistack.org/micro/v3 v3.10.16 h1:2er/SKKYbV60M+UuJM4eYCF0MZYAIq/yNUrAbTfgq8Q=
go.unistack.org/micro/v3 v3.10.14/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q= go.unistack.org/micro/v3 v3.10.16/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=