fixup after/before load/save
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
61
config/noop_test.go
Normal file
61
config/noop_test.go
Normal 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")
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@@ -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
|
||||
|
Reference in New Issue
Block a user