From 9e0e657003fc2160b8ec1c24666424f50fc24434 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 13 Jul 2023 20:26:47 +0300 Subject: [PATCH] config/default: add micro:generate uuid/id Signed-off-by: Vasiliy Tolstov --- config/default.go | 16 ++++++++++++++++ config/default_test.go | 25 ++++++++++++++++++++++--- go.mod | 3 ++- go.sum | 6 ++++-- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/config/default.go b/config/default.go index b28269f4..179fc9ba 100644 --- a/config/default.go +++ b/config/default.go @@ -7,7 +7,9 @@ import ( "strings" "time" + "github.com/google/uuid" "github.com/imdario/mergo" + mid "go.unistack.org/micro/v3/util/id" rutil "go.unistack.org/micro/v3/util/reflect" mtime "go.unistack.org/micro/v3/util/time" ) @@ -124,6 +126,20 @@ func fillValue(value reflect.Value, val string) error { } value.Set(reflect.ValueOf(v)) case reflect.String: + 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 + } value.Set(reflect.ValueOf(val)) case reflect.Float32: v, err := strconv.ParseFloat(val, 32) diff --git a/config/default_test.go b/config/default_test.go index 180d687e..40ee6924 100644 --- a/config/default_test.go +++ b/config/default_test.go @@ -7,6 +7,7 @@ import ( "time" "go.unistack.org/micro/v3/config" + mid "go.unistack.org/micro/v3/util/id" mtime "go.unistack.org/micro/v3/util/time" ) @@ -14,9 +15,12 @@ type cfg struct { StringValue string `default:"string_value"` IgnoreValue string `json:"-"` StructValue *cfgStructValue - IntValue int `default:"99"` - DurationValue time.Duration `default:"10s"` - MDurationValue mtime.Duration `default:"10s"` + IntValue int `default:"99"` + DurationValue time.Duration `default:"10s"` + MDurationValue mtime.Duration `default:"10s"` + MapValue map[string]bool `default:"key1=true,key2=false"` + UUIDValue string `default:"micro:generate uuid"` + IDValue string `default:"micro:generate id"` } type cfgStructValue struct { @@ -67,6 +71,21 @@ func TestDefault(t *testing.T) { if conf.StringValue != "after_load" { t.Fatal("AfterLoad option not working") } + if len(conf.MapValue) != 2 { + t.Fatalf("map value invalid: %#+v\n", conf.MapValue) + } + + 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) + } _ = conf // t.Logf("%#+v\n", conf) } diff --git a/go.mod b/go.mod index 55853a55..890d2b81 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module go.unistack.org/micro/v3 go 1.19 require ( - github.com/imdario/mergo v0.3.14 + github.com/google/uuid v1.3.0 + github.com/imdario/mergo v0.3.15 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35 ) diff --git a/go.sum b/go.sum index 40151841..22b410b6 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ -github.com/imdario/mergo v0.3.14 h1:fOqeC1+nCuuk6PKQdg9YmosXX7Y7mHX6R/0ZldI9iHo= -github.com/imdario/mergo v0.3.14/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM= +github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35 h1:4mohWoM/UGg1BvFFiqSPRl5uwJY3rVV0HQX0ETqauqQ= -- 2.45.2