Skip to content

Commit

Permalink
remove dependencies, move to go 1.18 again
Browse files Browse the repository at this point in the history
  • Loading branch information
broothie committed Feb 16, 2024
1 parent f6cd2a7 commit 0c8b718
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
30 changes: 19 additions & 11 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"errors"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type Client struct {
Expand Down Expand Up @@ -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)
}
}
6 changes: 2 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package option

import (
"errors"
"fmt"
)

Expand All @@ -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
}

0 comments on commit 0c8b718

Please sign in to comment.