diff --git a/config/noop_test.go b/config/noop_test.go new file mode 100644 index 00000000..9b58926d --- /dev/null +++ b/config/noop_test.go @@ -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") + } + + + +} diff --git a/config/options.go b/config/options.go index 5e50c88c..e6d050cd 100644 --- a/config/options.go +++ b/config/options.go @@ -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 diff --git a/service.go b/service.go index 3dba4c97..dac93600 100644 --- a/service.go +++ b/service.go @@ -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,10 +156,20 @@ 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 {