2020-12-04 02:28:45 +03:00
|
|
|
package config_test
|
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2023-02-07 06:47:46 +03:00
|
|
|
"time"
|
2020-12-13 13:10:04 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/config"
|
2023-07-13 20:10:47 +03:00
|
|
|
mid "go.unistack.org/micro/v4/util/id"
|
2023-04-11 22:20:37 +03:00
|
|
|
mtime "go.unistack.org/micro/v4/util/time"
|
2020-12-13 13:10:04 +03:00
|
|
|
)
|
2020-12-04 02:28:45 +03:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
type cfg struct {
|
2023-02-07 06:47:46 +03:00
|
|
|
StringValue string `default:"string_value"`
|
|
|
|
IgnoreValue string `json:"-"`
|
|
|
|
StructValue *cfgStructValue
|
2023-05-09 20:04:15 +03:00
|
|
|
IntValue int `default:"99"`
|
|
|
|
DurationValue time.Duration `default:"10s"`
|
|
|
|
MDurationValue mtime.Duration `default:"10s"`
|
|
|
|
MapValue map[string]bool `default:"key1=true,key2=false"`
|
2023-07-13 20:10:47 +03:00
|
|
|
UUIDValue string `default:"micro:generate uuid"`
|
|
|
|
IDValue string `default:"micro:generate id"`
|
2022-03-22 14:54:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type cfgStructValue struct {
|
|
|
|
StringValue string `default:"string_value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfg) Validate() error {
|
|
|
|
if c.IntValue != 10 {
|
|
|
|
return fmt.Errorf("invalid IntValue %d != %d", 10, c.IntValue)
|
2020-12-13 13:17:38 +03:00
|
|
|
}
|
2024-04-05 10:28:05 +03:00
|
|
|
if c.MapValue["key1"] != true {
|
|
|
|
return fmt.Errorf("invalid MapValue %t != %t", true, c.MapValue["key1"])
|
|
|
|
}
|
2022-03-22 14:54:43 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfgStructValue) Validate() error {
|
|
|
|
if c.StringValue != "string_value" {
|
|
|
|
return fmt.Errorf("invalid StringValue %s != %s", "string_value", c.StringValue)
|
|
|
|
}
|
|
|
|
return nil
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
|
2020-12-13 13:10:04 +03:00
|
|
|
func TestDefault(t *testing.T) {
|
2020-12-04 02:28:45 +03:00
|
|
|
ctx := context.Background()
|
2022-03-22 14:54:43 +03:00
|
|
|
conf := &cfg{IntValue: 10}
|
|
|
|
blfn := func(_ context.Context, c config.Config) error {
|
|
|
|
nconf, ok := c.Options().Struct.(*cfg)
|
2020-12-04 02:28:45 +03:00
|
|
|
if !ok {
|
2022-03-22 14:54:43 +03:00
|
|
|
return fmt.Errorf("failed to get Struct from options: %v", c.Options())
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
nconf.StringValue = "before_load"
|
2020-12-04 02:28:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-22 14:54:43 +03:00
|
|
|
alfn := func(_ context.Context, c config.Config) error {
|
|
|
|
nconf, ok := c.Options().Struct.(*cfg)
|
2020-12-04 02:28:45 +03:00
|
|
|
if !ok {
|
2022-03-22 14:54:43 +03:00
|
|
|
return fmt.Errorf("failed to get Struct from options: %v", c.Options())
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
nconf.StringValue = "after_load"
|
2020-12-04 02:28:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:41:14 +03:00
|
|
|
cfg := config.NewConfig(config.Struct(conf), config.BeforeLoad(blfn), config.AfterLoad(alfn))
|
2020-12-04 02:28:45 +03:00
|
|
|
if err := cfg.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := cfg.Load(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-12-13 13:10:04 +03:00
|
|
|
if conf.StringValue != "after_load" {
|
2020-12-04 02:28:45 +03:00
|
|
|
t.Fatal("AfterLoad option not working")
|
|
|
|
}
|
2023-05-09 20:04:15 +03:00
|
|
|
if len(conf.MapValue) != 2 {
|
|
|
|
t.Fatalf("map value invalid: %#+v\n", conf.MapValue)
|
|
|
|
}
|
2023-07-13 20:10:47 +03:00
|
|
|
|
|
|
|
if conf.UUIDValue == "" {
|
|
|
|
t.Fatalf("uuid value empty")
|
|
|
|
} else if len(conf.UUIDValue) != 36 {
|
|
|
|
t.Fatalf("uuid value invalid: %s", conf.UUIDValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.IDValue == "" {
|
|
|
|
t.Fatalf("id value empty")
|
|
|
|
} else if len(conf.IDValue) != mid.DefaultSize {
|
|
|
|
t.Fatalf("id value invalid: %s", conf.IDValue)
|
|
|
|
}
|
2021-08-03 00:24:40 +03:00
|
|
|
_ = conf
|
2021-09-28 23:43:43 +03:00
|
|
|
// t.Logf("%#+v\n", conf)
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
2022-03-22 14:54:43 +03:00
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
conf := &cfg{IntValue: 10}
|
|
|
|
cfg := config.NewConfig(config.Struct(conf))
|
|
|
|
if err := cfg.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := cfg.Load(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.Validate(ctx, conf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 10:28:05 +03:00
|
|
|
|
|
|
|
func TestString(t *testing.T) {
|
|
|
|
cfg := config.NewConfig()
|
|
|
|
res := cfg.String()
|
|
|
|
if res != "default" {
|
|
|
|
t.Fatalf("string value invalid: %s", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestName(t *testing.T) {
|
|
|
|
cfg := config.NewConfig()
|
|
|
|
res := cfg.Name()
|
|
|
|
if res != "" {
|
|
|
|
t.Fatal("name value not empty")
|
|
|
|
}
|
|
|
|
}
|