From f147f78a41117a3c5b95de2690efad85173d3c77 Mon Sep 17 00:00:00 2001 From: donutloop Date: Tue, 11 Jan 2022 13:13:25 +0100 Subject: [PATCH 1/3] Slice: sort from v2 branch (#22) ref: https://github.com/duke-git/lancet/commit/f1d7154179a4d94472bee9703097d78e6c37e9a7 --- slice/slice.go | 37 +++++++++++++++++++++++++++---------- slice/slice_test.go | 28 +++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/slice/slice.go b/slice/slice.go index 8df6748..da07554 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -675,18 +675,38 @@ func SortByField(slice interface{}, field string, sortType ...string) error { } // Create a less function based on the field's kind. - var less func(a, b reflect.Value) bool + var compare func(a, b reflect.Value) bool switch sf.Type.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - less = func(a, b reflect.Value) bool { return a.Int() < b.Int() } + if len(sortType) > 0 && sortType[0] == "desc" { + compare = func(a, b reflect.Value) bool { return a.Int() > b.Int() } + } else { + compare = func(a, b reflect.Value) bool { return a.Int() < b.Int() } + } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - less = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() } + if len(sortType) > 0 && sortType[0] == "desc" { + compare = func(a, b reflect.Value) bool { return a.Uint() > b.Uint() } + } else { + compare = func(a, b reflect.Value) bool { return a.Uint() < b.Uint() } + } case reflect.Float32, reflect.Float64: - less = func(a, b reflect.Value) bool { return a.Float() < b.Float() } + if len(sortType) > 0 && sortType[0] == "desc" { + compare = func(a, b reflect.Value) bool { return a.Float() > b.Float() } + } else { + compare = func(a, b reflect.Value) bool { return a.Float() < b.Float() } + } case reflect.String: - less = func(a, b reflect.Value) bool { return a.String() < b.String() } + if len(sortType) > 0 && sortType[0] == "desc" { + compare = func(a, b reflect.Value) bool { return a.String() > b.String() } + } else { + compare = func(a, b reflect.Value) bool { return a.String() < b.String() } + } case reflect.Bool: - less = func(a, b reflect.Value) bool { return !a.Bool() && b.Bool() } + if len(sortType) > 0 && sortType[0] == "desc" { + compare = func(a, b reflect.Value) bool { return a.Bool() && !b.Bool() } + } else { + compare = func(a, b reflect.Value) bool { return !a.Bool() && b.Bool() } + } default: return fmt.Errorf("field type %s not supported", sf.Type) } @@ -700,12 +720,9 @@ func SortByField(slice interface{}, field string, sortType ...string) error { } a = a.FieldByIndex(sf.Index) b = b.FieldByIndex(sf.Index) - return less(a, b) + return compare(a, b) }) - if sortType[0] == "desc" { - ReverseSlice(slice) - } return nil } diff --git a/slice/slice_test.go b/slice/slice_test.go index d3f402e..9a7d6d2 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -443,7 +443,7 @@ func TestDifference(t *testing.T) { assert.Equal([]int{1, 2, 3}, Difference(s1, s2)) } -func TestSortByField(t *testing.T) { +func TestSortByFielDesc(t *testing.T) { assert := internal.NewAssert(t, "TestWithout") type student struct { @@ -469,6 +469,32 @@ func TestSortByField(t *testing.T) { assert.Equal(students, studentsOfSortByAge) } +func TestSortByFieldAsc(t *testing.T) { + assert := internal.NewAssert(t, "TestSortByField") + + type student struct { + name string + age int + } + students := []student{ + {"a", 10}, + {"b", 15}, + {"c", 5}, + {"d", 6}, + } + studentsOfSortByAge := []student{ + {"c", 5}, + {"d", 6}, + {"a", 10}, + {"b", 15}, + } + + err := SortByField(students, "age") + assert.IsNil(err) + + assert.Equal(students, studentsOfSortByAge) +} + func TestWithout(t *testing.T) { assert := internal.NewAssert(t, "TestWithout") assert.Equal([]int{3, 4, 5}, Without([]int{1, 2, 3, 4, 5}, 1, 2)) From 69453eba19f14a75188317a93941356af8828fbf Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 11 Jan 2022 20:42:04 +0800 Subject: [PATCH 2/3] release v1.1.9 --- README.md | 2 +- README_zh-CN.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6423bf7..0af279b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.1.8-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) diff --git a/README_zh-CN.md b/README_zh-CN.md index 25990f5..ec8312d 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -6,7 +6,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.1.8-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.9-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) @@ -396,7 +396,7 @@ func main() { func Contain(slice interface{}, value interface{}) bool //判断slice是否包含value func Chunk(slice []interface{}, size int) [][]interface{} //均分slice func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //将originalSlice转换为 newSliceType -func Difference(slice1, slice2 interface{}) interface{} //返回 +func Difference(slice1, slice2 interface{}) interface{} //返回切片,其元素在slice1中,不在slice2中 func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //删除切片中start到end位置的值 func Drop(slice interface{}, n int) interface{} //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 func Every(slice, function interface{}) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool From ba73847b80606450b98f95939cdbd6d343a842df Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 12 Jan 2022 09:57:10 +0800 Subject: [PATCH 3/3] fix: fix some go report issue --- function/watcher_test.go | 2 +- internal/assert.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/function/watcher_test.go b/function/watcher_test.go index f59772c..2660a29 100644 --- a/function/watcher_test.go +++ b/function/watcher_test.go @@ -24,7 +24,7 @@ func TestWatcher(t *testing.T) { assert.Equal(false, w.excuting) w.Reset() - + assert.Equal(int64(0), w.startTime) assert.Equal(int64(0), w.stopTime) } diff --git a/internal/assert.go b/internal/assert.go index 80d42f5..3e817d8 100644 --- a/internal/assert.go +++ b/internal/assert.go @@ -18,7 +18,7 @@ const ( compareGreater ) -// Assert is a simple implementation of assertion, only for internal useage +// Assert is a simple implementation of assertion, only for internal usage type Assert struct { T *testing.T CaseName string @@ -154,9 +154,8 @@ func compare(x, y interface{}) int { default: if reflect.DeepEqual(x, y) { return compareEqual - } else { - return compareNotEqual } + return compareNotEqual } return compareNotEqual