aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2021-10-12 10:46:09 -0700
committerGitHub <noreply@github.com>2021-10-12 10:46:09 -0700
commitf1773ad7bdb1ddccb1801c5b8ca4311cf0d301e4 (patch)
tree9970edec5338ceb8d0565588c2ffbe8ad641672c
parent9094ef9afacbd93e9f232d0ccadaf8530f353441 (diff)
downloadgo-cmp-f1773ad7bdb1ddccb1801c5b8ca4311cf0d301e4.tar.gz
Use any alias instead of interface{} (#276)
See golang/go#33232.
-rw-r--r--cmp/internal/value/name.go7
-rw-r--r--cmp/internal/value/name_test.go2
-rw-r--r--cmp/testdata/diffs46
3 files changed, 31 insertions, 24 deletions
diff --git a/cmp/internal/value/name.go b/cmp/internal/value/name.go
index b6c12ce..7b498bb 100644
--- a/cmp/internal/value/name.go
+++ b/cmp/internal/value/name.go
@@ -9,6 +9,8 @@ import (
"strconv"
)
+var anyType = reflect.TypeOf((*interface{})(nil)).Elem()
+
// TypeString is nearly identical to reflect.Type.String,
// but has an additional option to specify that full type names be used.
func TypeString(t reflect.Type, qualified bool) string {
@@ -20,6 +22,11 @@ func appendTypeName(b []byte, t reflect.Type, qualified, elideFunc bool) []byte
// of the same name and within the same package,
// but declared within the namespace of different functions.
+ // Use the "any" alias instead of "interface{}" for better readability.
+ if t == anyType {
+ return append(b, "any"...)
+ }
+
// Named type.
if t.Name() != "" {
if qualified && t.PkgPath() != "" {
diff --git a/cmp/internal/value/name_test.go b/cmp/internal/value/name_test.go
index 3eec91c..c177e72 100644
--- a/cmp/internal/value/name_test.go
+++ b/cmp/internal/value/name_test.go
@@ -111,7 +111,7 @@ func TestTypeString(t *testing.T) {
want: "*$PackagePath.Named",
}, {
in: (*interface{})(nil),
- want: "*interface{}",
+ want: "*any",
}, {
in: (*interface{ Read([]byte) (int, error) })(nil),
want: "*interface{ Read([]uint8) (int, error) }",
diff --git a/cmp/testdata/diffs b/cmp/testdata/diffs
index afc7366..59afe6d 100644
--- a/cmp/testdata/diffs
+++ b/cmp/testdata/diffs
@@ -46,7 +46,7 @@
}
>>> TestDiff/Comparer/DifferingHash
<<< TestDiff/Comparer/NilStringer
- interface{}(
+ any(
- &fmt.Stringer(nil),
)
>>> TestDiff/Comparer/NilStringer
@@ -135,19 +135,19 @@
)
>>> TestDiff/Comparer/StringerMapKey
<<< TestDiff/Comparer/StringerBacktick
- interface{}(
+ any(
- []*testprotos.Stringer{s`multi\nline\nline\nline`},
)
>>> TestDiff/Comparer/StringerBacktick
<<< TestDiff/Comparer/DynamicMap
- []interface{}{
- map[string]interface{}{
+ []any{
+ map[string]any{
"avg": float64(0.278),
- "hr": int(65),
+ "hr": float64(65),
"name": string("Mark McGwire"),
},
- map[string]interface{}{
+ map[string]any{
"avg": float64(0.288),
- "hr": int(63),
+ "hr": float64(63),
@@ -199,14 +199,14 @@
}
>>> TestDiff/Transformer/Filtered
<<< TestDiff/Transformer/DisjointOutput
- int(Inverse(λ, interface{}(
+ int(Inverse(λ, any(
- string("zero"),
+ float64(1),
)))
>>> TestDiff/Transformer/DisjointOutput
<<< TestDiff/Transformer/JSON
- string(Inverse(ParseJSON, map[string]interface{}{
- "address": map[string]interface{}{
+ string(Inverse(ParseJSON, map[string]any{
+ "address": map[string]any{
- "city": string("Los Angeles"),
+ "city": string("New York"),
"postalCode": string("10021-3100"),
@@ -215,18 +215,18 @@
"streetAddress": string("21 2nd Street"),
},
"age": float64(25),
- "children": []interface{}{},
+ "children": []any{},
"firstName": string("John"),
"isAlive": bool(true),
"lastName": string("Smith"),
- "phoneNumbers": []interface{}{
- map[string]interface{}{
+ "phoneNumbers": []any{
+ map[string]any{
- "number": string("212 555-4321"),
+ "number": string("212 555-1234"),
"type": string("home"),
},
- map[string]interface{}{"number": string("646 555-4567"), "type": string("office")},
- map[string]interface{}{"number": string("123 456-7890"), "type": string("mobile")},
+ map[string]any{"number": string("646 555-4567"), "type": string("office")},
+ map[string]any{"number": string("123 456-7890"), "type": string("mobile")},
},
+ "spouse": nil,
}))
@@ -267,7 +267,7 @@
}
>>> TestDiff/Reporter/PanicError
<<< TestDiff/Reporter/AmbiguousType
- interface{}(
+ any(
- "github.com/google/go-cmp/cmp/internal/teststructs/foo1".Bar{},
+ "github.com/google/go-cmp/cmp/internal/teststructs/foo2".Bar{},
)
@@ -297,7 +297,7 @@
}
>>> TestDiff/Reporter/AmbiguousPointerMap
<<< TestDiff/Reporter/AmbiguousStringer
- interface{}(
+ any(
- cmp_test.Stringer("hello"),
+ &cmp_test.Stringer("hello"),
)
@@ -327,7 +327,7 @@
)
>>> TestDiff/Reporter/AmbiguousSliceHeader
<<< TestDiff/Reporter/AmbiguousStringerMapKey
- map[interface{}]string{
+ map[any]string{
- nil: "nil",
+ &⟪0xdeadf00f⟫"github.com/google/go-cmp/cmp_test".Stringer("hello"): "goodbye",
- "github.com/google/go-cmp/cmp_test".Stringer("hello"): "goodbye",
@@ -336,13 +336,13 @@
}
>>> TestDiff/Reporter/AmbiguousStringerMapKey
<<< TestDiff/Reporter/NonAmbiguousStringerMapKey
- map[interface{}]string{
+ map[any]string{
+ s"fizz": "buzz",
- s"hello": "goodbye",
}
>>> TestDiff/Reporter/NonAmbiguousStringerMapKey
<<< TestDiff/Reporter/InvalidUTF8
- interface{}(
+ any(
- cmp_test.MyString("\xed\xa0\x80"),
)
>>> TestDiff/Reporter/InvalidUTF8
@@ -399,7 +399,7 @@
}
>>> TestDiff/Reporter/BatchedWithComparer
<<< TestDiff/Reporter/BatchedLong
- interface{}(
+ any(
- cmp_test.MyComposite{IntsA: []int8{0, 1, 2, 3, 4, 5, 6, 7, ...}},
)
>>> TestDiff/Reporter/BatchedLong
@@ -1017,7 +1017,7 @@
}
>>> TestDiff/Reporter/LargeMapKey
<<< TestDiff/Reporter/LargeStringInInterface
- struct{ X interface{} }{
+ struct{ X any }{
X: strings.Join({
... // 485 identical bytes
"s mus. Pellentesque mi lorem, consectetur id porttitor id, solli",
@@ -1028,7 +1028,7 @@
}
>>> TestDiff/Reporter/LargeStringInInterface
<<< TestDiff/Reporter/LargeBytesInInterface
- struct{ X interface{} }{
+ struct{ X any }{
X: bytes.Join({
... // 485 identical bytes
"s mus. Pellentesque mi lorem, consectetur id porttitor id, solli",
@@ -1039,7 +1039,7 @@
}
>>> TestDiff/Reporter/LargeBytesInInterface
<<< TestDiff/Reporter/LargeStandaloneString
- struct{ X interface{} }{
+ struct{ X any }{
- X: [1]string{
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet pretium ligula, at gravida quam. Integer iaculis, velit at sagittis ultricies, lacus metus scelerisque turpis, ornare feugiat nulla nisl ac erat. Maecenas elementum ultricies libero, sed efficitur lacus molestie non. Nulla ac pretium dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque mi lorem, consectetur id porttitor id, sollicitudin sit amet enim. Duis eu dolor magna. Nunc ut augue turpis.",
- },
@@ -1614,7 +1614,7 @@
... // 4 identical fields
ContSlaps: nil,
ContSlapsInterval: 0,
- Animal: []interface{}{
+ Animal: []any{
teststructs.Goat{
Target: "corporation",
Slaps: nil,