diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 6fbd93c..0e2f1e5 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -15,7 +15,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.20 + go-version: 1.18 - run: go test ./... -coverprofile cover.out diff --git a/go.mod b/go.mod index 504f5bf..5513ba4 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,3 @@ module github.com/broothie/option -go 1.20 - -require github.com/stretchr/testify v1.8.4 - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) +go 1.18 diff --git a/go.sum b/go.sum index fa4b6e6..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/option_test.go b/option_test.go index 3970b98..351b0fc 100644 --- a/option_test.go +++ b/option_test.go @@ -4,9 +4,6 @@ import ( "errors" "net/url" "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) type Client struct { @@ -37,23 +34,34 @@ func TestApply(t *testing.T) { t.Run("applies options", func(t *testing.T) { apiKey := "foo-api-key" baseURL, err := url.Parse("https://example.com") - require.NoError(t, err) + requireNoError(t, err) - client, err := Apply[Client](Client{}, + got, err := Apply[Client](Client{}, OptionAPIKey(apiKey), OptionBaseURL(baseURL.String()), ) - require.NoError(t, err) + requireNoError(t, err) - assert.Equal(t, Client{APIKey: apiKey, BaseURL: baseURL}, client) + want := Client{APIKey: apiKey, BaseURL: baseURL} + if got.APIKey != want.APIKey || got.BaseURL.String() != want.BaseURL.String() { + t.Errorf("got: %v, want: %v", got, want) + } }) - t.Run("collects errors", func(t *testing.T) { - _, err := Apply[Client](Client{}, - OptionBaseURL("%"), + t.Run("fails on error", func(t *testing.T) { + _, got := Apply[Client](Client{}, Func[Client](func(Client) (Client, error) { return Client{}, errors.New("foo error") }), ) - assert.EqualError(t, err, "failed to apply option 0: parse \"%\": invalid URL escape \"%\"\nfailed to apply option 1: foo error") + want := "failed to apply option 0: foo error" + if got.Error() != want { + t.Errorf("got: %v, want: %v", got, want) + } }) } + +func requireNoError(t *testing.T, err error) { + if err != nil { + t.Fatalf("error when none expected: %v", err) + } +} diff --git a/options.go b/options.go index ef99c73..7a08030 100644 --- a/options.go +++ b/options.go @@ -1,7 +1,6 @@ package option import ( - "errors" "fmt" ) @@ -20,13 +19,12 @@ func NewOptions[T any](options ...Option[T]) Options[T] { // Apply applies a list of options to t. func (o Options[T]) Apply(t T) (T, error) { - var errs []error for i, option := range o { var err error if t, err = option.Apply(t); err != nil { - errs = append(errs, fmt.Errorf("failed to apply option %d: %w", i, err)) + return t, fmt.Errorf("failed to apply option %d: %w", i, err) } } - return t, errors.Join(errs...) + return t, nil }