Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Dec 20, 2024
1 parent 8ce1fb4 commit 043dd20
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 97 deletions.
3 changes: 1 addition & 2 deletions binder/cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ func Test_CBORBinder_Bind(t *testing.T) {

type User struct {
Name string `cbor:"name"`
Posts []Post `cbor:"posts"`
Names []string `cbor:"names"`
Age int `cbor:"age"`

Posts []Post `cbor:"posts"`
}
var user User

Expand Down
12 changes: 6 additions & 6 deletions binder/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ func Test_CookieBinder_Bind(t *testing.T) {
type User struct {
Name string `form:"name"`
Names []string `form:"names"`
Posts []Post `form:"posts"`
Age int `form:"age"`

Posts []Post `form:"posts"`
}
var user User

Expand Down Expand Up @@ -56,10 +55,9 @@ func Benchmark_CookieBinder_Bind(b *testing.B) {
}

type User struct {
Name string `query:"name"`
Age int `query:"age"`

Name string `query:"name"`
Posts []string `query:"posts"`
Age int `query:"age"`
}
var user User

Expand All @@ -71,10 +69,12 @@ func Benchmark_CookieBinder_Bind(b *testing.B) {

b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
_ = binder.Bind(req, &user)
err = binder.Bind(req, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Len(b, user.Posts, 3)
Expand Down
36 changes: 11 additions & 25 deletions binder/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package binder

import (
"bytes"
"fmt"
"mime/multipart"
"testing"

Expand All @@ -25,9 +24,8 @@ func Test_FormBinder_Bind(t *testing.T) {
type User struct {
Name string `form:"name"`
Names []string `form:"names"`
Posts []Post `form:"posts"`
Age int `form:"age"`

Posts []Post `form:"posts"`
}
var user User

Expand Down Expand Up @@ -61,10 +59,9 @@ func Benchmark_FormBinder_Bind(b *testing.B) {
}

type User struct {
Name string `query:"name"`
Age int `query:"age"`

Name string `query:"name"`
Posts []string `query:"posts"`
Age int `query:"age"`
}
var user User

Expand All @@ -74,10 +71,12 @@ func Benchmark_FormBinder_Bind(b *testing.B) {

b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
_ = binder.Bind(req, &user)
err = binder.Bind(req, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Len(b, user.Posts, 3)
Expand All @@ -91,16 +90,10 @@ func Test_FormBinder_BindMultipart(t *testing.T) {
}
require.Equal(t, "form", b.Name())

type Post struct {
Title string `form:"title"`
}

type User struct {
Name string `form:"name"`
Names []string `form:"names"`
Age int `form:"age"`

Posts []Post `form:"posts"`
}
var user User

Expand All @@ -113,9 +106,6 @@ func Test_FormBinder_BindMultipart(t *testing.T) {
require.NoError(t, mw.WriteField("names", "john"))
require.NoError(t, mw.WriteField("names", "doe"))
require.NoError(t, mw.WriteField("age", "42"))
// require.NoError(t, mw.WriteField("posts[0][title]", "post1"))
// require.NoError(t, mw.WriteField("posts[1][title]", "post2"))
// require.NoError(t, mw.WriteField("posts[2][title]", "post3"))
require.NoError(t, mw.Close())

req.Header.SetContentType(mw.FormDataContentType())
Expand All @@ -126,15 +116,10 @@ func Test_FormBinder_BindMultipart(t *testing.T) {
})

err := b.BindMultipart(req, &user)
fmt.Print(user)

require.NoError(t, err)
require.Equal(t, "john", user.Name)
require.Equal(t, 42, user.Age)
// require.Len(t, user.Posts, 3)
// require.Equal(t, "post1", user.Posts[0].Title)
// require.Equal(t, "post2", user.Posts[1].Title)
// require.Equal(t, "post3", user.Posts[2].Title)
require.Contains(t, user.Names, "john")
require.Contains(t, user.Names, "doe")
}
Expand All @@ -148,10 +133,9 @@ func Benchmark_FormBinder_BindMultipart(b *testing.B) {
}

type User struct {
Name string `query:"name"`
Age int `query:"age"`

Name string `query:"name"`
Posts []string `query:"posts"`
Age int `query:"age"`
}
var user User

Expand All @@ -172,10 +156,12 @@ func Benchmark_FormBinder_BindMultipart(b *testing.B) {

b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
_ = binder.BindMultipart(req, &user)
err = binder.BindMultipart(req, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Len(b, user.Posts, 3)
Expand Down
27 changes: 17 additions & 10 deletions binder/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ func Test_HeaderBinder_Bind(t *testing.T) {
require.Equal(t, "header", b.Name())

type User struct {
Name string `header:"name"`
Names []string `header:"names"`
Age int `header:"age"`

Posts []string `header:"posts"`
Name string `header:"Name"`
Names []string `header:"Names"`
Posts []string `header:"Posts"`
Age int `header:"Age"`
}
var user User

Expand Down Expand Up @@ -56,10 +55,9 @@ func Benchmark_HeaderBinder_Bind(b *testing.B) {
}

type User struct {
Name string `query:"name"`
Age int `query:"age"`

Posts []string `query:"posts"`
Name string `header:"Name"`
Posts []string `header:"Posts"`
Age int `header:"Age"`
}
var user User

Expand All @@ -68,7 +66,16 @@ func Benchmark_HeaderBinder_Bind(b *testing.B) {
req.Header.Set("age", "42")
req.Header.Set("posts", "post1,post2,post3")

var err error
for i := 0; i < b.N; i++ {
_ = binder.Bind(req, &user)
err = binder.Bind(req, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Len(b, user.Posts, 3)
require.Contains(b, user.Posts, "post1")
require.Contains(b, user.Posts, "post2")
require.Contains(b, user.Posts, "post3")
}
10 changes: 4 additions & 6 deletions binder/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ func Test_JSON_Binding_Bind(t *testing.T) {
}

type User struct {
Name string `json:"name"`
Age int `json:"age"`

Name string `json:"name"`
Posts []Post `json:"posts"`
Age int `json:"age"`
}
var user User

Expand All @@ -46,10 +45,9 @@ func Benchmark_JSON_Binding_Bind(b *testing.B) {
}

type User struct {
Name string `json:"name"`
Age int `json:"age"`

Name string `json:"name"`
Posts []string `json:"posts"`
Age int `json:"age"`
}

var user User
Expand Down
6 changes: 2 additions & 4 deletions binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func parseToStruct(aliasTag string, out any, data map[string][]string) error {
// Parse data into the map
// thanks to https://github.com/gin-gonic/gin/blob/master/binding/binding.go
func parseToMap(ptr any, data map[string][]string) error {
fmt.Print(ptr)
elem := reflect.TypeOf(ptr).Elem()
fmt.Print(elem.Kind())

//nolint:exhaustive // it's not necessary to check all types
switch elem.Kind() {
case reflect.Slice:
newMap, ok := ptr.(map[string][]string)
Expand All @@ -124,8 +124,6 @@ func parseToMap(ptr any, data map[string][]string) error {
return ErrMapNotConvertable
}

fmt.Print(newMap)

for k, v := range data {
if len(v) == 0 {
newMap[k] = ""
Expand Down
6 changes: 2 additions & 4 deletions binder/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func Test_EqualFieldType(t *testing.T) {
require.True(t, equalFieldType(&user2, reflect.String, "user.Address"))
require.True(t, equalFieldType(&user2, reflect.Int, "user.AGE"))
require.True(t, equalFieldType(&user2, reflect.Int, "user.age"))

}

func Test_ParseParamSquareBrackets(t *testing.T) {
Expand Down Expand Up @@ -141,11 +140,10 @@ func Test_parseToMap(t *testing.T) {
require.Len(t, m2["key2"], 1)
require.Len(t, m2["key3"], 1)

// Test map[string]interface{}
m3 := make(map[string]interface{})
// Test map[string]any
m3 := make(map[string]any)
err = parseToMap(m3, inputMap)
require.ErrorIs(t, err, ErrMapNotConvertable)

}

func Test_FilterFlags(t *testing.T) {
Expand Down
19 changes: 13 additions & 6 deletions binder/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ func Test_QueryBinder_Bind(t *testing.T) {
type User struct {
Name string `query:"name"`
Names []string `query:"names"`
Posts []Post `query:"posts"`
Age int `query:"age"`

Posts []Post `query:"posts"`
}
var user User

Expand Down Expand Up @@ -57,17 +56,25 @@ func Benchmark_QueryBinder_Bind(b *testing.B) {
}

type User struct {
Name string `query:"name"`
Age int `query:"age"`

Name string `query:"name"`
Posts []string `query:"posts"`
Age int `query:"age"`
}
var user User

req := fasthttp.AcquireRequest()
req.URI().SetQueryString("name=john&age=42&posts=post1,post2,post3")

var err error
for i := 0; i < b.N; i++ {
_ = binder.Bind(req, &user)
err = binder.Bind(req, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Len(b, user.Posts, 3)
require.Contains(b, user.Posts, "post1")
require.Contains(b, user.Posts, "post2")
require.Contains(b, user.Posts, "post3")
}
18 changes: 9 additions & 9 deletions binder/resp_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ func Test_RespHeaderBinder_Bind(t *testing.T) {
require.Equal(t, "respHeader", b.Name())

type User struct {
Name string `respHeader:"name"`
Age int `respHeader:"age"`

Name string `respHeader:"name"`
Posts []string `respHeader:"posts"`
Age int `respHeader:"age"`
}
var user User

Expand All @@ -42,17 +41,15 @@ func Test_RespHeaderBinder_Bind(t *testing.T) {

func Benchmark_RespHeaderBinder_Bind(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

binder := &RespHeaderBinding{
EnableSplitting: true,
}

type User struct {
Name string `respHeader:"name"`
Age int `respHeader:"age"`

Name string `respHeader:"name"`
Posts []string `respHeader:"posts"`
Age int `respHeader:"age"`
}
var user User

Expand All @@ -65,11 +62,14 @@ func Benchmark_RespHeaderBinder_Bind(b *testing.B) {
fasthttp.ReleaseResponse(resp)
})

b.StartTimer()
b.ResetTimer()

var err error
for i := 0; i < b.N; i++ {
_ = binder.Bind(resp, &user)
err = binder.Bind(resp, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Equal(b, []string{"post1", "post2", "post3"}, user.Posts)
Expand Down
Loading

0 comments on commit 043dd20

Please sign in to comment.