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

Add an AppendNoFlatten helper function #31

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 17 additions & 3 deletions append.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ package multierror
// onto an Error in order to create a larger multi-error.
//
// If err is not a multierror.Error, then it will be turned into
// one. If any of the errs are multierr.Error, they will be flattened
// one. If any of the errs are multierror.Error, they will be flattened
// one level into err.
func Append(err error, errs ...error) *Error {
return appendFlattenOrNo(true, err, errs...)
}

// AppendNoFlatten is just like Append, except that if any of the
// errs are multierror.Error, they will not be flattened into err.
func AppendNoFlatten(err error, errs ...error) *Error {
return appendFlattenOrNo(false, err, errs...)
}

func appendFlattenOrNo(flatten bool, err error, errs ...error) *Error {
switch err := err.(type) {
case *Error:
// Typed nils can reach here, so initialize if we are nil
Expand All @@ -19,7 +29,11 @@ func Append(err error, errs ...error) *Error {
switch e := e.(type) {
case *Error:
if e != nil {
err.Errors = append(err.Errors, e.Errors...)
if flatten {
err.Errors = append(err.Errors, e.Errors...)
} else {
err.Errors = append(err.Errors, e)
}
}
default:
if e != nil {
Expand All @@ -36,6 +50,6 @@ func Append(err error, errs ...error) *Error {
}
newErrs = append(newErrs, errs...)

return Append(&Error{}, newErrs...)
return appendFlattenOrNo(flatten, &Error{}, newErrs...)
}
}
92 changes: 92 additions & 0 deletions append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ func TestAppend_Error(t *testing.T) {
}
}

func TestAppendNoFlatten_Error(t *testing.T) {
original := &Error{
Errors: []error{errors.New("foo")},
}

result := AppendNoFlatten(original, errors.New("bar"))
if len(result.Errors) != 2 {
t.Fatalf("wrong len: %d", len(result.Errors))
}

original = &Error{}
result = AppendNoFlatten(original, errors.New("bar"))
if len(result.Errors) != 1 {
t.Fatalf("wrong len: %d", len(result.Errors))
}

// Test when a typed nil is passed
var e *Error
result = AppendNoFlatten(e, errors.New("baz"))
if len(result.Errors) != 1 {
t.Fatalf("wrong len: %d", len(result.Errors))
}

// Test flattening
original = &Error{
Errors: []error{errors.New("foo")},
}

result = AppendNoFlatten(original, AppendNoFlatten(nil, errors.New("foo"), errors.New("bar")))
if len(result.Errors) != 2 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}

func TestAppend_NilError(t *testing.T) {
var err error
result := Append(err, errors.New("bar"))
Expand All @@ -47,6 +81,30 @@ func TestAppend_NilError(t *testing.T) {
}
}

func TestAppendNoFlatten_NilError(t *testing.T) {
var err error
result := AppendNoFlatten(err, errors.New("bar"))
if len(result.Errors) != 1 {
t.Fatalf("wrong len: %d", len(result.Errors))
}

// Test flattening...
inner := Append(errors.New("foo"), errors.New("bar"))

// ...with nil error:
result = AppendNoFlatten(nil, inner)
if len(result.Errors) != 1 {
t.Fatalf("wrong len: %d", len(result.Errors))
}

// ...with nil *Error:
var nilErr *Error
result = AppendNoFlatten(nilErr, inner)
if len(result.Errors) != 1 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}

func TestAppend_NilErrorArg(t *testing.T) {
var err error
var nilErr *Error
Expand All @@ -56,6 +114,15 @@ func TestAppend_NilErrorArg(t *testing.T) {
}
}

func TestAppendNoFlatten_NilErrorArg(t *testing.T) {
var err error
var nilErr *Error
result := AppendNoFlatten(err, nilErr)
if len(result.Errors) != 0 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}

func TestAppend_NilErrorIfaceArg(t *testing.T) {
var err error
var nilErr error
Expand All @@ -65,6 +132,15 @@ func TestAppend_NilErrorIfaceArg(t *testing.T) {
}
}

func TestAppendNoFlatten_NilErrorIfaceArg(t *testing.T) {
var err error
var nilErr error
result := AppendNoFlatten(err, nilErr)
if len(result.Errors) != 0 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}

func TestAppend_NonError(t *testing.T) {
original := errors.New("foo")
result := Append(original, errors.New("bar"))
Expand All @@ -73,10 +149,26 @@ func TestAppend_NonError(t *testing.T) {
}
}

func TestAppendNoFlatten_NonError(t *testing.T) {
original := errors.New("foo")
result := AppendNoFlatten(original, errors.New("bar"))
if len(result.Errors) != 2 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}

func TestAppend_NonError_Error(t *testing.T) {
original := errors.New("foo")
result := Append(original, Append(nil, errors.New("bar")))
if len(result.Errors) != 2 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}

func TestAppendNoFlatten_NonError_Error(t *testing.T) {
original := errors.New("foo")
result := AppendNoFlatten(original, AppendNoFlatten(nil, errors.New("bar")))
if len(result.Errors) != 2 {
t.Fatalf("wrong len: %d", len(result.Errors))
}
}