update for latest micro

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-09-30 01:51:18 +03:00
parent 82d9037eed
commit 850ed7e455
3 changed files with 86 additions and 68 deletions

139
file.go
View File

@ -13,9 +13,7 @@ import (
rutil "github.com/unistack-org/micro/v3/util/reflect"
)
var (
DefaultStructTag = "file"
)
var DefaultStructTag = "file"
type fileConfig struct {
opts config.Options
@ -38,89 +36,108 @@ func (c *fileConfig) Init(opts ...config.Option) error {
}
if c.path == "" {
return 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 {
return err
}
}
return nil
}
func (c *fileConfig) 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
}
if err := config.DefaultBeforeLoad(ctx, c); err != nil {
return err
}
fp, err := os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400))
if err != nil && !c.opts.AllowFail {
return fmt.Errorf("failed to open: %s, error: %w", c.path, err)
} else if err == nil {
defer fp.Close()
var buf []byte
buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize)))
if err == nil {
src, err := rutil.Zero(c.opts.Struct)
if err == nil {
err = c.opts.Codec.Unmarshal(buf, src)
if err == nil {
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...)
}
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
if err != nil && !c.opts.AllowFail {
return config.DefaultAfterLoad(ctx, c)
}
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", c.path, err)
if !c.opts.AllowFail {
return err
}
return config.DefaultAfterLoad(ctx, c)
}
src, err := rutil.Zero(c.opts.Struct)
if err == nil {
err = c.opts.Codec.Unmarshal(buf, src)
if err == nil {
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...)
}
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file load path %s error: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
}
for _, fn := range c.opts.AfterLoad {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
if err := config.DefaultAfterLoad(ctx, c); err != nil {
return err
}
return nil
}
func (c *fileConfig) 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
}
}
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
if err == nil {
var fp *os.File
fp, err = os.OpenFile(c.path, os.O_RDONLY, os.FileMode(0400))
if err != nil && c.opts.AllowFail {
return nil
} else if err != nil && !c.opts.AllowFail {
return fmt.Errorf("failed to open: %s, error: %w", c.path, err)
}
if _, werr := fp.Write(buf); werr == nil {
err = fp.Close()
} else {
err = werr
}
}
if err != nil && !c.opts.AllowFail {
if err := config.DefaultBeforeSave(ctx, c); err != nil {
return err
}
for _, fn := range c.opts.AfterSave {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
return config.DefaultAfterSave(ctx, c)
}
fp, err := os.OpenFile(c.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", c.path, err)
if !c.opts.AllowFail {
return err
}
return config.DefaultAfterSave(ctx, c)
}
defer fp.Close()
if _, err = fp.Write(buf); err == nil {
err = fp.Close()
}
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "file save path %s error: %v", c.path, err)
if !c.opts.AllowFail {
return err
}
}
if err := config.DefaultAfterSave(ctx, c); err != nil {
return err
}
return nil

2
go.mod
View File

@ -4,5 +4,5 @@ go 1.16
require (
github.com/imdario/mergo v0.3.12
github.com/unistack-org/micro/v3 v3.7.4
github.com/unistack-org/micro/v3 v3.7.6
)

13
go.sum
View File

@ -1,24 +1,25 @@
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
github.com/unistack-org/micro/v3 v3.7.4 h1:nRk7f58v0VsiW7aKL0UQaFCQxFoursHQLoWdeZ1RYbg=
github.com/unistack-org/micro/v3 v3.7.4/go.mod h1:78vy7ggElJD+ayx8ruhJsY+SkGUupfR32LXrg1UObvg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
github.com/unistack-org/micro/v3 v3.7.6 h1:cobNkaicZR+8nbDWRUmX3/CSLh6ZNSytK2zWth4s4IM=
github.com/unistack-org/micro/v3 v3.7.6/go.mod h1:Ke/8WJlNZi4ZYwL9HcsANAbQ66/HocTBEZM+od99/mM=
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=