Skip to content

Commit

Permalink
cmp/cmpopts: use errors.Is with ≥go1.13 in compareErrors
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 people using newer Go versions to get rid of the
transitive dependency on golang.org/x/xerrors.
  • Loading branch information
tklauser committed Feb 4, 2021
1 parent ec71d6d commit 6cfaebc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
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)
}
16 changes: 16 additions & 0 deletions cmp/cmpopts/errors_go113.go
@@ -0,0 +1,16 @@
// 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)
// Use standard definition of errors.Is with ≥go1.13.
return errors.Is(xe, ye) || errors.Is(ye, xe)
}
16 changes: 16 additions & 0 deletions cmp/cmpopts/errors_xerrors.go
@@ -0,0 +1,16 @@
// 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 "golang.org/x/xerrors"

func compareErrors(x, y interface{}) bool {
xe := x.(error)
ye := y.(error)
// Use xerrors with <go1.13.
return xerrors.Is(xe, ye) || xerrors.Is(ye, xe)
}

0 comments on commit 6cfaebc

Please sign in to comment.