From 6e24807edf4846487c4ec88d9f2e87e7ebca2fc4 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 13 Jul 2023 20:10:47 +0300 Subject: [PATCH] config/default: add micro:generate uuid/id Signed-off-by: Vasiliy Tolstov --- config/default.go | 16 ++++++++++++++++ config/default_test.go | 15 +++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 34 insertions(+) diff --git a/config/default.go b/config/default.go index c7e68469..5153dbb6 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/v4/util/id" rutil "go.unistack.org/micro/v4/util/reflect" mtime "go.unistack.org/micro/v4/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 4d2dc810..d790fb5f 100644 --- a/config/default_test.go +++ b/config/default_test.go @@ -7,6 +7,7 @@ import ( "time" "go.unistack.org/micro/v4/config" + mid "go.unistack.org/micro/v4/util/id" mtime "go.unistack.org/micro/v4/util/time" ) @@ -18,6 +19,8 @@ type cfg struct { 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 { @@ -71,6 +74,18 @@ func TestDefault(t *testing.T) { 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 22dde63b..2a89480a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 + 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-20220518035006-a7e85ada93c5 diff --git a/go.sum b/go.sum index 93cc2b79..1d7674b4 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +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= -- 2.45.2