-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1858: Add query params binding support for anonymous struct pointer field #1861
Conversation
…uct pointer filed
Codecov Report
@@ Coverage Diff @@
## master #1861 +/- ##
==========================================
+ Coverage 89.57% 89.59% +0.02%
==========================================
Files 31 31
Lines 2686 2740 +54
==========================================
+ Hits 2406 2455 +49
- Misses 180 184 +4
- Partials 100 101 +1
Continue to review full report at Codecov.
|
bind.go
Outdated
@@ -144,11 +144,20 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri | |||
for i := 0; i < typ.NumField(); i++ { | |||
typeField := typ.Field(i) | |||
structField := val.Field(i) | |||
if typeField.Anonymous { | |||
for structField.Kind() == reflect.Ptr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you want probably want to use if
here instead of for
?
if !structField.CanSet() { | ||
continue | ||
} | ||
structFieldKind := structField.Kind() | ||
inputFieldName := typeField.Tag.Get(tag) | ||
if typeField.Anonymous && structField.Kind() == reflect.Struct && inputFieldName != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this case should return an error and not silently hide the error that developer has made.
Let's just enforce rule that
This is NOT valid/allowed:
result := struct {
*Bar `query:"bar"` // or param:"bar"
}{&Bar{}}
This is OK:
result := struct {
*Bar // or json:"bar" as json has its own implementation
}{&Bar{}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
try to fix #1858
if the anonymous struct filed is a pointer, try dig one step deeper and get the underlaying struct