Skip to content

Commit

Permalink
Merge pull request #3 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature

- refact(map): MergeHashMap and MergeStringMap with test
- refact(map): ReplaceHashMap and replaceStringMap with test
  • Loading branch information
Matrix-X authored May 17, 2022
2 parents 6ac56cf + c959767 commit 19943e5
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 46 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion http/drivers/gout/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 6 additions & 6 deletions logger/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
57 changes: 57 additions & 0 deletions logger/app_test.go
Original file line number Diff line number Diff line change
@@ -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

}
31 changes: 0 additions & 31 deletions logger/drivers/zap/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ package zap
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) {
logger, err := NewLogger(&object.HashMap{
"env": "test",
Expand All @@ -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

}
36 changes: 36 additions & 0 deletions object/hashMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,44 @@ 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) {
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
}

}
}
}
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
}
Expand Down
78 changes: 78 additions & 0 deletions object/hashMap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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)

}

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)

}
26 changes: 25 additions & 1 deletion object/stringMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,38 @@ 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
}

}
}
}
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 {
Expand Down
Loading

0 comments on commit 19943e5

Please sign in to comment.