Skip to content

Commit

Permalink
replace: interface{} → any
Browse files Browse the repository at this point in the history
  • Loading branch information
lkeix committed Apr 28, 2024
1 parent 74475b0 commit 2d48a49
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 166 deletions.
16 changes: 8 additions & 8 deletions bind.go
Expand Up @@ -16,7 +16,7 @@ import (

// Binder is the interface that wraps the Bind method.
type Binder interface {
Bind(i interface{}, c Context) error
Bind(i any, c Context) error
}

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

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

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

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

// bindData will bind data ONLY fields in destination struct that have EXPLICIT tag
func (b *DefaultBinder) bindData(destination interface{}, data map[string][]string, tag string) error {
func (b *DefaultBinder) bindData(destination any, data map[string][]string, tag string) error {
if destination == nil || len(data) == 0 {
return nil
}
Expand All @@ -142,7 +142,7 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
// Support binding to limited Map destinations:
// - map[string][]string,
// - map[string]string <-- (binds first value from data slice)
// - map[string]interface{}
// - map[string]any
// You are better off binding to struct but there are user who want this map feature. Source of data for these cases are:
// params,query,header,form as these sources produce string values, most of the time slice of strings, actually.
if typ.Kind() == reflect.Map && typ.Key().Kind() == reflect.String {
Expand Down
20 changes: 10 additions & 10 deletions bind_test.go
Expand Up @@ -493,10 +493,10 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
})

t.Run("ok, bind to map[string]interface", func(t *testing.T) {
dest := map[string]interface{}{}
dest := map[string]any{}
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
map[string]any{
"multiple": []string{"1", "2"},
"single": []string{"3"},
},
Expand All @@ -505,10 +505,10 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
})

t.Run("ok, bind to map[string]interface with nil map", func(t *testing.T) {
var dest map[string]interface{}
var dest map[string]any
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
map[string]any{
"multiple": []string{"1", "2"},
"single": []string{"3"},
},
Expand Down Expand Up @@ -767,9 +767,9 @@ func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
givenURL string
givenContent io.Reader
givenMethod string
whenBindTarget interface{}
whenBindTarget any
whenNoPathParams bool
expect interface{}
expect any
expectError string
}{
{
Expand Down Expand Up @@ -902,7 +902,7 @@ func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
c.SetParamValues("node_from_path")
}

var bindTarget interface{}
var bindTarget any
if tc.whenBindTarget != nil {
bindTarget = tc.whenBindTarget
} else {
Expand Down Expand Up @@ -941,8 +941,8 @@ func TestDefaultBinder_BindBody(t *testing.T) {
givenMethod string
givenContentType string
whenNoPathParams bool
whenBindTarget interface{}
expect interface{}
whenBindTarget any
expect any
expectError string
}{
{
Expand Down Expand Up @@ -1083,7 +1083,7 @@ func TestDefaultBinder_BindBody(t *testing.T) {
c.SetParamValues("real_node")
}

var bindTarget interface{}
var bindTarget any
if tc.whenBindTarget != nil {
bindTarget = tc.whenBindTarget
} else {
Expand Down
34 changes: 17 additions & 17 deletions binder.go
Expand Up @@ -75,7 +75,7 @@ type BindingError struct {
}

// NewBindingError creates new instance of binding error
func NewBindingError(sourceParam string, values []string, message interface{}, internalError error) error {
func NewBindingError(sourceParam string, values []string, message any, internalError error) error {
return &BindingError{
Field: sourceParam,
Values: values,
Expand Down Expand Up @@ -103,7 +103,7 @@ type ValueBinder struct {
// ValuesFunc is used to get all values for parameter from request. i.e. `/api/search?ids=1&ids=2`
ValuesFunc func(sourceParam string) []string
// ErrorFunc is used to create errors. Allows you to use your own error type, that for example marshals to your specific json response
ErrorFunc func(sourceParam string, values []string, message interface{}, internalError error) error
ErrorFunc func(sourceParam string, values []string, message any, internalError error) error
}

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

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

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

func (b *ValueBinder) bindWithDelimiter(sourceParam string, dest interface{}, delimiter string, valueMustExist bool) *ValueBinder {
func (b *ValueBinder) bindWithDelimiter(sourceParam string, dest any, delimiter string, valueMustExist bool) *ValueBinder {
if b.failFast && b.errors != nil {
return b
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func (b *ValueBinder) MustInt(sourceParam string, dest *int) *ValueBinder {
return b.intValue(sourceParam, dest, 0, true)
}

func (b *ValueBinder) intValue(sourceParam string, dest interface{}, bitSize int, valueMustExist bool) *ValueBinder {
func (b *ValueBinder) intValue(sourceParam string, dest any, bitSize int, valueMustExist bool) *ValueBinder {
if b.failFast && b.errors != nil {
return b
}
Expand All @@ -517,7 +517,7 @@ func (b *ValueBinder) intValue(sourceParam string, dest interface{}, bitSize int
return b.int(sourceParam, value, dest, bitSize)
}

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

func (b *ValueBinder) intsValue(sourceParam string, dest interface{}, valueMustExist bool) *ValueBinder {
func (b *ValueBinder) intsValue(sourceParam string, dest any, valueMustExist bool) *ValueBinder {
if b.failFast && b.errors != nil {
return b
}
Expand All @@ -558,7 +558,7 @@ func (b *ValueBinder) intsValue(sourceParam string, dest interface{}, valueMustE
return b.ints(sourceParam, values, dest)
}

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

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

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

func (b *ValueBinder) uintsValue(sourceParam string, dest interface{}, valueMustExist bool) *ValueBinder {
func (b *ValueBinder) uintsValue(sourceParam string, dest any, valueMustExist bool) *ValueBinder {
if b.failFast && b.errors != nil {
return b
}
Expand All @@ -786,7 +786,7 @@ func (b *ValueBinder) uintsValue(sourceParam string, dest interface{}, valueMust
return b.uints(sourceParam, values, dest)
}

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

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

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

func (b *ValueBinder) floatsValue(sourceParam string, dest interface{}, valueMustExist bool) *ValueBinder {
func (b *ValueBinder) floatsValue(sourceParam string, dest any, valueMustExist bool) *ValueBinder {
if b.failFast && b.errors != nil {
return b
}
Expand All @@ -1039,7 +1039,7 @@ func (b *ValueBinder) floatsValue(sourceParam string, dest interface{}, valueMus
return b.floats(sourceParam, values, dest)
}

func (b *ValueBinder) floats(sourceParam string, values []string, dest interface{}) *ValueBinder {
func (b *ValueBinder) floats(sourceParam string, values []string, dest any) *ValueBinder {
switch d := dest.(type) {
case *[]float64:
tmp := make([]float64, len(values))
Expand Down
9 changes: 5 additions & 4 deletions binder_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"math/big"
"net/http"
Expand All @@ -16,6 +15,8 @@ import (
"strings"
"testing"
"time"

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

func createTestContext(URL string, body io.Reader, pathParams map[string]string) Context {
Expand Down Expand Up @@ -271,7 +272,7 @@ func TestValueBinder_CustomFunc(t *testing.T) {
givenFuncErrors []error
whenURL string
expectParamValues []string
expectValue interface{}
expectValue any
expectErrors []string
}{
{
Expand Down Expand Up @@ -346,7 +347,7 @@ func TestValueBinder_MustCustomFunc(t *testing.T) {
givenFuncErrors []error
whenURL string
expectParamValues []string
expectValue interface{}
expectValue any
expectErrors []string
}{
{
Expand Down Expand Up @@ -2376,7 +2377,7 @@ func TestValueBinder_BindWithDelimiter_types(t *testing.T) {
var testCases = []struct {
name string
whenURL string
expect interface{}
expect any
}{
{
name: "ok, strings",
Expand Down

0 comments on commit 2d48a49

Please sign in to comment.