Skip to content

Commit 2d48a49

Browse files
committed
replace: interface{} → any
1 parent 74475b0 commit 2d48a49

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,
@@ -103,7 +103,7 @@ type ValueBinder struct {
103103
// ValuesFunc is used to get all values for parameter from request. i.e. `/api/search?ids=1&ids=2`
104104
ValuesFunc func(sourceParam string) []string
105105
// ErrorFunc is used to create errors. Allows you to use your own error type, that for example marshals to your specific json response
106-
ErrorFunc func(sourceParam string, values []string, message interface{}, internalError error) error
106+
ErrorFunc func(sourceParam string, values []string, message any, internalError error) error
107107
}
108108

109109
// QueryParamsBinder creates query parameter value binder
@@ -403,17 +403,17 @@ func (b *ValueBinder) MustTextUnmarshaler(sourceParam string, dest encoding.Text
403403

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1042-
func (b *ValueBinder) floats(sourceParam string, values []string, dest interface{}) *ValueBinder {
1042+
func (b *ValueBinder) floats(sourceParam string, values []string, dest any) *ValueBinder {
10431043
switch d := dest.(type) {
10441044
case *[]float64:
10451045
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)