Skip to content

Commit 3a98a11

Browse files
authored
cmp/cmpopts: use errors.Is with ≥go1.13 in compareErrors (#251)
Use the standard definition of errors.Is to implement compareErrors with ≥go1.13. Retain the implementation using golang.org/x/xerrors for versions <go1.13. This will allow packages using newer Go versions and already relying on the errors package to get rid of the transitive dependency on golang.org/x/xerrors.
1 parent ec71d6d commit 3a98a11

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

cmp/cmpopts/equate.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/google/go-cmp/cmp"
14-
"golang.org/x/xerrors"
1514
)
1615

1716
func equateAlways(_, _ interface{}) bool { return true }
@@ -147,10 +146,3 @@ func areConcreteErrors(x, y interface{}) bool {
147146
_, ok2 := y.(error)
148147
return ok1 && ok2
149148
}
150-
151-
func compareErrors(x, y interface{}) bool {
152-
xe := x.(error)
153-
ye := y.(error)
154-
// TODO(≥go1.13): Use standard definition of errors.Is.
155-
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)
156-
}

cmp/cmpopts/errors_go113.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2021, The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build go1.13
6+
7+
package cmpopts
8+
9+
import "errors"
10+
11+
func compareErrors(x, y interface{}) bool {
12+
xe := x.(error)
13+
ye := y.(error)
14+
return errors.Is(xe, ye) || errors.Is(ye, xe)
15+
}

cmp/cmpopts/errors_xerrors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2021, The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !go1.13
6+
7+
// TODO(≥go1.13): For support on <go1.13, we use the xerrors package.
8+
// Drop this file when we no longer support older Go versions.
9+
10+
package cmpopts
11+
12+
import "golang.org/x/xerrors"
13+
14+
func compareErrors(x, y interface{}) bool {
15+
xe := x.(error)
16+
ye := y.(error)
17+
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)
18+
}

0 commit comments

Comments
 (0)