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:39:35 +03:00
parent 55ce153545
commit b3f0be323c

View File

@ -81,6 +81,12 @@ func (c *consulConfig) Init(opts ...config.Option) error {
} }
func (c *consulConfig) Load(ctx context.Context) error { func (c *consulConfig) Load(ctx context.Context) error {
for _, fn := range c.opts.BeforeLoad {
if err := fn(ctx, c); err != nil {
return err
}
}
pair, _, err := c.cli.KV().Get(c.path, nil) pair, _, err := c.cli.KV().Get(c.path, nil)
if err != nil { if err != nil {
return fmt.Errorf("consul path load error: %v", err) return fmt.Errorf("consul path load error: %v", err)
@ -88,10 +94,32 @@ func (c *consulConfig) Load(ctx context.Context) error {
return fmt.Errorf("consul path not found %v", ErrPathNotExist) return fmt.Errorf("consul path not found %v", ErrPathNotExist)
} }
return c.opts.Codec.Unmarshal(pair.Value, c.opts.Struct) if err = c.opts.Codec.Unmarshal(pair.Value, c.opts.Struct); err != nil {
return err
}
for _, fn := range c.opts.AfterLoad {
if err := fn(ctx, c); err != nil {
return err
}
}
return nil
} }
func (c *consulConfig) Save(ctx context.Context) error { func (c *consulConfig) 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
} }