From 92fc97c9f296781019650e30a73fe08106e3669b Mon Sep 17 00:00:00 2001 From: ahl5esoft Date: Fri, 20 Aug 2021 14:48:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E7=BB=93=E6=9E=9C=E6=97=B6=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E6=96=B9=E6=B3=95=E6=8A=A5=E9=94=99=E7=9A=84?= =?UTF-8?q?BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- any_test.go | 20 +++++++++++++++++ chain.go | 26 ++++++++++++---------- group_test.go | 2 +- map_test.go | 4 ---- order_test.go | 60 +++++++++++++++++++++++++++++++++------------------ 6 files changed, 76 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 76cbed7..c4d68f3 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ \_/__/ ``` -# Underscore.go [![GoDoc](https://godoc.org/github.com/ahl5esoft/golang-underscore?status.svg)](https://godoc.org/github.com/ahl5esoft/golang-underscore) [![Go Report Card](https://goreportcard.com/badge/github.com/ahl5esoft/golang-underscore)](https://goreportcard.com/report/github.com/ahl5esoft/golang-underscore) ![Version](https://img.shields.io/badge/version-2.4.0-green.svg) +# Underscore.go [![GoDoc](https://godoc.org/github.com/ahl5esoft/golang-underscore?status.svg)](https://godoc.org/github.com/ahl5esoft/golang-underscore) [![Go Report Card](https://goreportcard.com/badge/github.com/ahl5esoft/golang-underscore)](https://goreportcard.com/report/github.com/ahl5esoft/golang-underscore) ![Version](https://img.shields.io/badge/version-2.4.1-green.svg) like underscore.js and C# LINQ, but for Go ## Installation diff --git a/any_test.go b/any_test.go index b0385a0..375c39e 100644 --- a/any_test.go +++ b/any_test.go @@ -14,6 +14,26 @@ func Benchmark_Any(b *testing.B) { } } +func Test_Any(t *testing.T) { + t.Run("source is empty slice", func(t *testing.T) { + arr := make([]testModel, 0) + res := Chain(arr).Any(func(r testModel, _ int) bool { + return r.ID == 0 + }) + assert.False(t, res) + }) + + t.Run("source is nil", func(t *testing.T) { + var arr []testModel + assert.Nil(t, arr) + + res := Chain(arr).Any(func(r testModel, _ int) bool { + return r.ID == 0 + }) + assert.False(t, res) + }) +} + func Test_Any_False(t *testing.T) { ok := Chain([]testModel{ {ID: 1, Name: "one"}, diff --git a/chain.go b/chain.go index 41920f8..cfea446 100644 --- a/chain.go +++ b/chain.go @@ -1,6 +1,8 @@ package underscore -import "reflect" +import ( + "reflect" +) // Chain is 创建枚举器 func Chain(src interface{}) IEnumerable { @@ -63,18 +65,20 @@ func chainFromValue(value reflect.Value) IEnumerable { value.Len(), ) default: - if iterator, ok := value.Interface().(IEnumerator); ok { - return enumerable{ - Enumerator: func() IEnumerator { - return iterator - }, + if value.IsValid() { + if iterator, ok := value.Interface().(IEnumerator); ok { + return enumerable{ + Enumerator: func() IEnumerator { + return iterator + }, + } } - } - if value.Kind() == reflect.Ptr { - return chainFromValue( - value.Elem(), - ) + if value.Kind() == reflect.Ptr { + return chainFromValue( + value.Elem(), + ) + } } return enumerable{ diff --git a/group_test.go b/group_test.go index 894b852..7cc2355 100644 --- a/group_test.go +++ b/group_test.go @@ -53,7 +53,7 @@ func Test_Group(t *testing.T) { }).Value(resValue) assert.EqualValues( t, - resValue.Interface(), + resValue.Elem().Interface(), map[string][]int{ "odd": {1, 3, 5}, "even": {2, 4}, diff --git a/map_test.go b/map_test.go index 6085f91..9d9b2a0 100644 --- a/map_test.go +++ b/map_test.go @@ -1,7 +1,6 @@ package underscore import ( - "fmt" "reflect" "strconv" "testing" @@ -51,9 +50,6 @@ func Test_Map(t *testing.T) { resValue.Elem().Interface(), []int{0, 1}, ) - fmt.Println(resValue) - - assert.True(t, false) }) } diff --git a/order_test.go b/order_test.go index 4d3e43d..86a3931 100644 --- a/order_test.go +++ b/order_test.go @@ -7,27 +7,45 @@ import ( ) func Test_Order(t *testing.T) { - arr := []testModel{ - {ID: 2, Name: "two"}, - {ID: 1, Name: "one"}, - {ID: 3, Name: "three"}, - } - var res []int - Chain(arr).Order(func(n testModel, _ int) int { - return n.ID - }).Map(func(r testModel, _ int) int { - return r.ID - }).Value(&res) - assert.Len( - t, - res, - len(arr), - ) - assert.EqualValues( - t, - res, - []int{1, 2, 3}, - ) + t.Run("ok", func(t *testing.T) { + arr := []testModel{ + {ID: 2, Name: "two"}, + {ID: 1, Name: "one"}, + {ID: 3, Name: "three"}, + } + var res []int + Chain(arr).Order(func(n testModel, _ int) int { + return n.ID + }).Map(func(r testModel, _ int) int { + return r.ID + }).Value(&res) + assert.Len( + t, + res, + len(arr), + ) + assert.EqualValues( + t, + res, + []int{1, 2, 3}, + ) + }) + + t.Run("chain", func(t *testing.T) { + arr := []testModel{ + {ID: 2, Name: "two"}, + {ID: 1, Name: "one"}, + {ID: 3, Name: "three"}, + } + var res []int + Chain(arr).Where(func(r testModel, _ int) bool { + return r.ID > 5 + }).Order(func(r testModel, _ int) int { + return r.ID + }).Map(func(r testModel, _ int) int { + return r.ID + }).Value(&res) + }) } func Test_OrderBy(t *testing.T) {