Skip to content

Commit e6ff948

Browse files
committed
refactor: Improve Bind.All test and struct field ordering
- Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution.
1 parent 4799c50 commit e6ff948

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

bind.go

-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ func (b *Bind) All(out any) error {
299299
// TODO: Create WithOverrideEmptyValues
300300
// Bind from each source, but only update unset fields
301301
for _, bindFunc := range sources {
302-
303302
if err := bindFunc(tempStruct); err != nil {
304303
return err
305304
}

bind_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -1889,11 +1889,11 @@ func Test_Bind_RepeatParserWithSameStruct(t *testing.T) {
18891889
}
18901890

18911891
type RequestConfig struct {
1892-
ContentType string
1893-
Body []byte
18941892
Headers map[string]string
18951893
Cookies map[string]string
1894+
ContentType string
18961895
Query string
1896+
Body []byte
18971897
}
18981898

18991899
func (rc *RequestConfig) ApplyTo(ctx Ctx) {
@@ -1919,12 +1919,12 @@ func (rc *RequestConfig) ApplyTo(ctx Ctx) {
19191919
func Test_Bind_All(t *testing.T) {
19201920
t.Parallel()
19211921
type User struct {
1922-
ID int `param:"id" query:"id" json:"id" form:"id"`
19231922
Avatar *multipart.FileHeader `form:"avatar"`
19241923
Name string `query:"name" json:"name" form:"name"`
19251924
Email string `json:"email" form:"email"`
1926-
Role string `header:"x-user-role"`
1925+
Role string `header:"X-User-Role"`
19271926
SessionID string `json:"session_id" cookie:"session_id"`
1927+
ID int `param:"id" query:"id" json:"id" form:"id"`
19281928
}
19291929
newBind := func(app *App) *Bind {
19301930
return &Bind{
@@ -1937,7 +1937,7 @@ func Test_Bind_All(t *testing.T) {
19371937
ContentType: MIMEApplicationJSON,
19381938
Body: []byte(`{"name":"john", "email": "[email protected]", "session_id": "abc1234", "id": 1}`),
19391939
Headers: map[string]string{
1940-
"x-user-role": "admin",
1940+
"X-User-Role": "admin",
19411941
},
19421942
Cookies: map[string]string{
19431943
"session_id": "abc123",
@@ -1947,10 +1947,10 @@ func Test_Bind_All(t *testing.T) {
19471947
}
19481948

19491949
tests := []struct {
1950-
name string
19511950
out any
19521951
expected *User
19531952
config *RequestConfig
1953+
name string
19541954
wantErr bool
19551955
}{
19561956
{
@@ -2013,6 +2013,7 @@ func Test_Bind_All(t *testing.T) {
20132013

20142014
for _, tt := range tests {
20152015
t.Run(tt.name, func(t *testing.T) {
2016+
t.Parallel()
20162017
bind := newBind(app)
20172018

20182019
if tt.config != nil {
@@ -2024,7 +2025,7 @@ func Test_Bind_All(t *testing.T) {
20242025
assert.Error(t, err)
20252026
return
20262027
}
2027-
assert.NoError(t, err)
2028+
require.NoError(t, err)
20282029

20292030
if tt.expected != nil {
20302031
actual, ok := tt.out.(*User)
@@ -2044,9 +2045,9 @@ func Test_Bind_All(t *testing.T) {
20442045
func Test_Bind_All_Uri_Precedence(t *testing.T) {
20452046
t.Parallel()
20462047
type User struct {
2047-
ID int `param:"id" json:"id" query:"id" form:"id"`
20482048
Name string `json:"name"`
20492049
Email string `json:"email"`
2050+
ID int `param:"id" json:"id" query:"id" form:"id"`
20502051
}
20512052

20522053
app := New()
@@ -2074,12 +2075,11 @@ func Test_Bind_All_Uri_Precedence(t *testing.T) {
20742075
// go test -v -run=^$ -bench=Benchmark_Bind_All -benchmem -count=4
20752076
func BenchmarkBind_All(b *testing.B) {
20762077
type User struct {
2077-
ID int `param:"id" query:"id" json:"id" form:"id"`
2078-
Avatar *multipart.FileHeader `form:"avatar"`
2079-
Name string `query:"name" json:"name" form:"name"`
2080-
Email string `json:"email" form:"email"`
2081-
Role string `header:"x-user-role"`
2082-
SessionID string `json:"session_id" cookie:"session_id"`
2078+
SessionID string `json:"session_id" cookie:"session_id"`
2079+
Name string `query:"name" json:"name" form:"name"`
2080+
Email string `json:"email" form:"email"`
2081+
Role string `header:"X-User-Role"`
2082+
ID int `param:"id" query:"id" json:"id" form:"id"`
20832083
}
20842084

20852085
app := New()
@@ -2089,7 +2089,7 @@ func BenchmarkBind_All(b *testing.B) {
20892089
ContentType: MIMEApplicationJSON,
20902090
Body: []byte(`{"name":"john", "email": "[email protected]", "session_id": "abc1234", "id": 1}`),
20912091
Headers: map[string]string{
2092-
"x-user-role": "admin",
2092+
"X-User-Role": "admin",
20932093
},
20942094
Cookies: map[string]string{
20952095
"session_id": "abc123",

0 commit comments

Comments
 (0)