Skip to content

Commit d5715da

Browse files
author
Dean Karn
authored
General maintenance from go1.21 & 1.22 changes (#47)
1 parent 67dfc10 commit d5715da

File tree

15 files changed

+428
-158
lines changed

15 files changed

+428
-158
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ jobs:
3838
with:
3939
go-version: stable
4040
- name: golangci-lint
41-
uses: golangci/golangci-lint-action@v3
41+
uses: golangci/golangci-lint-action@v4
4242
with:
4343
version: latest

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

1313
# Output of the go coverage tool, specifically when used with LiteIDE
1414
*.out
15-
15+
.idea

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [5.28.0] - 2024-02-13
10+
### Added
11+
- Additionally supported types, cast to `sql.Valuer` supported types.
12+
13+
### Changed
14+
- Option scan to take advantage of new `sql.Null` and `reflect.TypeFor` for go1.22+.
15+
- `BytesToString` & `StringToBytes` to use `unsafe.String` & `unsafe.Slice` for go1.21+.
16+
17+
### Deprecated
18+
- `mathext.Min` & `mathext.Max` in favour of std lib min & max.
19+
20+
### Fixed
21+
- Some documentation typos.
22+
923
## [5.27.0] - 2024-01-29
1024
### Changed
1125
- `sliceext.Retain` & `sliceext.Filter` to not shuffle data in the underlying slice array but create new slice referencing the data instead. In practice, it can cause unexpected behaviour and users expectations not met when the same data is also referenced elsewhere. If anyone still requires a `shuffle` implementation for efficiency I'd be happy to add a separate function for that as well.
@@ -102,7 +116,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
102116
### Added
103117
- Added `timext.NanoTime` for fast low level monotonic time with nanosecond precision.
104118

105-
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.26.0...HEAD
119+
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.28.0...HEAD
120+
[5.28.0]: https://github.com/go-playground/pkg/compare/v5.27.0..v5.28.0
121+
[5.27.0]: https://github.com/go-playground/pkg/compare/v5.26.0..v5.27.0
106122
[5.26.0]: https://github.com/go-playground/pkg/compare/v5.25.0..v5.26.0
107123
[5.25.0]: https://github.com/go-playground/pkg/compare/v5.24.0..v5.25.0
108124
[5.24.0]: https://github.com/go-playground/pkg/compare/v5.23.0..v5.24.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pkg
22

3-
![Project status](https://img.shields.io/badge/version-5.27.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.28.0-green.svg)
44
[![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
66
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

math/max.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
//go:build go1.18
2-
// +build go1.18
1+
//go:build go1.18 && !go1.21
2+
// +build go1.18,!go1.21
33

44
package mathext
55

66
import (
7-
constraintsext "github.com/go-playground/pkg/v5/constraints"
87
"math"
8+
9+
constraintsext "github.com/go-playground/pkg/v5/constraints"
910
)
1011

1112
// Max returns the larger value.

math/max_go121.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build go1.21
2+
3+
package mathext
4+
5+
import (
6+
constraintsext "github.com/go-playground/pkg/v5/constraints"
7+
)
8+
9+
// Max returns the larger value.
10+
//
11+
// NOTE: this function does not check for difference in floats of 0/zero vs -0/negative zero using Signbit.
12+
//
13+
// Deprecated: use the new std library `max` instead.
14+
func Max[N constraintsext.Number](x, y N) N {
15+
return max(x, y)
16+
}

math/min.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
//go:build go1.18
2-
// +build go1.18
1+
//go:build go1.18 && !go1.21
2+
// +build go1.18,!go1.21
33

44
package mathext
55

66
import (
7-
constraintsext "github.com/go-playground/pkg/v5/constraints"
87
"math"
8+
9+
constraintsext "github.com/go-playground/pkg/v5/constraints"
910
)
1011

1112
// Min returns the smaller value.

math/min_go121.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build go1.21
2+
3+
package mathext
4+
5+
import (
6+
constraintsext "github.com/go-playground/pkg/v5/constraints"
7+
)
8+
9+
// Min returns the smaller value.
10+
//
11+
// NOTE: this function does not check for difference in floats of 0/zero vs -0/negative zero using Signbit.
12+
//
13+
// Deprecated: use the new std library `max` instead.
14+
func Min[N constraintsext.Number](x, y N) N {
15+
return min(x, y)
16+
}

net/http/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func AcceptedLanguages(r *http.Request) (languages []string) {
5959
return
6060
}
6161

62-
// Attachment is a helper method for returning an attachement file
62+
// Attachment is a helper method for returning an attachment file
6363
// to be downloaded, if you with to open inline see function Inline
6464
func Attachment(w http.ResponseWriter, r io.Reader, filename string) (err error) {
6565
w.Header().Set(ContentDisposition, "attachment;filename="+filename)
@@ -79,7 +79,7 @@ func Inline(w http.ResponseWriter, r io.Reader, filename string) (err error) {
7979
return
8080
}
8181

82-
// ClientIP implements a best effort algorithm to return the real client IP, it parses
82+
// ClientIP implements the best effort algorithm to return the real client IP, it parses
8383
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
8484
func ClientIP(r *http.Request) (clientIP string) {
8585
values := r.Header[XRealIP]
@@ -103,7 +103,7 @@ func ClientIP(r *http.Request) (clientIP string) {
103103
return
104104
}
105105

106-
// JSONStream uses json.Encoder to stream the JSON reponse body.
106+
// JSONStream uses json.Encoder to stream the JSON response body.
107107
//
108108
// This differs from the JSON helper which unmarshalls into memory first allowing the capture of JSON encoding errors.
109109
func JSONStream(w http.ResponseWriter, status int, i interface{}) error {

net/http/retryable.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package httpext
66
import (
77
"context"
88
"fmt"
9-
bytesext "github.com/go-playground/pkg/v5/bytes"
10-
errorsext "github.com/go-playground/pkg/v5/errors"
11-
resultext "github.com/go-playground/pkg/v5/values/result"
129
"net/http"
1310
"strconv"
11+
12+
bytesext "github.com/go-playground/pkg/v5/bytes"
13+
errorsext "github.com/go-playground/pkg/v5/errors"
14+
. "github.com/go-playground/pkg/v5/values/result"
1415
)
1516

1617
var (
@@ -59,39 +60,39 @@ type BuildRequestFn func(ctx context.Context) (*http.Request, error)
5960
type IsRetryableStatusCodeFn func(code int) bool
6061

6162
// DoRetryableResponse will execute the provided functions code and automatically retry before returning the *http.Response.
62-
func DoRetryableResponse(ctx context.Context, onRetryFn errorsext.OnRetryFn[error], isRetryableStatusCode IsRetryableStatusCodeFn, client *http.Client, buildFn BuildRequestFn) resultext.Result[*http.Response, error] {
63+
func DoRetryableResponse(ctx context.Context, onRetryFn errorsext.OnRetryFn[error], isRetryableStatusCode IsRetryableStatusCodeFn, client *http.Client, buildFn BuildRequestFn) Result[*http.Response, error] {
6364
if client == nil {
6465
client = http.DefaultClient
6566
}
6667
var attempt int
6768
for {
6869
req, err := buildFn(ctx)
6970
if err != nil {
70-
return resultext.Err[*http.Response, error](err)
71+
return Err[*http.Response, error](err)
7172
}
7273

7374
resp, err := client.Do(req)
7475
if err != nil {
7576
if retryReason, isRetryable := errorsext.IsRetryableHTTP(err); isRetryable {
7677
opt := onRetryFn(ctx, err, retryReason, attempt)
7778
if opt.IsSome() {
78-
return resultext.Err[*http.Response, error](opt.Unwrap())
79+
return Err[*http.Response, error](opt.Unwrap())
7980
}
8081
attempt++
8182
continue
8283
}
83-
return resultext.Err[*http.Response, error](err)
84+
return Err[*http.Response, error](err)
8485
}
8586

8687
if isRetryableStatusCode(resp.StatusCode) {
8788
opt := onRetryFn(ctx, ErrRetryableStatusCode{Response: resp}, strconv.Itoa(resp.StatusCode), attempt)
8889
if opt.IsSome() {
89-
return resultext.Err[*http.Response, error](opt.Unwrap())
90+
return Err[*http.Response, error](opt.Unwrap())
9091
}
9192
attempt++
9293
continue
9394
}
94-
return resultext.Ok[*http.Response, error](resp)
95+
return Ok[*http.Response, error](resp)
9596
}
9697
}
9798

@@ -101,25 +102,25 @@ func DoRetryableResponse(ctx context.Context, onRetryFn errorsext.OnRetryFn[erro
101102
// Gzip supported:
102103
// - JSON
103104
// - XML
104-
func DoRetryable[T any](ctx context.Context, isRetryableFn errorsext.IsRetryableFn[error], onRetryFn errorsext.OnRetryFn[error], isRetryableStatusCode IsRetryableStatusCodeFn, client *http.Client, expectedResponseCode int, maxMemory bytesext.Bytes, buildFn BuildRequestFn) resultext.Result[T, error] {
105+
func DoRetryable[T any](ctx context.Context, isRetryableFn errorsext.IsRetryableFn[error], onRetryFn errorsext.OnRetryFn[error], isRetryableStatusCode IsRetryableStatusCodeFn, client *http.Client, expectedResponseCode int, maxMemory bytesext.Bytes, buildFn BuildRequestFn) Result[T, error] {
105106

106-
return errorsext.DoRetryable(ctx, isRetryableFn, onRetryFn, func(ctx context.Context) resultext.Result[T, error] {
107+
return errorsext.DoRetryable(ctx, isRetryableFn, onRetryFn, func(ctx context.Context) Result[T, error] {
107108

108109
result := DoRetryableResponse(ctx, onRetryFn, isRetryableStatusCode, client, buildFn)
109110
if result.IsErr() {
110-
return resultext.Err[T, error](result.Err())
111+
return Err[T, error](result.Err())
111112
}
112113
resp := result.Unwrap()
113114

114115
if resp.StatusCode != expectedResponseCode {
115-
return resultext.Err[T, error](ErrUnexpectedResponse{Response: resp})
116+
return Err[T, error](ErrUnexpectedResponse{Response: resp})
116117
}
117118
defer resp.Body.Close()
118119

119120
data, err := DecodeResponse[T](resp, maxMemory)
120121
if err != nil {
121-
return resultext.Err[T, error](err)
122+
return Err[T, error](err)
122123
}
123-
return resultext.Ok[T, error](data)
124+
return Ok[T, error](data)
124125
})
125126
}

0 commit comments

Comments
 (0)