2020-12-13 13:10:04 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-02-07 06:47:46 +03:00
|
|
|
"time"
|
2021-01-19 01:40:34 +03:00
|
|
|
|
2024-03-06 16:45:19 +03:00
|
|
|
"dario.cat/mergo"
|
2023-07-13 20:26:47 +03:00
|
|
|
"github.com/google/uuid"
|
2024-04-22 08:47:50 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
2023-07-13 20:26:47 +03:00
|
|
|
mid "go.unistack.org/micro/v3/util/id"
|
2021-10-02 19:55:07 +03:00
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2023-02-07 06:47:46 +03:00
|
|
|
mtime "go.unistack.org/micro/v3/util/time"
|
2020-12-13 13:10:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type defaultConfig struct {
|
2024-04-22 08:47:50 +03:00
|
|
|
funcLoad FuncLoad
|
|
|
|
funcSave FuncSave
|
|
|
|
opts Options
|
2020-12-13 13:10:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfig) Options() Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfig) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
2023-03-14 13:23:41 +03:00
|
|
|
|
|
|
|
if err := DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
c.funcLoad = c.fnLoad
|
|
|
|
c.funcSave = c.fnSave
|
|
|
|
|
2024-12-18 20:31:07 +03:00
|
|
|
c.opts.Hooks.EachPrev(func(hook options.Hook) {
|
2024-04-22 08:47:50 +03:00
|
|
|
switch h := hook.(type) {
|
|
|
|
case HookLoad:
|
|
|
|
c.funcLoad = h(c.funcLoad)
|
|
|
|
case HookSave:
|
|
|
|
c.funcSave = h(c.funcSave)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-14 13:23:41 +03:00
|
|
|
if err := DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:57:13 +03:00
|
|
|
func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
return c.funcLoad(ctx, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfig) fnLoad(ctx context.Context, opts ...LoadOption) error {
|
|
|
|
var err error
|
|
|
|
|
2024-01-15 00:46:00 +03:00
|
|
|
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
if err = DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:24:16 +03:00
|
|
|
return err
|
2020-12-18 03:37:18 +03:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:57:13 +03:00
|
|
|
options := NewLoadOptions(opts...)
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:24:40 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := rutil.Zero(dst)
|
2021-09-30 01:24:16 +03:00
|
|
|
if err != nil {
|
|
|
|
if !c.opts.AllowFail {
|
|
|
|
return err
|
2021-01-21 02:20:06 +03:00
|
|
|
}
|
2023-03-14 13:23:41 +03:00
|
|
|
if err = DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-21 02:20:06 +03:00
|
|
|
}
|
2020-12-13 13:10:04 +03:00
|
|
|
|
2021-09-30 01:24:16 +03:00
|
|
|
if err = fillValues(reflect.ValueOf(src), c.opts.StructTag); err == nil {
|
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2020-12-13 13:10:04 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:23:41 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
|
|
|
return err
|
2020-12-18 03:37:18 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 13:23:41 +03:00
|
|
|
if err := DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
|
2021-09-30 01:24:16 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2021-08-03 00:24:40 +03:00
|
|
|
func fillValue(value reflect.Value, val string) error {
|
2021-01-19 01:40:34 +03:00
|
|
|
if !rutil.IsEmpty(value) {
|
2020-12-19 23:22:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-07 06:47:46 +03:00
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
t := value.Type()
|
|
|
|
nvals := strings.FieldsFunc(val, func(c rune) bool { return c == ',' || c == ';' })
|
|
|
|
if value.IsNil() {
|
|
|
|
value.Set(reflect.MakeMapWithSize(t, len(nvals)))
|
|
|
|
}
|
|
|
|
kt := t.Key()
|
|
|
|
et := t.Elem()
|
|
|
|
for _, nval := range nvals {
|
|
|
|
kv := strings.FieldsFunc(nval, func(c rune) bool { return c == '=' })
|
|
|
|
mkey := reflect.Indirect(reflect.New(kt))
|
|
|
|
mval := reflect.Indirect(reflect.New(et))
|
2021-08-03 00:24:40 +03:00
|
|
|
if err := fillValue(mkey, kv[0]); err != nil {
|
2020-12-13 13:10:04 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-08-03 00:24:40 +03:00
|
|
|
if err := fillValue(mval, kv[1]); err != nil {
|
2020-12-13 13:10:04 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.SetMapIndex(mkey, mval)
|
|
|
|
}
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
nvals := strings.FieldsFunc(val, func(c rune) bool { return c == ',' || c == ';' })
|
|
|
|
value.Set(reflect.MakeSlice(reflect.SliceOf(value.Type().Elem()), len(nvals), len(nvals)))
|
|
|
|
for idx, nval := range nvals {
|
|
|
|
nvalue := reflect.Indirect(reflect.New(value.Type().Elem()))
|
2021-08-03 00:24:40 +03:00
|
|
|
if err := fillValue(nvalue, nval); err != nil {
|
2020-12-13 13:10:04 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Index(idx).Set(nvalue)
|
|
|
|
}
|
|
|
|
case reflect.Bool:
|
|
|
|
v, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(v))
|
|
|
|
case reflect.String:
|
2023-07-13 20:26:47 +03:00
|
|
|
switch val {
|
|
|
|
case "micro:generate uuid":
|
|
|
|
uid, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val = uid.String()
|
|
|
|
case "micro:generate id":
|
|
|
|
uid, err := mid.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val = uid
|
|
|
|
}
|
2020-12-13 13:10:04 +03:00
|
|
|
value.Set(reflect.ValueOf(val))
|
|
|
|
case reflect.Float32:
|
|
|
|
v, err := strconv.ParseFloat(val, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(float32(v)))
|
|
|
|
case reflect.Float64:
|
|
|
|
v, err := strconv.ParseFloat(val, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-13 01:46:16 +03:00
|
|
|
value.Set(reflect.ValueOf(v))
|
2020-12-13 13:10:04 +03:00
|
|
|
case reflect.Int:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int(v)))
|
|
|
|
case reflect.Int8:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(v))
|
|
|
|
case reflect.Int16:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int16(v)))
|
|
|
|
case reflect.Int32:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(int32(v)))
|
|
|
|
case reflect.Int64:
|
2023-02-07 06:47:46 +03:00
|
|
|
switch {
|
|
|
|
case value.Type().String() == "time.Duration" && value.Type().PkgPath() == "time":
|
|
|
|
v, err := time.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(v))
|
|
|
|
case value.Type().String() == "time.Duration" && value.Type().PkgPath() == "go.unistack.org/micro/v3/util/time":
|
|
|
|
v, err := mtime.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.SetInt(int64(v))
|
|
|
|
default:
|
|
|
|
v, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(v))
|
2020-12-13 13:10:04 +03:00
|
|
|
}
|
|
|
|
case reflect.Uint:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint(v)))
|
|
|
|
case reflect.Uint8:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint8(v)))
|
|
|
|
case reflect.Uint16:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint16(v)))
|
|
|
|
case reflect.Uint32:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value.Set(reflect.ValueOf(uint32(v)))
|
|
|
|
case reflect.Uint64:
|
|
|
|
v, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-13 01:46:16 +03:00
|
|
|
value.Set(reflect.ValueOf(v))
|
2020-12-13 13:10:04 +03:00
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:24:40 +03:00
|
|
|
func fillValues(valueOf reflect.Value, tname string) error {
|
2020-12-13 13:10:04 +03:00
|
|
|
var values reflect.Value
|
|
|
|
|
|
|
|
if valueOf.Kind() == reflect.Ptr {
|
|
|
|
values = valueOf.Elem()
|
|
|
|
} else {
|
|
|
|
values = valueOf
|
|
|
|
}
|
|
|
|
|
|
|
|
if values.Kind() == reflect.Invalid {
|
|
|
|
return ErrInvalidStruct
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := values.Type()
|
|
|
|
|
|
|
|
for idx := 0; idx < fields.NumField(); idx++ {
|
|
|
|
field := fields.Field(idx)
|
|
|
|
value := values.Field(idx)
|
|
|
|
if !value.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(field.PkgPath) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch value.Kind() {
|
2020-12-13 13:17:38 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
value.Set(reflect.Indirect(reflect.New(value.Type())))
|
2021-08-03 00:24:40 +03:00
|
|
|
if err := fillValues(value, tname); err != nil {
|
2020-12-13 13:17:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2020-12-13 13:10:04 +03:00
|
|
|
case reflect.Ptr:
|
|
|
|
if value.IsNil() {
|
|
|
|
if value.Type().Elem().Kind() != reflect.Struct {
|
|
|
|
// nil pointer to a non-struct: leave it alone
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// nil pointer to struct: create a zero instance
|
|
|
|
value.Set(reflect.New(value.Type().Elem()))
|
|
|
|
}
|
|
|
|
value = value.Elem()
|
2021-08-03 00:24:40 +03:00
|
|
|
if err := fillValues(value, tname); err != nil {
|
2020-12-13 13:10:04 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-08-03 00:24:40 +03:00
|
|
|
tag, ok := field.Tag.Lookup(tname)
|
2020-12-13 13:10:04 +03:00
|
|
|
if !ok {
|
2020-12-13 19:28:29 +03:00
|
|
|
continue
|
2020-12-13 13:10:04 +03:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:24:40 +03:00
|
|
|
if err := fillValue(value, tag); err != nil {
|
2020-12-13 13:10:04 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (c *defaultConfig) Save(ctx context.Context, opts ...SaveOption) error {
|
|
|
|
return c.funcSave(ctx, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfig) fnSave(ctx context.Context, opts ...SaveOption) error {
|
2024-01-15 00:46:00 +03:00
|
|
|
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:24:16 +03:00
|
|
|
if err := DefaultBeforeSave(ctx, c); err != nil {
|
|
|
|
return err
|
2020-12-18 03:37:18 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:24:16 +03:00
|
|
|
if err := DefaultAfterSave(ctx, c); err != nil {
|
|
|
|
return err
|
2020-12-18 03:37:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfig) String() string {
|
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:18:17 +03:00
|
|
|
func (c *defaultConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (c *defaultConfig) Watch(_ context.Context, _ ...WatchOption) (Watcher, error) {
|
2021-11-30 07:34:49 +03:00
|
|
|
return nil, ErrWatcherNotImplemented
|
2021-08-03 00:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 11:28:50 +03:00
|
|
|
// NewConfig returns new default config source
|
2020-12-13 13:10:04 +03:00
|
|
|
func NewConfig(opts ...Option) Config {
|
|
|
|
options := NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = "default"
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
c := &defaultConfig{opts: options}
|
|
|
|
c.funcLoad = c.fnLoad
|
|
|
|
c.funcSave = c.fnSave
|
|
|
|
|
|
|
|
return c
|
2020-12-13 13:10:04 +03:00
|
|
|
}
|