Skip to content

Commit 81bb9d8

Browse files
committed
replace: interface{} → any
1 parent f7d9f51 commit 81bb9d8

22 files changed

+169
-166
lines changed

bind.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// Binder is the interface that wraps the Bind method.
1818
type Binder interface {
19-
Bind(i interface{}, c Context) error
19+
Bind(i any, c Context) error
2020
}
2121

2222
// DefaultBinder is the default implementation of the Binder interface.
@@ -38,7 +38,7 @@ type bindMultipleUnmarshaler interface {
3838
}
3939

4040
// BindPathParams binds path params to bindable object
41-
func (b *DefaultBinder) BindPathParams(c Context, i interface{}) error {
41+
func (b *DefaultBinder) BindPathParams(c Context, i any) error {
4242
names := c.ParamNames()
4343
values := c.ParamValues()
4444
params := map[string][]string{}
@@ -52,7 +52,7 @@ func (b *DefaultBinder) BindPathParams(c Context, i interface{}) error {
5252
}
5353

5454
// BindQueryParams binds query params to bindable object
55-
func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
55+
func (b *DefaultBinder) BindQueryParams(c Context, i any) error {
5656
if err := b.bindData(i, c.QueryParams(), "query"); err != nil {
5757
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
5858
}
@@ -64,7 +64,7 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error {
6464
// which parses form data from BOTH URL and BODY if content type is not MIMEMultipartForm
6565
// See non-MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseForm
6666
// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
67-
func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
67+
func (b *DefaultBinder) BindBody(c Context, i any) (err error) {
6868
req := c.Request()
6969
if req.ContentLength == 0 {
7070
return
@@ -105,7 +105,7 @@ func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
105105
}
106106

107107
// BindHeaders binds HTTP headers to a bindable object
108-
func (b *DefaultBinder) BindHeaders(c Context, i interface{}) error {
108+
func (b *DefaultBinder) BindHeaders(c Context, i any) error {
109109
if err := b.bindData(i, c.Request().Header, "header"); err != nil {
110110
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
111111
}
@@ -115,7 +115,7 @@ func (b *DefaultBinder) BindHeaders(c Context, i interface{}) error {
115115
// Bind implements the `Binder#Bind` function.
116116
// Binding is done in following order: 1) path params; 2) query params; 3) request body. Each step COULD override previous
117117
// step binded values. For single source binding use their own methods BindBody, BindQueryParams, BindPathParams.
118-
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
118+
func (b *DefaultBinder) Bind(i any, c Context) (err error) {
119119
if err := b.BindPathParams(c, i); err != nil {
120120
return err
121121
}
@@ -132,7 +132,7 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
132132
}
133133

134134
// bindData will bind data ONLY fields in destination struct that have EXPLICIT tag
135-
func (b *DefaultBinder) bindData(destination interface{}, data map[string][]string, tag string) error {
135+
func (b *DefaultBinder) bindData(destination any, data map[string][]string, tag string) error {
136136
if destination == nil || len(data) == 0 {
137137
return nil
138138
}
@@ -142,7 +142,7 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
142142
// Support binding to limited Map destinations:
143143
// - map[string][]string,
144144
// - map[string]string <-- (binds first value from data slice)
145-
// - map[string]interface{}
145+
// - map[string]any
146146
// You are better off binding to struct but there are user who want this map feature. Source of data for these cases are:
147147
// params,query,header,form as these sources produce string values, most of the time slice of strings, actually.
148148
if typ.Kind() == reflect.Map && typ.Key().Kind() == reflect.String {

bind_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
493493
})
494494

495495
t.Run("ok, bind to map[string]interface", func(t *testing.T) {
496-
dest := map[string]interface{}{}
496+
dest := map[string]any{}
497497
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
498498
assert.Equal(t,
499-
map[string]interface{}{
499+
map[string]any{
500500
"multiple": []string{"1", "2"},
501501
"single": []string{"3"},
502502
},
@@ -505,10 +505,10 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
505505
})
506506

507507
t.Run("ok, bind to map[string]interface with nil map", func(t *testing.T) {
508-
var dest map[string]interface{}
508+
var dest map[string]any
509509
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
510510
assert.Equal(t,
511-
map[string]interface{}{
511+
map[string]any{
512512
"multiple": []string{"1", "2"},
513513
"single": []string{"3"},
514514
},
@@ -767,9 +767,9 @@ func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
767767
givenURL string
768768
givenContent io.Reader
769769
givenMethod string
770-
whenBindTarget interface{}
770+
whenBindTarget any
771771
whenNoPathParams bool
772-
expect interface{}
772+
expect any
773773
expectError string
774774
}{
775775
{
@@ -902,7 +902,7 @@ func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
902902
c.SetParamValues("node_from_path")
903903
}
904904

905-
var bindTarget interface{}
905+
var bindTarget any
906906
if tc.whenBindTarget != nil {
907907
bindTarget = tc.whenBindTarget
908908
} else {
@@ -941,8 +941,8 @@ func TestDefaultBinder_BindBody(t *testing.T) {
941941
givenMethod string
942942
givenContentType string
943943
whenNoPathParams bool
944-
whenBindTarget interface{}
945-
expect interface{}
944+
whenBindTarget any
945+
expect any
946946
expectError string
947947
}{
948948
{
@@ -1083,7 +1083,7 @@ func TestDefaultBinder_BindBody(t *testing.T) {
10831083
c.SetParamValues("real_node")
10841084
}
10851085

1086-
var bindTarget interface{}
1086+
var bindTarget any
10871087
if tc.whenBindTarget != nil {
10881088
bindTarget = tc.whenBindTarget
10891089
} else {

binder.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type BindingError struct {
7575
}
7676

7777
// NewBindingError creates new instance of binding error
78-
func NewBindingError(sourceParam string, values []string, message interface{}, internalError error) error {
78+
func NewBindingError(sourceParam string, values []string, message any, internalError error) error {
7979
return &BindingError{
8080
Field: sourceParam,
8181
Values: values,
@@ -99,7 +99,7 @@ type ValueBinder struct {
9999
// ValuesFunc is used to get all values for parameter from request. i.e. `/api/search?ids=1&ids=2`
100100
ValuesFunc func(sourceParam string) []string
101101
// ErrorFunc is used to create errors. Allows you to use your own error type, that for example marshals to your specific json response
102-
ErrorFunc func(sourceParam string, values []string, message interface{}, internalError error) error
102+
ErrorFunc func(sourceParam string, values []string, message any, internalError error) error
103103
errors []error
104104
// failFast is flag for binding methods to return without attempting to bind when previous binding already failed
105105
failFast bool
@@ -402,17 +402,17 @@ func (b *ValueBinder) MustTextUnmarshaler(sourceParam string, dest encoding.Text
402402

403403
// BindWithDelimiter binds parameter to destination by suitable conversion function.
404404
// Delimiter is used before conversion to split parameter value to separate values
405-
func (b *ValueBinder) BindWithDelimiter(sourceParam string, dest interface{}, delimiter string) *ValueBinder {
405+
func (b *ValueBinder) BindWithDelimiter(sourceParam string, dest any, delimiter string) *ValueBinder {
406406
return b.bindWithDelimiter(sourceParam, dest, delimiter, false)
407407
}
408408

409409
// MustBindWithDelimiter requires parameter value to exist to bind destination by suitable conversion function.
410410
// Delimiter is used before conversion to split parameter value to separate values
411-
func (b *ValueBinder) MustBindWithDelimiter(sourceParam string, dest interface{}, delimiter string) *ValueBinder {
411+
func (b *ValueBinder) MustBindWithDelimiter(sourceParam string, dest any, delimiter string) *ValueBinder {
412412
return b.bindWithDelimiter(sourceParam, dest, delimiter, true)
413413
}
414414

415-
func (b *ValueBinder) bindWithDelimiter(sourceParam string, dest interface{}, delimiter string, valueMustExist bool) *ValueBinder {
415+
func (b *ValueBinder) bindWithDelimiter(sourceParam string, dest any, delimiter string, valueMustExist bool) *ValueBinder {
416416
if b.failFast && b.errors != nil {
417417
return b
418418
}
@@ -500,7 +500,7 @@ func (b *ValueBinder) MustInt(sourceParam string, dest *int) *ValueBinder {
500500
return b.intValue(sourceParam, dest, 0, true)
501501
}
502502

503-
func (b *ValueBinder) intValue(sourceParam string, dest interface{}, bitSize int, valueMustExist bool) *ValueBinder {
503+
func (b *ValueBinder) intValue(sourceParam string, dest any, bitSize int, valueMustExist bool) *ValueBinder {
504504
if b.failFast && b.errors != nil {
505505
return b
506506
}
@@ -516,7 +516,7 @@ func (b *ValueBinder) intValue(sourceParam string, dest interface{}, bitSize int
516516
return b.int(sourceParam, value, dest, bitSize)
517517
}
518518

519-
func (b *ValueBinder) int(sourceParam string, value string, dest interface{}, bitSize int) *ValueBinder {
519+
func (b *ValueBinder) int(sourceParam string, value string, dest any, bitSize int) *ValueBinder {
520520
n, err := strconv.ParseInt(value, 10, bitSize)
521521
if err != nil {
522522
if bitSize == 0 {
@@ -542,7 +542,7 @@ func (b *ValueBinder) int(sourceParam string, value string, dest interface{}, bi
542542
return b
543543
}
544544

545-
func (b *ValueBinder) intsValue(sourceParam string, dest interface{}, valueMustExist bool) *ValueBinder {
545+
func (b *ValueBinder) intsValue(sourceParam string, dest any, valueMustExist bool) *ValueBinder {
546546
if b.failFast && b.errors != nil {
547547
return b
548548
}
@@ -557,7 +557,7 @@ func (b *ValueBinder) intsValue(sourceParam string, dest interface{}, valueMustE
557557
return b.ints(sourceParam, values, dest)
558558
}
559559

560-
func (b *ValueBinder) ints(sourceParam string, values []string, dest interface{}) *ValueBinder {
560+
func (b *ValueBinder) ints(sourceParam string, values []string, dest any) *ValueBinder {
561561
switch d := dest.(type) {
562562
case *[]int64:
563563
tmp := make([]int64, len(values))
@@ -728,7 +728,7 @@ func (b *ValueBinder) MustUint(sourceParam string, dest *uint) *ValueBinder {
728728
return b.uintValue(sourceParam, dest, 0, true)
729729
}
730730

731-
func (b *ValueBinder) uintValue(sourceParam string, dest interface{}, bitSize int, valueMustExist bool) *ValueBinder {
731+
func (b *ValueBinder) uintValue(sourceParam string, dest any, bitSize int, valueMustExist bool) *ValueBinder {
732732
if b.failFast && b.errors != nil {
733733
return b
734734
}
@@ -744,7 +744,7 @@ func (b *ValueBinder) uintValue(sourceParam string, dest interface{}, bitSize in
744744
return b.uint(sourceParam, value, dest, bitSize)
745745
}
746746

747-
func (b *ValueBinder) uint(sourceParam string, value string, dest interface{}, bitSize int) *ValueBinder {
747+
func (b *ValueBinder) uint(sourceParam string, value string, dest any, bitSize int) *ValueBinder {
748748
n, err := strconv.ParseUint(value, 10, bitSize)
749749
if err != nil {
750750
if bitSize == 0 {
@@ -770,7 +770,7 @@ func (b *ValueBinder) uint(sourceParam string, value string, dest interface{}, b
770770
return b
771771
}
772772

773-
func (b *ValueBinder) uintsValue(sourceParam string, dest interface{}, valueMustExist bool) *ValueBinder {
773+
func (b *ValueBinder) uintsValue(sourceParam string, dest any, valueMustExist bool) *ValueBinder {
774774
if b.failFast && b.errors != nil {
775775
return b
776776
}
@@ -785,7 +785,7 @@ func (b *ValueBinder) uintsValue(sourceParam string, dest interface{}, valueMust
785785
return b.uints(sourceParam, values, dest)
786786
}
787787

788-
func (b *ValueBinder) uints(sourceParam string, values []string, dest interface{}) *ValueBinder {
788+
func (b *ValueBinder) uints(sourceParam string, values []string, dest any) *ValueBinder {
789789
switch d := dest.(type) {
790790
case *[]uint64:
791791
tmp := make([]uint64, len(values))
@@ -991,7 +991,7 @@ func (b *ValueBinder) MustFloat32(sourceParam string, dest *float32) *ValueBinde
991991
return b.floatValue(sourceParam, dest, 32, true)
992992
}
993993

994-
func (b *ValueBinder) floatValue(sourceParam string, dest interface{}, bitSize int, valueMustExist bool) *ValueBinder {
994+
func (b *ValueBinder) floatValue(sourceParam string, dest any, bitSize int, valueMustExist bool) *ValueBinder {
995995
if b.failFast && b.errors != nil {
996996
return b
997997
}
@@ -1007,7 +1007,7 @@ func (b *ValueBinder) floatValue(sourceParam string, dest interface{}, bitSize i
10071007
return b.float(sourceParam, value, dest, bitSize)
10081008
}
10091009

1010-
func (b *ValueBinder) float(sourceParam string, value string, dest interface{}, bitSize int) *ValueBinder {
1010+
func (b *ValueBinder) float(sourceParam string, value string, dest any, bitSize int) *ValueBinder {
10111011
n, err := strconv.ParseFloat(value, bitSize)
10121012
if err != nil {
10131013
b.setError(b.ErrorFunc(sourceParam, []string{value}, fmt.Sprintf("failed to bind field value to float%v", bitSize), err))
@@ -1023,7 +1023,7 @@ func (b *ValueBinder) float(sourceParam string, value string, dest interface{},
10231023
return b
10241024
}
10251025

1026-
func (b *ValueBinder) floatsValue(sourceParam string, dest interface{}, valueMustExist bool) *ValueBinder {
1026+
func (b *ValueBinder) floatsValue(sourceParam string, dest any, valueMustExist bool) *ValueBinder {
10271027
if b.failFast && b.errors != nil {
10281028
return b
10291029
}
@@ -1038,7 +1038,7 @@ func (b *ValueBinder) floatsValue(sourceParam string, dest interface{}, valueMus
10381038
return b.floats(sourceParam, values, dest)
10391039
}
10401040

1041-
func (b *ValueBinder) floats(sourceParam string, values []string, dest interface{}) *ValueBinder {
1041+
func (b *ValueBinder) floats(sourceParam string, values []string, dest any) *ValueBinder {
10421042
switch d := dest.(type) {
10431043
case *[]float64:
10441044
tmp := make([]float64, len(values))

binder_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"github.com/stretchr/testify/assert"
1110
"io"
1211
"math/big"
1312
"net/http"
@@ -16,6 +15,8 @@ import (
1615
"strings"
1716
"testing"
1817
"time"
18+
19+
"github.com/stretchr/testify/assert"
1920
)
2021

2122
func createTestContext(URL string, body io.Reader, pathParams map[string]string) Context {
@@ -271,7 +272,7 @@ func TestValueBinder_CustomFunc(t *testing.T) {
271272
givenFuncErrors []error
272273
whenURL string
273274
expectParamValues []string
274-
expectValue interface{}
275+
expectValue any
275276
expectErrors []string
276277
}{
277278
{
@@ -346,7 +347,7 @@ func TestValueBinder_MustCustomFunc(t *testing.T) {
346347
givenFuncErrors []error
347348
whenURL string
348349
expectParamValues []string
349-
expectValue interface{}
350+
expectValue any
350351
expectErrors []string
351352
}{
352353
{
@@ -2376,7 +2377,7 @@ func TestValueBinder_BindWithDelimiter_types(t *testing.T) {
23762377
var testCases = []struct {
23772378
name string
23782379
whenURL string
2379-
expect interface{}
2380+
expect any
23802381
}{
23812382
{
23822383
name: "ok, strings",

0 commit comments

Comments
 (0)