Merge pull request 'allow to override Struct on Save' (#94) from config-improve into v3

Reviewed-on: #94
This commit is contained in:
Василий Толстов 2023-03-15 00:16:41 +03:00
commit 7018485fde
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 {
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
for _, o := range opts {
o(&c.opts)
}
@ -37,17 +41,20 @@ func (c *fileConfig) Init(opts ...config.Option) error {
if c.path == "" {
err := fmt.Errorf("file path not exists: %v", c.path)
c.opts.Logger.Error(c.opts.Context, err)
if !c.opts.AllowFail {
return err
}
}
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil
}
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
}
@ -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))
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
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 config.DefaultAfterLoad(ctx, c)
return nil
}
defer fp.Close()
buf, err := ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
if !c.opts.AllowFail {
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 {
err = c.opts.Codec.Unmarshal(buf, src)
if err == nil {
@ -91,18 +109,15 @@ func (c *fileConfig) Load(ctx context.Context, opts ...config.LoadOption) error
if options.Append {
mopts = append(mopts, mergo.WithAppendSlice)
}
err = mergo.Merge(c.opts.Struct, src, mopts...)
err = mergo.Merge(dst, src, mopts...)
}
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", path, err)
if !c.opts.AllowFail {
return err
}
if err != nil && !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
}
@ -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 {
if err := config.DefaultBeforeSave(ctx, c); err != nil {
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
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 {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
if !c.opts.AllowFail {
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))
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
if !c.opts.AllowFail {
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()
@ -145,14 +171,11 @@ func (c *fileConfig) Save(ctx context.Context, opts ...config.SaveOption) error
err = fp.Close()
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", path, err)
if !c.opts.AllowFail {
return err
}
if err != nil && !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
}

4
go.mod
View File

@ -1,8 +1,8 @@
module go.unistack.org/micro-config-file/v3
go 1.16
go 1.18
require (
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/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=
go.unistack.org/micro/v3 v3.10.14 h1:7fgLpwGlCN67twhwtngJDEQvrMkUBDSA5vzZqxIDqNE=
go.unistack.org/micro/v3 v3.10.14/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
go.unistack.org/micro/v3 v3.10.16 h1:2er/SKKYbV60M+UuJM4eYCF0MZYAIq/yNUrAbTfgq8Q=
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/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=