Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code optimization #2667

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions container/garray/garray_z_unit_normal_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ func TestIntArray_Json(t *testing.T) {

a2 := garray.NewIntArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s1)

var a3 garray.IntArray
Expand All @@ -660,6 +661,7 @@ func TestIntArray_Json(t *testing.T) {

a2 := garray.NewIntArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s1)

var a3 garray.IntArray
Expand Down
2 changes: 2 additions & 0 deletions container/garray/garray_z_unit_normal_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ func TestStrArray_Json(t *testing.T) {

a2 := garray.NewStrArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s1)

var a3 garray.StrArray
Expand All @@ -672,6 +673,7 @@ func TestStrArray_Json(t *testing.T) {

a2 := garray.NewStrArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s1)

var a3 garray.StrArray
Expand Down
2 changes: 2 additions & 0 deletions container/garray/garray_z_unit_sorted_any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ func TestSortedArray_Json(t *testing.T) {

a2 := garray.NewSortedArray(gutil.ComparatorString)
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s2)

var a3 garray.SortedArray
Expand All @@ -730,6 +731,7 @@ func TestSortedArray_Json(t *testing.T) {

a2 := garray.NewSortedArray(gutil.ComparatorString)
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s2)

var a3 garray.SortedArray
Expand Down
2 changes: 2 additions & 0 deletions container/garray/garray_z_unit_sorted_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ func TestSortedIntArray_Json(t *testing.T) {

a2 := garray.NewSortedIntArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s2)

var a3 garray.SortedIntArray
Expand All @@ -632,6 +633,7 @@ func TestSortedIntArray_Json(t *testing.T) {

a2 := garray.NewSortedIntArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s2)

var a3 garray.SortedIntArray
Expand Down
2 changes: 2 additions & 0 deletions container/garray/garray_z_unit_sorted_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ func TestSortedStrArray_Json(t *testing.T) {

a2 := garray.NewSortedStrArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s2)
t.Assert(a2.Interfaces(), s2)

Expand All @@ -643,6 +644,7 @@ func TestSortedStrArray_Json(t *testing.T) {

a2 := garray.NewSortedStrArray()
err1 = json.UnmarshalUseNumber(b2, &a2)
t.AssertNil(err1)
t.Assert(a2.Slice(), s2)
t.Assert(a2.Interfaces(), s2)

Expand Down
15 changes: 3 additions & 12 deletions container/glist/glist_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,7 @@ func TestList_IteratorAsc(t *testing.T) {
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.Int(e1.Value) > 2 {
return true
}
return false
return gconv.Int(e1.Value) > 2
}
checkList(t, l, []interface{}{4, 3, 6, 5, 2, 1})
l.IteratorAsc(fun1)
Expand All @@ -649,10 +646,7 @@ func TestList_IteratorDesc(t *testing.T) {
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.Int(e1.Value) > 6 {
return true
}
return false
return gconv.Int(e1.Value) > 6
}
l.IteratorDesc(fun1)
t.Assert(l.Len(), 4)
Expand All @@ -667,10 +661,7 @@ func TestList_Iterator(t *testing.T) {
l.PushFronts(a1)
e1 := l.Back()
fun1 := func(e *Element) bool {
if gconv.String(e1.Value) > "c" {
return true
}
return false
return gconv.String(e1.Value) > "c"
}
checkList(t, l, []interface{}{"e", "d", "c", "b", "a"})
l.Iterator(fun1)
Expand Down
26 changes: 13 additions & 13 deletions encoding/gjson/gjson_z_unit_feature_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Set1(t *testing.T) {
p.Set("k1.k11", []int{1, 2, 3})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, []byte(`{"k1":{"k11":[1,2,3]},"k2":"v2"}`)) != 0 {
if !bytes.Equal(c, []byte(`{"k1":{"k11":[1,2,3]},"k2":"v2"}`)) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -51,7 +51,7 @@ func Test_Set3(t *testing.T) {
"k1": "v1",
})
if c, err := p.ToJson(); err == nil {
if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -67,7 +67,7 @@ func Test_Set4(t *testing.T) {
})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -81,7 +81,7 @@ func Test_Set5(t *testing.T) {
p.Set("0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", []int{1, 2, 3})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -95,7 +95,7 @@ func Test_Set6(t *testing.T) {
p.Set("1", []int{1, 2, 3})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -112,7 +112,7 @@ func Test_Set7(t *testing.T) {
p.Set("0.1", []int{1, 2, 3})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -129,7 +129,7 @@ func Test_Set8(t *testing.T) {
p.Set("0.0.0.0.0.0.1", []int{1, 2, 3})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -146,7 +146,7 @@ func Test_Set9(t *testing.T) {
p.Set("k1.1", []int{1, 2, 3})
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -160,7 +160,7 @@ func Test_Set10(t *testing.T) {
p.Set("a.b.c", 1)
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -174,7 +174,7 @@ func Test_Set11(t *testing.T) {
p.Remove("a.b.c")
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -189,7 +189,7 @@ func Test_Set12(t *testing.T) {
p.Set("1", 1)
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -204,7 +204,7 @@ func Test_Set13(t *testing.T) {
p.Set("array.1", 1)
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand All @@ -219,7 +219,7 @@ func Test_Set14(t *testing.T) {
p.Set("f.a", 1)
if c, err := p.ToJson(); err == nil {

if bytes.Compare(c, e) != 0 {
if !bytes.Equal(c, e) {
t.Error("expect:", string(e))
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion encoding/gxml/gxml_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Test_XmlToJson(t *testing.T) {
t.Errorf("dstXml to json error. %s", dstXml)
}

if bytes.Compare(srcJson, dstJson) != 0 {
if !bytes.Equal(srcJson, dstJson) {
t.Errorf("convert to json error. srcJson:%s, dstJson:%s", string(srcJson), string(dstJson))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/empty/empty_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestIsEmpty(t *testing.T) {
tmpT1 := "0"
tmpT2 := func() {}
tmpT2 = nil
tmpT3 := make(chan int, 0)
tmpT3 := make(chan int)
var (
tmpT4 TestPerson = nil
tmpT5 *TestPerson = nil
Expand Down
5 changes: 1 addition & 4 deletions internal/utils/utils_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ func IsNumeric(s string) bool {
return false
}
}
if dotCount > 1 {
return false
}
return true
return dotCount <= 1
}

// UcFirst returns a copy of the string s with the first letter mapped to its upper case.
Expand Down
3 changes: 1 addition & 2 deletions net/gtcp/gtcp_z_unit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package gtcp_test

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -42,7 +41,7 @@ func Test_Pool_Basic1(t *testing.T) {
})

gtest.C(t, func(t *gtest.T) {
_, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:80"))
_, err := gtcp.NewPoolConn("127.0.0.1:80")
t.AssertNE(err, nil)
})
}
Expand Down
2 changes: 1 addition & 1 deletion os/glog/glog_logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (l *Logger) SetConfig(config Config) error {

// SetConfigWithMap set configurations with map for the logger.
func (l *Logger) SetConfigWithMap(m map[string]interface{}) error {
if m == nil || len(m) == 0 {
if len(m) == 0 {
return gerror.NewCode(gcode.CodeInvalidParameter, "configuration cannot be empty")
}
// The m now is a shallow copy of m.
Expand Down
4 changes: 2 additions & 2 deletions os/gmlock/gmlock_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func Test_Locker_TryLockFunc(t *testing.T) {

func Test_Multiple_Goroutine(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ch := make(chan struct{}, 0)
ch := make(chan struct{})
num := 1000
wait := sync.WaitGroup{}
wait.Add(num)
Expand All @@ -178,7 +178,7 @@ func Test_Multiple_Goroutine(t *testing.T) {
})

gtest.C(t, func(t *gtest.T) {
ch := make(chan struct{}, 0)
ch := make(chan struct{})
num := 100
wait := sync.WaitGroup{}
wait.Add(num * 2)
Expand Down
2 changes: 1 addition & 1 deletion os/gview/gview_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (view *View) SetConfig(config Config) error {

// SetConfigWithMap set configurations with map for the view.
func (view *View) SetConfigWithMap(m map[string]interface{}) error {
if m == nil || len(m) == 0 {
if len(m) == 0 {
return gerror.NewCode(gcode.CodeInvalidParameter, "configuration cannot be empty")
}
// The m now is a shallow copy of m.
Expand Down
4 changes: 2 additions & 2 deletions text/gregex/gregex_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func Test_ReplaceFun(t *testing.T) {
wanted := "acbb[x" + wantSubs + "y]dd"
wanted = "acbb" + "3个a" + "dd"
replacedStr, err := gregex.ReplaceFunc(re, []byte(s), func(s []byte) []byte {
if strings.Index(string(s), "aaa") >= 0 {
if strings.Contains(string(s), "aaa") {
return []byte("3个a")
}
return []byte("[x" + string(s) + "y]")
Expand Down Expand Up @@ -258,7 +258,7 @@ func Test_ReplaceStringFunc(t *testing.T) {
wanted := "acbb[x" + wantSubs + "y]dd"
wanted = "acbb" + "3个a" + "dd"
replacedStr, err := gregex.ReplaceStringFunc(re, s, func(s string) string {
if strings.Index(s, "aaa") >= 0 {
if strings.Contains(s, "aaa") {
return "3个a"
}
return "[x" + s + "y]"
Expand Down
2 changes: 1 addition & 1 deletion util/gconv/gconv_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ type doMapConvertForMapOrStructValueInput struct {
}

func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) interface{} {
if in.IsRoot == false && in.RecursiveOption == false {
if !in.IsRoot && !in.RecursiveOption {
return in.Value
}

Expand Down
6 changes: 2 additions & 4 deletions util/gconv/gconv_z_unit_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,9 @@ func Test_String_All(t *testing.T) {
t.AssertEQ(gconv.String(boolStruct{}), "{}")
t.AssertEQ(gconv.String(&boolStruct{}), "{}")

var info iString
info = new(S)
var info = new(S)
t.AssertEQ(gconv.String(info), "22222")
var errInfo iError
errInfo = new(S1)
var errInfo = new(S1)
t.AssertEQ(gconv.String(errInfo), "22222")
})
}
Expand Down