From 81d4e50f94df3b0983035b0ea0e4b4c5cbbe6e9b Mon Sep 17 00:00:00 2001 From: Monis Khan Date: Fri, 5 Feb 2021 12:55:18 -0500 Subject: [PATCH] Remove multierror package Signed-off-by: Monis Khan --- internal/multierror/multierror.go | 63 -------------------------- internal/multierror/multierror_test.go | 24 ---------- 2 files changed, 87 deletions(-) delete mode 100644 internal/multierror/multierror.go delete mode 100644 internal/multierror/multierror_test.go diff --git a/internal/multierror/multierror.go b/internal/multierror/multierror.go deleted file mode 100644 index 5f87107f..00000000 --- a/internal/multierror/multierror.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2020 the Pinniped contributors. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// Package multierror provides a type that can translate multiple errors into a Go error interface. -// -// A common use of this package is as follows. -// errs := multierror.New() -// for i := range stuff { -// err := doThing(i) -// errs.Add(err) -// } -// return errs.ErrOrNil() -package multierror - -import ( - "fmt" - "strings" -) - -// formatFunc is a function used to format the string representing of a MultiError. It is used in the -// Error() function. -// -// It is marked out here to indicate how we could potentially extend MultiError in the future to -// support more styles of converting from a list of error's to a string. -//nolint: gochecknoglobals -var formatFunc func(errs MultiError, sb *strings.Builder) = defaultFormat - -// MultiError holds a list of error's, that could potentially be empty. -// -// Use New() to create a MultiError. -type MultiError []error - -// New returns an empty MultiError. -func New() MultiError { - return make([]error, 0) -} - -// Add adds an error to the MultiError. The provided err must not be nil. -func (m *MultiError) Add(err error) { - *m = append(*m, err) -} - -// Error implements the error.Error() interface method. -func (m MultiError) Error() string { - sb := strings.Builder{} - formatFunc(m, &sb) - return sb.String() -} - -// ErrOrNil returns either nil, if there are no errors in this MultiError, or an error, otherwise. -func (m MultiError) ErrOrNil() error { - if len(m) > 0 { - return m - } - return nil -} - -func defaultFormat(errs MultiError, sb *strings.Builder) { - _, _ = fmt.Fprintf(sb, "%d error(s):", len(errs)) - for _, err := range errs { - _, _ = fmt.Fprintf(sb, "\n- %s", err.Error()) - } -} diff --git a/internal/multierror/multierror_test.go b/internal/multierror/multierror_test.go deleted file mode 100644 index cb96771e..00000000 --- a/internal/multierror/multierror_test.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2020 the Pinniped contributors. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package multierror - -import ( - "errors" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestMultierror(t *testing.T) { - errs := New() - - require.Nil(t, errs.ErrOrNil()) - - errs.Add(errors.New("some error 1")) - require.EqualError(t, errs.ErrOrNil(), "1 error(s):\n- some error 1") - - errs.Add(errors.New("some error 2")) - errs.Add(errors.New("some error 3")) - require.EqualError(t, errs.ErrOrNil(), "3 error(s):\n- some error 1\n- some error 2\n- some error 3") -}