Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmp/cmpopts: use errors.Is with ≥go1.13 in compareErrors #251

Merged
merged 1 commit into from Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 0 additions & 8 deletions cmp/cmpopts/equate.go
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
@@ -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
@@ -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
tklauser marked this conversation as resolved.
Show resolved Hide resolved

// 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)
}