Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mkmik committed Dec 14, 2023
1 parent 87ba393 commit b86e467
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 10 deletions.
File renamed without changes.
22 changes: 22 additions & 0 deletions after_go120.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build go1.20
// +build go1.20

package multierror

// Split returns the underlying list of errors wrapped in a multierror.
//
// A multierror is an error tha implements Unwrap() []error (see https://pkg.go.dev/errors)
// Errors produced by multierror.Join implement Unwrap() []error.
func Split(err error) []error {
u, ok := err.(interface {
Unwrap() []error
})
if !ok {
return []error{err}
}
return u.Unwrap()
}

func (e *Error) Unwrap() []error {
return e.errs
}
17 changes: 17 additions & 0 deletions after_go120_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package multierror_test

import (
"errors"
"fmt"

"github.com/mkmik/multierror"
)

// multierror.Split works also with multierrors produced by the new stdlib errors.Join
func ExampleErrorsSplit() {
err := errors.Join(fmt.Errorf("foo"), fmt.Errorf("bar"), fmt.Errorf("baz"))

Check failure on line 12 in after_go120_test.go

View workflow job for this annotation

GitHub Actions / Build (1.11.x)

undefined: errors.Join

Check failure on line 12 in after_go120_test.go

View workflow job for this annotation

GitHub Actions / Build (1.12.x)

undefined: errors.Join

Check failure on line 12 in after_go120_test.go

View workflow job for this annotation

GitHub Actions / Build (1.13.x)

undefined: errors.Join

Check failure on line 12 in after_go120_test.go

View workflow job for this annotation

GitHub Actions / Build (1.17.x)

undefined: errors.Join

fmt.Printf("%q", multierror.Split(err))
// Output:
// ["foo" "bar" "baz"]
}
14 changes: 14 additions & 0 deletions before_go120.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !go1.20
// +build !go1.20

package multierror

// Split returns the underlying list of errors wrapped in a multierror.
// If err is not a multierror, then a singleton list is returned.
func Split(err error) []error {
if me, ok := err.(*Error); ok {
return me.errs
} else {
return []error{err}
}
}
10 changes: 0 additions & 10 deletions multierror.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ func Fold(errs []error) error {
return Join(errs)
}

// Split returns the underlying list of errors wrapped in a multierror.
// If err is not a multierror, then a singleton list is returned.
func Split(err error) []error {
if me, ok := err.(*Error); ok {
return me.errs
} else {
return []error{err}
}
}

// Unfold is deprecated, use Split instead.
//
// Unfold returns the underlying list of errors wrapped in a multierror.
Expand Down
44 changes: 44 additions & 0 deletions multierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ func TestAppendNil(t *testing.T) {
}
}

func TestAppendNil2(t *testing.T) {
err := fmt.Errorf("foo")
nerr := multierror.Append(nil, err)
if got, want := nerr, err; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}

func TestAppendNil3(t *testing.T) {
err1 := fmt.Errorf("foo1")
err2 := fmt.Errorf("foo2")
nerr := multierror.Append(nil, err1, err2)
serr := multierror.Split(nerr)
if got, want := len(serr), 2; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := serr[0], err1; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := serr[1], err2; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}

func TestAppendNilOnSomething(t *testing.T) {
err1 := fmt.Errorf("test")
errs := err1
Expand Down Expand Up @@ -152,6 +176,16 @@ func TestSplitSingleton(t *testing.T) {
}
}

func TestUnfoldSingleton(t *testing.T) {
errs := multierror.Unfold(fmt.Errorf("foo"))
if got, want := len(errs), 1; got != want {
t.Fatalf("got: %d, want: %d", got, want)
}
if got, want := errs[0].Error(), "foo"; got != want {
t.Fatalf("got: %q, want: %q", got, want)
}
}

func TestUniqEmpty(t *testing.T) {
errs := multierror.Uniq(nil)
if got, want := errs, []error(nil); got != nil {
Expand Down Expand Up @@ -234,3 +268,13 @@ func ExampleTransformer() {
// Output:
// foo (k1, k2); bar (k3)
}

func TestFormatLegacyError(t *testing.T) {
err := fmt.Errorf("foo")
formatted := multierror.Format(err, func(errs []string) string {
return strings.Join(errs, "; ")
})
if got, want := err, formatted; got != want {
t.Fatalf("got %v, wanted: %v", got, want)
}
}
File renamed without changes.

0 comments on commit b86e467

Please sign in to comment.