Skip to content

Commit

Permalink
fix: BindURLValuesAndFiles supports the anonymous field
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Sep 13, 2022
1 parent b9b6f0a commit f7e6076
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions binder/binder_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func bindURLValues(val reflect.Value, files map[string][]*multipart.FileHeader,

fieldValue := val.Field(i)
fieldKind := fieldValue.Kind()
if !fieldValue.CanSet() {
if field.Anonymous && fieldKind == reflect.Struct {
if err = bindURLValues(fieldValue, files, data, tag); err != nil {
return err
}
if field.Anonymous && fieldKind == reflect.Struct {
if err = bindURLValues(fieldValue, files, data, tag); err != nil {
return err
}
continue
} else if !fieldValue.CanSet() {
continue
}

inputValue, exists := data[fieldName]
Expand Down
11 changes: 11 additions & 0 deletions binder/binder_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (b *paramBinder) UnmarshalBind(param string) error {
return err
}

type AnonymousString string
type AnonymousStruct struct {
Embed string `query:"Embed"`
}

func TestBindURLValues(t *testing.T) {
type T struct {
Bool bool `query:"bool"`
Expand All @@ -82,6 +87,8 @@ func TestBindURLValues(t *testing.T) {

BindUnmarshaler `query:"anonymous1"`
paramBinder `query:"anonymous2"`
AnonymousStruct `query:"anonymous3"`
AnonymousString `query:"anonymous4"`

Ingore int `query:"-"`
Ptr *int
Expand Down Expand Up @@ -110,6 +117,8 @@ func TestBindURLValues(t *testing.T) {
"interface1": []string{"41"},
"interface2": []string{"42"},
"anonymous1": []string{"43"},
"anonymous4": []string{"44"},
"Embed": []string{"45"},

"Ptr": []string{"51"},
"Value": []string{"51"},
Expand Down Expand Up @@ -140,6 +149,8 @@ func TestBindURLValues(t *testing.T) {
Interface1: paramBinder{41},
Interface2: &paramBinder{42},
BindUnmarshaler: &paramBinder{43},
AnonymousString: "44",
AnonymousStruct: AnonymousStruct{Embed: "45"},

paramBinder: paramBinder{int1},
Ptr: &int1,
Expand Down

0 comments on commit f7e6076

Please sign in to comment.