2023-05-05 20:06:27 +03:00
|
|
|
package service // import "go.unistack.org/micro-config-service/v4"
|
2021-01-13 09:06:45 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
2023-05-05 20:06:27 +03:00
|
|
|
pbmicro "go.unistack.org/micro-config-service/v4/micro"
|
|
|
|
pb "go.unistack.org/micro-config-service/v4/proto"
|
|
|
|
"go.unistack.org/micro/v4/client"
|
|
|
|
"go.unistack.org/micro/v4/config"
|
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2021-01-13 09:06:45 +03:00
|
|
|
)
|
|
|
|
|
2022-03-13 11:43:23 +03:00
|
|
|
var _ config.Config = &serviceConfig{}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
var DefaultStructTag = "service"
|
2021-01-13 09:06:45 +03:00
|
|
|
|
|
|
|
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 {
|
2023-03-15 01:17:29 +03:00
|
|
|
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
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 {
|
2021-09-30 02:02:53 +03:00
|
|
|
err := fmt.Errorf("missing client option")
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 01:17:29 +03:00
|
|
|
|
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.service == "" {
|
2021-09-30 02:02:53 +03:00
|
|
|
err := fmt.Errorf("missing Service option")
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 01:17:29 +03:00
|
|
|
|
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-22 08:59:52 +03:00
|
|
|
c.client = pbmicro.NewConfigClient(c.service, cli)
|
2021-01-19 19:29:45 +03:00
|
|
|
|
2023-03-15 01:17:29 +03:00
|
|
|
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:32 +03:00
|
|
|
func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
2023-03-15 01:17:29 +03:00
|
|
|
if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 02:02:53 +03:00
|
|
|
return err
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service})
|
2021-09-30 02:02:53 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
|
|
}
|
2023-03-15 01:17:29 +03:00
|
|
|
|
|
|
|
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:32 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:17:29 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := rutil.Zero(dst)
|
2021-01-13 09:06:45 +03:00
|
|
|
if err == nil {
|
|
|
|
err = c.opts.Codec.Unmarshal(rsp.Config, src)
|
|
|
|
if err == nil {
|
2023-03-15 01:17:29 +03:00
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:17:29 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to load config: %w", err)
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 01:17:29 +03:00
|
|
|
if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 02:02:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:32 +03:00
|
|
|
func (c *serviceConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2023-03-15 01:17:29 +03:00
|
|
|
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 02:02:53 +03:00
|
|
|
return err
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 01:17:29 +03:00
|
|
|
options := config.NewSaveOptions(opts...)
|
|
|
|
|
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := c.opts.Codec.Marshal(dst)
|
2021-09-30 02:02:53 +03:00
|
|
|
if err == nil {
|
|
|
|
_, err = c.client.Save(ctx, &pb.SaveRequest{Service: c.service, Config: buf})
|
2021-05-09 18:47:50 +03:00
|
|
|
}
|
2023-03-15 01:17:29 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return fmt.Errorf("failed to save config: %w", err)
|
2021-05-09 18:47:50 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 01:17:29 +03:00
|
|
|
if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 02:02:53 +03:00
|
|
|
return err
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-30 02:02:53 +03:00
|
|
|
func (c *serviceConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2022-03-13 11:43:23 +03:00
|
|
|
func NewConfig(opts ...config.Option) *serviceConfig {
|
2021-01-13 09:06:45 +03:00
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &serviceConfig{opts: options}
|
|
|
|
}
|