Skip to content

Fix #1858: Add query params binding support for anonymous struct pointer field - #1861

Merged
aldas merged 2 commits into
labstack:masterfrom
pwli0755:master
May 8, 2021
Merged

aldas merged 2 commits into
labstack:masterfrom
pwli0755:master

Conversation

@pwli0755

Copy link
Copy Markdown
Contributor

try to fix #1858

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

@codecov

codecov Bot commented Apr 25, 2021 •

Copy link
Copy Markdown

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.

Comment thread bind.go Outdated
typeField := typ.Field(i)
structField := val.Field(i)
if typeField.Anonymous {
for structField.Kind() == reflect.Ptr {

Copy link
Copy Markdown
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?

Comment thread bind.go
}
structFieldKind := structField.Kind()
inputFieldName := typeField.Tag.Get(tag)
if typeField.Anonymous && structField.Kind() == reflect.Struct && inputFieldName != "" {

Copy link
Copy Markdown
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

@aldas aldas left a comment

Copy link
Copy Markdown
Contributor

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

Development

Successfully merging this pull request may close these issues.

echo.Bind not working while binding query params to a struct with another nested anonymous struct's pointer in it

2 participants