From bac4c1873305de9a434c4e6b8f1b9704336dfd08 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 17 May 2022 16:59:19 +0800 Subject: [PATCH 1/3] refact(map): MergeHashMap and MergeStringMap with test --- go.mod | 6 ++-- go.sum | 10 +++--- logger/app.go | 12 +++---- logger/app_test.go | 57 ++++++++++++++++++++++++++++++++++ logger/drivers/zap/log_test.go | 31 ------------------ object/hashMap.go | 20 +++++++++++- object/hashMap_test.go | 38 +++++++++++++++++++++++ object/stringMap.go | 8 ++++- object/stringMap_test.go | 35 +++++++++++++++++++++ 9 files changed, 171 insertions(+), 46 deletions(-) create mode 100644 logger/app_test.go create mode 100644 object/hashMap_test.go create mode 100644 object/stringMap_test.go diff --git a/go.mod b/go.mod index 9ee11da..46cafd5 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/go-redis/redis/v8 v8.11.0 github.com/golang-module/carbon v1.5.5 - github.com/guonaihong/gout v0.2.9 + github.com/guonaihong/gout v0.2.12 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/stretchr/testify v1.7.0 go.uber.org/zap v1.21.0 @@ -24,14 +24,14 @@ require ( github.com/gobuffalo/packr v1.30.1 // indirect github.com/joho/godotenv v1.3.0 // indirect github.com/leodido/go-urn v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.3.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect - golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect google.golang.org/protobuf v1.26.0 // indirect gopkg.in/yaml.v2 v2.3.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect diff --git a/go.sum b/go.sum index 66de6f1..0f93880 100644 --- a/go.sum +++ b/go.sum @@ -59,8 +59,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/guonaihong/gout v0.2.9 h1:8nU5hrtwP1qDwiadFvU+D+z3ud9WEk8iPSfxQDiebng= -github.com/guonaihong/gout v0.2.9/go.mod h1:H1JqEuZmK4h/urWUq/LnIOEzS1kxl5rK3NkFqZ6Rn48= +github.com/guonaihong/gout v0.2.12 h1:ZNtg0Nq6yzlBNgDhFF2YaRdngZCIPuijgaA0s+rCvfY= +github.com/guonaihong/gout v0.2.12/go.mod h1:25bJRA+9fErgSvEUF5UAazc9dME+VdkjrFu7yrrVQUg= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -79,8 +79,9 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= @@ -179,8 +180,9 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/logger/app.go b/logger/app.go index 86526a4..48b746b 100644 --- a/logger/app.go +++ b/logger/app.go @@ -27,22 +27,22 @@ func NewLogger(driver string, config *object.HashMap) (logger *Logger, err error } func (log *Logger) Debug(msg string, v ...interface{}) { - log.Driver.Debug(msg, v) + log.Driver.Debug(msg, v...) } func (log *Logger) Info(msg string, v ...interface{}) { - log.Driver.Info(msg, v) + log.Driver.Info(msg, v...) } func (log *Logger) Warn(msg string, v ...interface{}) { - log.Driver.Warn(msg, v) + log.Driver.Warn(msg, v...) } func (log *Logger) Error(msg string, v ...interface{}) { - log.Driver.Error(msg, v) + log.Driver.Error(msg, v...) } func (log *Logger) Panic(msg string, v ...interface{}) { - log.Driver.Panic(msg, v) + log.Driver.Panic(msg, v...) } func (log *Logger) Fatal(msg string, v ...interface{}) { - log.Driver.Fatal(msg, v) + log.Driver.Fatal(msg, v...) } func (log *Logger) DebugF(format string, args ...interface{}) { diff --git a/logger/app_test.go b/logger/app_test.go new file mode 100644 index 0000000..a859afe --- /dev/null +++ b/logger/app_test.go @@ -0,0 +1,57 @@ +package logger + +import ( + "github.com/ArtisanCloud/PowerLibs/object" + "net/http" + "os" + "testing" +) + +var strArtisanCloudPath = "/var/log/ArtisanCloud/PowerLibs" +var strOutputPath = strArtisanCloudPath + "/output.log" +var strErrorPath = strArtisanCloudPath + "/errors.log" + +func init() { + err := initLogPath(strArtisanCloudPath, strOutputPath, strErrorPath) + if err != nil { + panic(err) + } +} + +func Test_Log_Info(t *testing.T) { + driver := "zap" + logger, err := NewLogger(driver, &object.HashMap{ + "env": "test", + "outputPath": strOutputPath, + "errorPath": strErrorPath, + }) + if err != nil { + t.Error(err) + } + + logger.Info("test info", "app response", &http.Response{}) + +} + +func initLogPath(path string, files ...string) (err error) { + if _, err = os.Stat(path); os.IsNotExist(err) { + err = os.MkdirAll(path, os.ModePerm) + if err != nil { + return err + } + } else if os.IsPermission(err) { + return err + } + + for _, fileName := range files { + if _, err = os.Stat(fileName); os.IsNotExist(err) { + _, err = os.Create(fileName) + if err != nil { + return err + } + } + } + + return err + +} diff --git a/logger/drivers/zap/log_test.go b/logger/drivers/zap/log_test.go index a96f888..c770fef 100644 --- a/logger/drivers/zap/log_test.go +++ b/logger/drivers/zap/log_test.go @@ -3,7 +3,6 @@ package zap import ( "github.com/ArtisanCloud/PowerLibs/object" "net/http" - "os" "testing" ) @@ -11,13 +10,6 @@ var strArtisanCloudPath = "/var/log/ArtisanCloud/PowerLibs" var strOutputPath = strArtisanCloudPath + "/output.log" var strErrorPath = strArtisanCloudPath + "/errors.log" -func init() { - err := initLogPath(strArtisanCloudPath, strOutputPath, strErrorPath) - if err != nil { - panic(err) - } -} - func Test_Log_Info(t *testing.T) { logger, err := NewLogger(&object.HashMap{ "env": "test", @@ -31,26 +23,3 @@ func Test_Log_Info(t *testing.T) { logger.Info("test info", "response", &http.Response{}) } - -func initLogPath(path string, files ...string) (err error) { - if _, err = os.Stat(path); os.IsNotExist(err) { - err = os.MkdirAll(path, os.ModePerm) - if err != nil { - return err - } - } else if os.IsPermission(err) { - return err - } - - for _, fileName := range files { - if _, err = os.Stat(fileName); os.IsNotExist(err) { - _, err = os.Create(fileName) - if err != nil { - return err - } - } - } - - return err - -} diff --git a/object/hashMap.go b/object/hashMap.go index 5f90166..ef4839e 100644 --- a/object/hashMap.go +++ b/object/hashMap.go @@ -13,10 +13,28 @@ func MergeHashMap(toMap *HashMap, subMaps ...*HashMap) *HashMap { if toMap == nil { toMap = &HashMap{} } + // 拍平subMaps for _, subMap := range subMaps { if subMap != nil { + // 迭代每个HashMap for k, v := range *subMap { - (*toMap)[k] = v + toV := (*toMap)[k] + + // if the key is not exist in toMap + if toV == nil { + (*toMap)[k] = v + continue + } + + // if the toMap by the key is "" + switch toV.(type) { + case string: + if (*toMap)[k] == "" && v != "" { + (*toMap)[k] = v + } + break + } + } } } diff --git a/object/hashMap_test.go b/object/hashMap_test.go new file mode 100644 index 0000000..c19e279 --- /dev/null +++ b/object/hashMap_test.go @@ -0,0 +1,38 @@ +package object + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_MergeHashMap(t *testing.T) { + + base := &HashMap{ + "key1": 123, + "key2": "456", + "key3": "", + "key4": nil, + "key5": &StringMap{}, + "key6": &HashMap{}, + } + + toMap := &HashMap{ + "key2": "", + "key3": "123", + "key4": &HashMap{}, + "key5": nil, + "key6": &StringMap{}, + } + + toMap = MergeHashMap(toMap, base) + + assert.EqualValues(t, &HashMap{ + "key1": 123, + "key2": "456", + "key3": "123", + "key4": &HashMap{}, + "key5": &StringMap{}, + "key6": &StringMap{}, + }, toMap) + +} diff --git a/object/stringMap.go b/object/stringMap.go index b614ee4..fea0730 100644 --- a/object/stringMap.go +++ b/object/stringMap.go @@ -17,8 +17,14 @@ func MergeStringMap(toStringMap *StringMap, subStringMaps ...*StringMap) *String } for _, subStringMap := range subStringMaps { if subStringMap != nil { + // 迭代每个HashMap for k, v := range *subStringMap { - (*toStringMap)[k] = v + toV := (*toStringMap)[k] + // if the key is not exist in toMap + if toV == "" && v != ""{ + (*toStringMap)[k] = v + } + } } } diff --git a/object/stringMap_test.go b/object/stringMap_test.go new file mode 100644 index 0000000..0a0f028 --- /dev/null +++ b/object/stringMap_test.go @@ -0,0 +1,35 @@ +package object + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_MergeStringMap(t *testing.T) { + + base := &StringMap{ + "key1": "123", + "key2": "xxx", + "key3": "", + "key4": "101112", + + } + + toMap := &StringMap{ + "key1": "", + "key2": "456", + "key3": "789", + + } + + toMap = MergeStringMap(toMap, base) + + assert.EqualValues(t, &StringMap{ + "key1": "123", + "key2": "456", + "key3": "789", + "key4": "101112", + }, toMap) + +} + From 66c1839ff2635aa1ecef0db7b7025b21192f6e38 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 17 May 2022 18:01:41 +0800 Subject: [PATCH 2/3] refact(map): ReplaceHashMap and replaceStringMap with test --- http/drivers/gout/client.go | 3 ++- object/hashMap.go | 20 ++++++++++++++++++- object/hashMap_test.go | 40 +++++++++++++++++++++++++++++++++++++ object/stringMap.go | 18 +++++++++++++++++ object/stringMap_test.go | 40 +++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 2 deletions(-) diff --git a/http/drivers/gout/client.go b/http/drivers/gout/client.go index cd049ef..7868497 100644 --- a/http/drivers/gout/client.go +++ b/http/drivers/gout/client.go @@ -287,7 +287,8 @@ func (client *Client) buildUri(uri *url.URL, config *object.HashMap) *url.URL { } } else { // use app config base uri - strBaseUri := (*client.Config)["http"].(object.HashMap)["base_uri"].(string) + mapHttp := (*client.Config)["http"].(*object.HashMap) + strBaseUri := (*mapHttp)["base_uri"].(string) baseUri, err = url.Parse(strBaseUri) if err != nil { print("cannot parse base url, pls make sure base_uri has scheme") diff --git a/object/hashMap.go b/object/hashMap.go index ef4839e..a223c7a 100644 --- a/object/hashMap.go +++ b/object/hashMap.go @@ -17,7 +17,7 @@ func MergeHashMap(toMap *HashMap, subMaps ...*HashMap) *HashMap { for _, subMap := range subMaps { if subMap != nil { // 迭代每个HashMap - for k, v := range *subMap { + for k, v := range (*subMap) { toV := (*toMap)[k] // if the key is not exist in toMap @@ -41,6 +41,24 @@ func MergeHashMap(toMap *HashMap, subMaps ...*HashMap) *HashMap { return toMap } + +// ------------------------------- Replace -------------------------------------------- +func ReplaceHashMapRecursive(toMap *HashMap, subMaps ...*HashMap) *HashMap { + if toMap == nil { + toMap = &HashMap{} + } + // 拍平subMaps + for _, subMap := range subMaps { + if subMap != nil { + // 迭代每个HashMap + for k, v := range *subMap { + (*toMap)[k] = v + } + } + } + return toMap +} + // ------------------------------- Conversion --------------------------------------- func HashMapToStringMap(obj *HashMap) (newMap *StringMap, err error) { diff --git a/object/hashMap_test.go b/object/hashMap_test.go index c19e279..ab86653 100644 --- a/object/hashMap_test.go +++ b/object/hashMap_test.go @@ -36,3 +36,43 @@ func Test_MergeHashMap(t *testing.T) { }, toMap) } + +func Test_ReplaceHashMapRecursive(t *testing.T) { + + base := &HashMap{ + "key1": 123, + "key2": "456", + "key3": "789", + "key4": nil, + "key5": map[string]int{}, + "key6": &map[string]float32{}, + } + + base2 := &HashMap{ + "key1": 456, + "key2": "base456", + "key3": "", + "key4": nil, + "key5": &StringMap{}, + } + + toMap := &HashMap{ + "key2": "", + "key3": "123", + "key4": &HashMap{}, + "key5": nil, + "key6": &StringMap{}, + } + + toMap = ReplaceHashMapRecursive(toMap, base, base2) + + assert.EqualValues(t, &HashMap{ + "key1": 456, + "key2": "base456", + "key3": "", + "key4": nil, + "key5": &StringMap{}, + "key6": &map[string]float32{}, + }, toMap) + +} diff --git a/object/stringMap.go b/object/stringMap.go index fea0730..a9d15f4 100644 --- a/object/stringMap.go +++ b/object/stringMap.go @@ -31,6 +31,24 @@ func MergeStringMap(toStringMap *StringMap, subStringMaps ...*StringMap) *String return toStringMap } + +// ------------------------------- Replace -------------------------------------------- +func ReplaceStringMapRecursive(toMap *StringMap, subMaps ...*StringMap) *StringMap { + if toMap == nil { + toMap = &StringMap{} + } + // 拍平subMaps + for _, subMap := range subMaps { + if subMap != nil { + // 迭代每个HashMap + for k, v := range *subMap { + (*toMap)[k] = v + } + } + } + return toMap +} + func ConvertStringMapToString(m *StringMap, separate string) string { var b bytes.Buffer for key, value := range *m { diff --git a/object/stringMap_test.go b/object/stringMap_test.go index 0a0f028..251c4d4 100644 --- a/object/stringMap_test.go +++ b/object/stringMap_test.go @@ -33,3 +33,43 @@ func Test_MergeStringMap(t *testing.T) { } + + +func Test_ReplaceStringMapRecursive(t *testing.T) { + + base := &StringMap{ + "key1": "123", + "key2": "456", + "key3": "789", + "key4": "nil", + } + + base2 := &StringMap{ + "key1": "456", + "key2": "base456", + "key3": "", + "key4": "nil", + "key5": "&StringMap{}", + } + + toMap := &StringMap{ + "key2": "", + "key3": "123", + "key4": "", + "key5": "nil", + "key6": "&StringMap{}", + } + + toMap = ReplaceStringMapRecursive(toMap, base, base2) + + assert.EqualValues(t, &StringMap{ + "key1": "456", + "key2": "base456", + "key3": "", + "key4": "nil", + "key5": "&StringMap{}", + "key6": "&StringMap{}", + }, toMap) + +} + From c9597679a4678b7cc6f61de898da73643a71165d Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 17 May 2022 18:13:44 +0800 Subject: [PATCH 3/3] refact(map): ReplaceHashMap and replaceStringMap with test --- http/drivers/gout/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/http/drivers/gout/client.go b/http/drivers/gout/client.go index 7868497..18cf11f 100644 --- a/http/drivers/gout/client.go +++ b/http/drivers/gout/client.go @@ -287,8 +287,8 @@ func (client *Client) buildUri(uri *url.URL, config *object.HashMap) *url.URL { } } else { // use app config base uri - mapHttp := (*client.Config)["http"].(*object.HashMap) - strBaseUri := (*mapHttp)["base_uri"].(string) + mapHttp := (*client.Config)["http"].(object.HashMap) + strBaseUri := mapHttp["base_uri"].(string) baseUri, err = url.Parse(strBaseUri) if err != nil { print("cannot parse base url, pls make sure base_uri has scheme")