Skip to content

Commit

Permalink
Upgrade to Go 1.22; replace minitrue.Or with cmp.Or
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Jul 12, 2024
1 parent e4d1efd commit a2311a6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 30 deletions.
3 changes: 2 additions & 1 deletion builder_core.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package requests

import (
"cmp"

Check failure on line 4 in builder_core.go

View workflow job for this annotation

GitHub Actions / Build

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/cmp)

Check failure on line 4 in builder_core.go

View workflow job for this annotation

GitHub Actions / Build

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/cmp)
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -242,7 +243,7 @@ func (rb *Builder) Request(ctx context.Context) (req *http.Request, err error) {

// Do calls the underlying http.Client and validates and handles any resulting response. The response body is closed after all validators and the handler run.
func (rb *Builder) Do(req *http.Request) (err error) {
cl := minitrue.Or(rb.cl, http.DefaultClient)
cl := cmp.Or(rb.cl, http.DefaultClient)
if rb.rt != nil {
cl2 := *cl
cl2.Transport = rb.rt
Expand Down
5 changes: 3 additions & 2 deletions core_req.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package requests

import (
"cmp"
"context"
"io"
"net/http"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (rb *requestBuilder) Request(ctx context.Context, u *url.URL) (req *http.Re
body = nopper.Reader
}
}
method := minitrue.Or(rb.method,
method := cmp.Or(rb.method,
minitrue.Cond(rb.getBody == nil,
http.MethodGet,
http.MethodPost))
Expand All @@ -91,7 +92,7 @@ func (rb *requestBuilder) Request(ctx context.Context, u *url.URL) (req *http.Re
for _, kv := range rb.headers {
if kv.optional &&
req.Header.Get(kv.key) == "" &&
minitrue.Or(kv.values...) != "" {
cmp.Or(kv.values...) != "" {
req.Header[http.CanonicalHeaderKey(kv.key)] = kv.values
}
}
Expand Down
8 changes: 4 additions & 4 deletions core_url.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package requests

import (
"cmp"
"net/url"

"github.com/carlmjohnson/requests/internal/minitrue"
"github.com/carlmjohnson/requests/internal/slicex"
)

Expand Down Expand Up @@ -61,12 +61,12 @@ func (ub *urlBuilder) URL() (u *url.URL, err error) {
if err != nil {
return new(url.URL), err
}
u.Scheme = minitrue.Or(
u.Scheme = cmp.Or(
ub.scheme,
u.Scheme,
"https",
)
u.Host = minitrue.Or(ub.host, u.Host)
u.Host = cmp.Or(ub.host, u.Host)
for _, p := range ub.paths {
u.Path = u.ResolveReference(&url.URL{Path: p}).Path
}
Expand All @@ -80,7 +80,7 @@ func (ub *urlBuilder) URL() (u *url.URL, err error) {
for _, kv := range ub.params {
if kv.optional &&
q.Get(kv.key) == "" &&
minitrue.Or(kv.values...) != "" {
cmp.Or(kv.values...) != "" {
q[kv.key] = kv.values
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/carlmjohnson/requests

go 1.20
go 1.22

require golang.org/x/net v0.23.0
require golang.org/x/net v0.27.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
11 changes: 0 additions & 11 deletions internal/minitrue/minitrue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,3 @@ func Cond[T any](val bool, a, b T) T {
}
return b
}

// Or returns the first non-empty argument it receives
// or the zero value for T.
func Or[T comparable](vals ...T) T {
for _, val := range vals {
if val != *new(T) {
return val
}
}
return *new(T)
}
8 changes: 0 additions & 8 deletions internal/minitrue/minitrue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,3 @@ func TestCond(t *testing.T) {
be.Equal(t, 1, minitrue.Cond(true, 1, 2))
be.Equal(t, 2, minitrue.Cond(false, 1, 2))
}

func TestOr(t *testing.T) {
be.Equal(t, 0, minitrue.Or[int]())
be.Equal(t, 0, minitrue.Or(0))
be.Equal(t, 1, minitrue.Or(1))
be.Equal(t, 2, minitrue.Or(0, 2))
be.Equal(t, 3, minitrue.Or(0, 0, 3))
}

0 comments on commit a2311a6

Please sign in to comment.