Skip to content

Commit

Permalink
explicitly return an error instead of hiding it
Browse files Browse the repository at this point in the history
  • Loading branch information
lipengwei authored and aldas committed May 8, 2021
1 parent 18d7fe1 commit 1aef300
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 3 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
typeField := typ.Field(i)
structField := val.Field(i)
if typeField.Anonymous {
for structField.Kind() == reflect.Ptr {
if structField.Kind() == reflect.Ptr {
structField = structField.Elem()
}
}
Expand All @@ -155,8 +155,8 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
structFieldKind := structField.Kind()
inputFieldName := typeField.Tag.Get(tag)
if typeField.Anonymous && structField.Kind() == reflect.Struct && inputFieldName != "" {
// if anonymous struct, ignore custom tag
inputFieldName = ""
// if anonymous struct with query/param/form tags, report an error
return errors.New("query/param/form tags are not allowed with anonymous struct field")
}

if inputFieldName == "" {
Expand Down
4 changes: 1 addition & 3 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,7 @@ func TestBindUnmarshalParamAnonymousFieldPtrCustomTag(t *testing.T) {
*Bar `json:"bar" query:"bar"`
}{&Bar{}}
err := c.Bind(&result)
if assert.NoError(t, err) {
assert.Equal(t, 1, result.Baz)
}
assert.Contains(t, err.Error(), "query/param/form tags are not allowed with anonymous struct field")
}

func TestBindUnmarshalTextPtr(t *testing.T) {
Expand Down

0 comments on commit 1aef300

Please sign in to comment.