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"
|
|
|
|
|
2024-03-06 17:42:16 +03:00
|
|
|
"dario.cat/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"
|
2023-08-15 07:43:43 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-05-05 20:06:27 +03:00
|
|
|
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
|
2023-08-15 07:43:43 +03:00
|
|
|
client pb.ConfigServiceClient
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *serviceConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:43:43 +03:00
|
|
|
func (c *serviceConfig) Init(opts ...options.Option) error {
|
2024-12-12 11:51:13 +03:00
|
|
|
var err error
|
|
|
|
if err = config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-15 01:17:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
for _, o := range opts {
|
2024-12-12 11:51:13 +03:00
|
|
|
if err = o(&c.opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-12-12 11:51:13 +03:00
|
|
|
err = fmt.Errorf("missing client option")
|
2021-09-30 02:02:53 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 01:17:29 +03:00
|
|
|
|
2024-12-12 11:51:13 +03:00
|
|
|
if err = config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-15 01:17:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.service == "" {
|
2024-12-12 11:51:13 +03:00
|
|
|
err = fmt.Errorf("missing Service option")
|
2021-09-30 02:02:53 +03:00
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-15 01:17:29 +03:00
|
|
|
|
2024-12-12 11:51:13 +03:00
|
|
|
if err = config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-15 01:17:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:02:53 +03:00
|
|
|
return nil
|
2021-01-13 09:06:45 +03:00
|
|
|
}
|
|
|
|
|
2023-08-15 07:43:43 +03:00
|
|
|
c.client = pbmicro.NewConfigServiceClient(c.service, cli)
|
2021-01-19 19:29:45 +03:00
|
|
|
|
2024-12-12 11:51:13 +03:00
|
|
|
if err = config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
2023-03-15 01:17:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:06:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:43:43 +03:00
|
|
|
func (c *serviceConfig) Load(ctx context.Context, opts ...options.Option) 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
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:43:43 +03:00
|
|
|
func (c *serviceConfig) Save(ctx context.Context, opts ...options.Option) 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
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:43:43 +03:00
|
|
|
func (c *serviceConfig) Watch(ctx context.Context, opts ...options.Option) (config.Watcher, error) {
|
2021-09-30 02:02:53 +03:00
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:43:43 +03:00
|
|
|
func NewConfig(opts ...options.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}
|
|
|
|
}
|