Skip to content
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

Merged
merged 2 commits into from
May 8, 2021

Conversation

pwli0755
Copy link
Contributor

try to fix #1858

if the anonymous struct filed is a pointer, try dig one step deeper and get the underlaying struct

@codecov
Copy link

codecov bot commented Apr 25, 2021

Codecov Report

Merging #1861 (2d930a9) into master (a4ab482) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
bind.go 89.22% <100.00%> (+0.33%) ⬆️
router.go 95.03% <0.00%> (-1.02%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3b07058...2d930a9. Read the comment docs.

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 {
Copy link
Contributor

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 != "" {
Copy link
Contributor

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{}}

@pwli0755 pwli0755 requested a review from aldas April 30, 2021 04:36
Copy link
Contributor

@aldas aldas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aldas aldas merged commit 1aef300 into labstack:master May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants