Compare commits

...

7 Commits
v3.8.4 ... v3

Author SHA1 Message Date
d4707d7b1d update deps
Some checks failed
build / test (push) Failing after 1m36s
codeql / analyze (go) (push) Failing after 2m9s
build / lint (push) Successful in 9m46s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-06 17:26:17 +03:00
b7d4e2e11e fixup conditions
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-01-15 01:58:06 +03:00
d2c852851e Merge pull request 'allow to override Struct on Load' (#96) from fix into v3
Reviewed-on: #96
2023-03-15 00:08:44 +03:00
087cc4e2ff Merge branch 'v3' into fix 2023-03-15 00:07:21 +03:00
39ae0fa6d3 allow to override Struct on Load
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-03-15 00:06:55 +03:00
ee51d15152 Merge pull request 'config: proper handle AllowFail option' (#94) from config-fix into v3
Reviewed-on: #94
2023-03-14 14:04:37 +03:00
8f36e9db79 config: proper handle AllowFail option
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-03-14 13:58:03 +03:00
3 changed files with 45 additions and 30 deletions

54
env.go
View File

@ -1,4 +1,4 @@
package env // import "go.unistack.org/micro-config-env/v3"
package env
import (
"context"
@ -8,7 +8,7 @@ import (
"strconv"
"strings"
"github.com/imdario/mergo"
"dario.cat/mergo"
"go.unistack.org/micro/v3/config"
rutil "go.unistack.org/micro/v3/util/reflect"
)
@ -28,11 +28,11 @@ func (c *envConfig) Init(opts ...config.Option) error {
o(&c.opts)
}
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil {
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil {
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
@ -40,7 +40,11 @@ func (c *envConfig) Init(opts ...config.Option) error {
}
func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
return nil
}
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
@ -53,21 +57,23 @@ func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
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 = fillValues(ctx, reflect.ValueOf(src), c.opts.StructTag); err == nil {
err = mergo.Merge(c.opts.Struct, src, mopts...)
err = mergo.Merge(dst, src, mopts...)
}
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "env load err: %v", 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
}
@ -314,18 +320,26 @@ func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) er
}
func (c *envConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
if err := config.DefaultBeforeSave(ctx, c); err != nil {
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
return nil
}
options := config.NewSaveOptions(opts...)
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
if err := c.setValues(ctx, reflect.ValueOf(c.opts.Struct)); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "env save error: %v", err)
if !c.opts.AllowFail {
return err
}
dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}
if err := config.DefaultAfterSave(ctx, c); err != nil {
if err := c.setValues(ctx, reflect.ValueOf(dst)); err != nil && !c.opts.AllowFail {
return err
}
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
return err
}

8
go.mod
View File

@ -1,8 +1,10 @@
module go.unistack.org/micro-config-env/v3
go 1.18
go 1.20
require (
github.com/imdario/mergo v0.3.13
go.unistack.org/micro/v3 v3.10.15
dario.cat/mergo v1.0.0
go.unistack.org/micro/v3 v3.10.44
)
require github.com/google/uuid v1.6.0 // indirect

13
go.sum
View File

@ -1,10 +1,9 @@
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.15 h1:vliCUhERMuDB08+XX5V6I+9YZl4jPUaBo3S+Jx1JLSo=
go.unistack.org/micro/v3 v3.10.15/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
go.unistack.org/micro/v3 v3.10.44 h1:Vgyy9BrJOSdFvo29/klrgIBE/Nme9E8udPAljos34o0=
go.unistack.org/micro/v3 v3.10.44/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
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=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=