Skip to content

Commit f7e6076

Browse files
committed
fix: BindURLValuesAndFiles supports the anonymous field
1 parent b9b6f0a commit f7e6076

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

binder/binder_values.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ func bindURLValues(val reflect.Value, files map[string][]*multipart.FileHeader,
9292

9393
fieldValue := val.Field(i)
9494
fieldKind := fieldValue.Kind()
95-
if !fieldValue.CanSet() {
96-
if field.Anonymous && fieldKind == reflect.Struct {
97-
if err = bindURLValues(fieldValue, files, data, tag); err != nil {
98-
return err
99-
}
95+
if field.Anonymous && fieldKind == reflect.Struct {
96+
if err = bindURLValues(fieldValue, files, data, tag); err != nil {
97+
return err
10098
}
10199
continue
100+
} else if !fieldValue.CanSet() {
101+
continue
102102
}
103103

104104
inputValue, exists := data[fieldName]

binder/binder_values_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (b *paramBinder) UnmarshalBind(param string) error {
5959
return err
6060
}
6161

62+
type AnonymousString string
63+
type AnonymousStruct struct {
64+
Embed string `query:"Embed"`
65+
}
66+
6267
func TestBindURLValues(t *testing.T) {
6368
type T struct {
6469
Bool bool `query:"bool"`
@@ -82,6 +87,8 @@ func TestBindURLValues(t *testing.T) {
8287

8388
BindUnmarshaler `query:"anonymous1"`
8489
paramBinder `query:"anonymous2"`
90+
AnonymousStruct `query:"anonymous3"`
91+
AnonymousString `query:"anonymous4"`
8592

8693
Ingore int `query:"-"`
8794
Ptr *int
@@ -110,6 +117,8 @@ func TestBindURLValues(t *testing.T) {
110117
"interface1": []string{"41"},
111118
"interface2": []string{"42"},
112119
"anonymous1": []string{"43"},
120+
"anonymous4": []string{"44"},
121+
"Embed": []string{"45"},
113122

114123
"Ptr": []string{"51"},
115124
"Value": []string{"51"},
@@ -140,6 +149,8 @@ func TestBindURLValues(t *testing.T) {
140149
Interface1: paramBinder{41},
141150
Interface2: &paramBinder{42},
142151
BindUnmarshaler: &paramBinder{43},
152+
AnonymousString: "44",
153+
AnonymousStruct: AnonymousStruct{Embed: "45"},
143154

144155
paramBinder: paramBinder{int1},
145156
Ptr: &int1,

0 commit comments

Comments
 (0)