2021-01-13 09:06:45 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
2021-03-22 08:59:52 +03:00
|
|
|
pbmicro "github.com/unistack-org/micro-config-service/v3/micro"
|
2021-01-16 02:15:17 +03:00
|
|
|
pb "github.com/unistack-org/micro-config-service/v3/proto"
|
2021-01-19 19:29:45 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
2021-01-13 09:06:45 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config"
|
2021-01-19 03:18:11 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2021-01-13 09:06:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultStructTag = "service"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serviceConfig struct {
|
|
|
|
opts config.Options
|
|
|
|
service string
|
2021-03-22 08:59:52 +03:00
|
|
|
client pbmicro.ConfigClient
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Init(opts ...config.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:29:45 +03:00
|
|
|
var cli client.Client
|
2021-01-13 09:06:45 +03:00
|
|
|
if c.opts.Context != nil {
|
2021-01-19 19:29:45 +03:00
|
|
|
if v, ok := c.opts.Context.Value(serviceKey{}).(string); ok && v != "" {
|
2021-01-13 09:06:45 +03:00
|
|
|
c.service = v
|
|
|
|
}
|
2021-01-19 19:29:45 +03:00
|
|
|
if v, ok := c.opts.Context.Value(clientKey{}).(client.Client); ok {
|
|
|
|
cli = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli == nil {
|
|
|
|
return fmt.Errorf("missing Client option")
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.service == "" {
|
|
|
|
return fmt.Errorf("missing Service option")
|
|
|
|
}
|
|
|
|
|
2021-03-22 08:59:52 +03:00
|
|
|
c.client = pbmicro.NewConfigClient(c.service, cli)
|
2021-01-19 19:29:45 +03:00
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Load(ctx context.Context) error {
|
|
|
|
for _, fn := range c.opts.BeforeLoad {
|
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service})
|
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to load error config: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-19 03:18:11 +03:00
|
|
|
src, err := rutil.Zero(c.opts.Struct)
|
2021-01-13 09:06:45 +03:00
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(rsp.Config, src)
|
|
|
|
if err == nil {
|
|
|
|
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range c.opts.AfterLoad {
|
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Save(ctx context.Context) error {
|
|
|
|
for _, fn := range c.opts.BeforeSave {
|
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range c.opts.AfterSave {
|
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) String() string {
|
|
|
|
return "service"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:25:37 +03:00
|
|
|
func (c *serviceConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &serviceConfig{opts: options}
|
|
|
|
}
|