Skip to content

Commit

Permalink
cmp/cmpopts: use errors.Is with ≥go1.13 in compareErrors (#251)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tklauser authored Feb 5, 2021
1 parent ec71d6d commit 3a98a11
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
8 changes: 0 additions & 8 deletions cmp/cmpopts/equate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"golang.org/x/xerrors"
)

func equateAlways(_, _ interface{}) bool { return true }
Expand Down Expand Up @@ -147,10 +146,3 @@ func areConcreteErrors(x, y interface{}) bool {
_, ok2 := y.(error)
return ok1 && ok2
}

func compareErrors(x, y interface{}) bool {
xe := x.(error)
ye := y.(error)
// TODO(≥go1.13): Use standard definition of errors.Is.
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)
}
15 changes: 15 additions & 0 deletions cmp/cmpopts/errors_go113.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021, 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 file.

// +build go1.13

package cmpopts

import "errors"

func compareErrors(x, y interface{}) bool {
xe := x.(error)
ye := y.(error)
return errors.Is(xe, ye) || errors.Is(ye, xe)
}
18 changes: 18 additions & 0 deletions cmp/cmpopts/errors_xerrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2021, 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 file.

// +build !go1.13

// TODO(≥go1.13): For support on <go1.13, we use the xerrors package.
// Drop this file when we no longer support older Go versions.

package cmpopts

import "golang.org/x/xerrors"

func compareErrors(x, y interface{}) bool {
xe := x.(error)
ye := y.(error)
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)
}

0 comments on commit 3a98a11

Please sign in to comment.