before/after config stuff handled by implementations

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-18 03:40:34 +03:00
parent a90e073b86
commit c65c85b08b

24
env.go
View File

@ -30,12 +30,24 @@ func (c *envConfig) Init(opts ...config.Option) error {
} }
func (c *envConfig) Load(ctx context.Context) error { func (c *envConfig) Load(ctx context.Context) error {
for _, fn := range c.opts.BeforeLoad {
if err := fn(ctx, c); err != nil {
return err
}
}
valueOf := reflect.ValueOf(c.opts.Struct) valueOf := reflect.ValueOf(c.opts.Struct)
if err := c.fillValues(ctx, valueOf); err != nil { if err := c.fillValues(ctx, valueOf); err != nil {
return err return err
} }
for _, fn := range c.opts.AfterLoad {
if err := fn(ctx, c); err != nil {
return err
}
}
return nil return nil
} }
@ -219,6 +231,18 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error
} }
func (c *envConfig) Save(ctx context.Context) error { func (c *envConfig) Save(ctx context.Context) error {
for _, fn := range c.opts.BeforeSave {
if err := fn(ctx, c); err != nil {
return err
}
}
for _, fn := range c.opts.AfterSave {
if err := fn(ctx, c); err != nil {
return err
}
}
return nil return nil
} }