fixup after/before load/save

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-04 00:47:59 +03:00
parent 6d0d8c1de3
commit e9e861a688
3 changed files with 104 additions and 5 deletions

61
config/noop_test.go Normal file
View File

@ -0,0 +1,61 @@
package config_test
import "testing"
import "context"
import "fmt"
import "github.com/unistack-org/micro/v3/config"
type Cfg struct {
Value string
}
func TestNoop(t *testing.T) {
ctx := context.Background()
conf := &Cfg{}
blfn := func(ctx context.Context, cfg config.Config) error {
conf, ok := cfg.Options().Struct.(*Cfg)
if !ok {
return fmt.Errorf("failed to get Struct from options: %v", cfg.Options())
}
conf.Value = "before_load"
return nil
}
alfn := func(ctx context.Context, cfg config.Config) error {
conf, ok := cfg.Options().Struct.(*Cfg)
if !ok {
return fmt.Errorf("failed to get Struct from options: %v", cfg.Options())
}
conf.Value = "after_load"
return nil
}
cfg := config.NewConfig(config.Struct(conf),config.BeforeLoad(blfn),config.AfterLoad(alfn))
if err := cfg.Init(); err != nil {
t.Fatal(err)
}
for _, fn := range cfg.Options().BeforeLoad {
if err := fn(ctx, cfg); err != nil {
t.Fatal(err)
}
}
if conf.Value != "before_load" {
t.Fatal("BeforeLoad option not working")
}
if err := cfg.Load(ctx); err != nil {
t.Fatal(err)
}
for _, fn := range cfg.Options().AfterLoad {
if err := fn(ctx, cfg); err != nil {
t.Fatal(err)
}
}
if conf.Value != "after_load" {
t.Fatal("AfterLoad option not working")
}
}

View File

@ -7,6 +7,10 @@ import (
)
type Options struct {
BeforeLoad []func(context.Context, Config) error
AfterLoad []func(context.Context, Config) error
BeforeSave []func(context.Context, Config) error
AfterSave []func(context.Context, Config) error
// Struct that holds config data
Struct interface{}
// struct tag name
@ -30,6 +34,32 @@ func NewOptions(opts ...Option) Options {
return options
}
func BeforeLoad(fn ...func(context.Context, Config) error) Option {
return func(o *Options) {
o.BeforeLoad = fn
}
}
func AfterLoad(fn ...func(context.Context, Config) error) Option {
return func(o *Options) {
o.AfterLoad = fn
}
}
func BeforeSave(fn ...func(context.Context, Config) error) Option {
return func(o *Options) {
o.BeforeSave = fn
}
}
func AfterSave(fn ...func(context.Context, Config) error) Option {
return func(o *Options) {
o.AfterSave= fn
}
}
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx

View File

@ -50,9 +50,7 @@ func (s *service) Init(opts ...Option) error {
if s.opts.Configs != nil {
for _, c := range s.opts.Configs {
if err := c.Init(
config.Context(s.opts.Context),
); err != nil {
if err := c.Init(config.Context(s.opts.Context) ); err != nil {
return err
}
}
@ -158,11 +156,21 @@ func (s *service) Start() error {
}
}
for idx := 0; idx < len(s.opts.Configs); idx++ {
if err := s.opts.Configs[idx].Load(s.opts.Context); err != nil {
for _, cfg := range s.opts.Configs {
for _, fn := range cfg.Options().BeforeLoad {
if err := fn(s.opts.Context, cfg); err != nil {
return err
}
}
if err := cfg.Load(s.opts.Context); err != nil {
return err
}
for _, fn := range cfg.Options().AfterLoad {
if err := fn(s.opts.Context, cfg); err != nil {
return err
}
}
}
if s.opts.Server == nil {
return fmt.Errorf("cant start nil server")