aboutsummaryrefslogtreecommitdiff
path: root/cmp/cmpopts
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2018-11-01 11:14:52 -0700
committerGitHub <noreply@github.com>2018-11-01 11:14:52 -0700
commit745b8ec8378318d64f3f04949d010ee8d2fc71e2 (patch)
tree1718a3233f668ba34f41c11d2fee3aafb9e1fa3e /cmp/cmpopts
parent875f8df8b7965f1eac1098d36d677f807ac0b49e (diff)
downloadgo-cmp-745b8ec8378318d64f3f04949d010ee8d2fc71e2.tar.gz
Bump minimum version to Go1.8 (#50)
Go1.8 went GA on AppEngine. The lack of Go1.8 support was the primary reason for much of these backwards compatibility hacks. Bumping to Go1.8 still ensures that we're supporting at least the latest 2 versions of Go, which are Go1.8 and Go1.9.
Diffstat (limited to 'cmp/cmpopts')
-rw-r--r--cmp/cmpopts/sort.go17
-rw-r--r--cmp/cmpopts/sort_go17.go46
-rw-r--r--cmp/cmpopts/sort_go18.go31
-rw-r--r--cmp/cmpopts/util_test.go18
4 files changed, 11 insertions, 101 deletions
diff --git a/cmp/cmpopts/sort.go b/cmp/cmpopts/sort.go
index da17d74..ddd557b 100644
--- a/cmp/cmpopts/sort.go
+++ b/cmp/cmpopts/sort.go
@@ -7,6 +7,7 @@ package cmpopts
import (
"fmt"
"reflect"
+ "sort"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/internal/function"
@@ -48,8 +49,8 @@ func (ss sliceSorter) filter(x, y interface{}) bool {
}
// Check whether the slices are already sorted to avoid an infinite
// recursion cycle applying the same transform to itself.
- ok1 := sliceIsSorted(x, func(i, j int) bool { return ss.less(vx, i, j) })
- ok2 := sliceIsSorted(y, func(i, j int) bool { return ss.less(vy, i, j) })
+ ok1 := sort.SliceIsSorted(x, func(i, j int) bool { return ss.less(vx, i, j) })
+ ok2 := sort.SliceIsSorted(y, func(i, j int) bool { return ss.less(vy, i, j) })
return !ok1 || !ok2
}
func (ss sliceSorter) sort(x interface{}) interface{} {
@@ -58,7 +59,7 @@ func (ss sliceSorter) sort(x interface{}) interface{} {
for i := 0; i < src.Len(); i++ {
dst.Index(i).Set(src.Index(i))
}
- sortSliceStable(dst.Interface(), func(i, j int) bool { return ss.less(dst, i, j) })
+ sort.SliceStable(dst.Interface(), func(i, j int) bool { return ss.less(dst, i, j) })
ss.checkSort(dst)
return dst.Interface()
}
@@ -118,7 +119,10 @@ func (ms mapSorter) filter(x, y interface{}) bool {
}
func (ms mapSorter) sort(x interface{}) interface{} {
src := reflect.ValueOf(x)
- outType := mapEntryType(src.Type())
+ outType := reflect.StructOf([]reflect.StructField{
+ {Name: "K", Type: src.Type().Key()},
+ {Name: "V", Type: src.Type().Elem()},
+ })
dst := reflect.MakeSlice(reflect.SliceOf(outType), src.Len(), src.Len())
for i, k := range src.MapKeys() {
v := reflect.New(outType).Elem()
@@ -126,7 +130,7 @@ func (ms mapSorter) sort(x interface{}) interface{} {
v.Field(1).Set(src.MapIndex(k))
dst.Index(i).Set(v)
}
- sortSlice(dst.Interface(), func(i, j int) bool { return ms.less(dst, i, j) })
+ sort.Slice(dst.Interface(), func(i, j int) bool { return ms.less(dst, i, j) })
ms.checkSort(dst)
return dst.Interface()
}
@@ -139,8 +143,5 @@ func (ms mapSorter) checkSort(v reflect.Value) {
}
func (ms mapSorter) less(v reflect.Value, i, j int) bool {
vx, vy := v.Index(i).Field(0), v.Index(j).Field(0)
- if !hasReflectStructOf {
- vx, vy = vx.Elem(), vy.Elem()
- }
return ms.fnc.Call([]reflect.Value{vx, vy})[0].Bool()
}
diff --git a/cmp/cmpopts/sort_go17.go b/cmp/cmpopts/sort_go17.go
deleted file mode 100644
index 839b88c..0000000
--- a/cmp/cmpopts/sort_go17.go
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright 2017, The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE.md file.
-
-// +build !go1.8
-
-package cmpopts
-
-import (
- "reflect"
- "sort"
-)
-
-const hasReflectStructOf = false
-
-func mapEntryType(reflect.Type) reflect.Type {
- return reflect.TypeOf(struct{ K, V interface{} }{})
-}
-
-func sliceIsSorted(slice interface{}, less func(i, j int) bool) bool {
- return sort.IsSorted(reflectSliceSorter{reflect.ValueOf(slice), less})
-}
-func sortSlice(slice interface{}, less func(i, j int) bool) {
- sort.Sort(reflectSliceSorter{reflect.ValueOf(slice), less})
-}
-func sortSliceStable(slice interface{}, less func(i, j int) bool) {
- sort.Stable(reflectSliceSorter{reflect.ValueOf(slice), less})
-}
-
-type reflectSliceSorter struct {
- slice reflect.Value
- less func(i, j int) bool
-}
-
-func (ss reflectSliceSorter) Len() int {
- return ss.slice.Len()
-}
-func (ss reflectSliceSorter) Less(i, j int) bool {
- return ss.less(i, j)
-}
-func (ss reflectSliceSorter) Swap(i, j int) {
- vi := ss.slice.Index(i).Interface()
- vj := ss.slice.Index(j).Interface()
- ss.slice.Index(i).Set(reflect.ValueOf(vj))
- ss.slice.Index(j).Set(reflect.ValueOf(vi))
-}
diff --git a/cmp/cmpopts/sort_go18.go b/cmp/cmpopts/sort_go18.go
deleted file mode 100644
index 8a59c0d..0000000
--- a/cmp/cmpopts/sort_go18.go
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright 2017, The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE.md file.
-
-// +build go1.8
-
-package cmpopts
-
-import (
- "reflect"
- "sort"
-)
-
-const hasReflectStructOf = true
-
-func mapEntryType(t reflect.Type) reflect.Type {
- return reflect.StructOf([]reflect.StructField{
- {Name: "K", Type: t.Key()},
- {Name: "V", Type: t.Elem()},
- })
-}
-
-func sliceIsSorted(slice interface{}, less func(i, j int) bool) bool {
- return sort.SliceIsSorted(slice, less)
-}
-func sortSlice(slice interface{}, less func(i, j int) bool) {
- sort.Slice(slice, less)
-}
-func sortSliceStable(slice interface{}, less func(i, j int) bool) {
- sort.SliceStable(slice, less)
-}
diff --git a/cmp/cmpopts/util_test.go b/cmp/cmpopts/util_test.go
index 790ba8d..53b9248 100644
--- a/cmp/cmpopts/util_test.go
+++ b/cmp/cmpopts/util_test.go
@@ -746,7 +746,7 @@ func TestOptions(t *testing.T) {
}}
for _, tt := range tests {
- tRun(t, tt.label, func(t *testing.T) {
+ t.Run(tt.label, func(t *testing.T) {
var gotEqual bool
var gotPanic string
func() {
@@ -974,7 +974,7 @@ func TestPanic(t *testing.T) {
}}
for _, tt := range tests {
- tRun(t, tt.label, func(t *testing.T) {
+ t.Run(tt.label, func(t *testing.T) {
// Prepare function arguments.
vf := reflect.ValueOf(tt.fnc)
var vargs []reflect.Value
@@ -1017,17 +1017,3 @@ func TestPanic(t *testing.T) {
})
}
}
-
-// TODO: Delete this hack when we drop Go1.6 support.
-func tRun(t *testing.T, name string, f func(t *testing.T)) {
- type runner interface {
- Run(string, func(t *testing.T)) bool
- }
- var ti interface{} = t
- if r, ok := ti.(runner); ok {
- r.Run(name, f)
- } else {
- t.Logf("Test: %s", name)
- f(t)
- }
-}