-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add custom structfield linter to check struct field names and tags #3843
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
base: master
Are you sure you want to change the base?
Conversation
3660e2f to
fd07588
Compare
fd07588 to
e7e528b
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3843 +/- ##
=======================================
Coverage 92.41% 92.42%
=======================================
Files 197 197
Lines 14152 14167 +15
=======================================
+ Hits 13079 13094 +15
Misses 884 884
Partials 189 189 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
gmlewis
left a comment
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.
Cool! Thank you, @alexandear!
Just a couple questions, really.
| Page string `url:"page,omitempty"` // want `change the "Page" field type to "\*string" in the struct "JSONFieldType" because its tag uses "omitempty"` | ||
| PerPage int `url:"per_page,omitempty"` // want `change the "PerPage" field type to "\*int" in the struct "JSONFieldType" because its tag uses "omitempty"` | ||
| Participating bool `url:"participating,omitempty"` // want `change the "Participating" field type to "\*bool" in the struct "JSONFieldType" because its tag uses "omitempty"` | ||
| } |
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.
Does this linter also handle the following cases? If so, could you please demonstrate them in this file?
*[]string=>[]string*[]anythingReally=>[]anythingReally*map[one]two=>map[one]two[]*string=>[]string[]*int<64>=>[]int<64>
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.
Thanks for review. Added these cases, please check.
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.
So I think I confused myself with the sliceofpointers linter with the above comment.
Does this linter now have all the functionality of the sliceofpointers linter, and do we need it anymore now that this one is updated?
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.
structfield analyzes only struct fields, while sliceofpointers checks variables. We could combine both linters, but we’d need to come up with a better, more general name for the merged version.
| PointerToSliceOfStructs *[]Struct `json:"pointer_to_slice_of_structs,omitempty"` // want `change the "PointerToSliceOfStructs" field type to "\[\]\*Struct" in the struct "JSONFieldType"` | ||
| PointerToSliceOfPointerStructs *[]*Struct `json:"pointer_to_slice_of_pointer_structs,omitempty"` // want `change the "PointerToSliceOfPointerStructs" field type to "\[\]\*Struct" in the struct "JSONFieldType"` | ||
| PointerToMap *map[string]string `json:"pointer_to_map,omitempty"` // want `change the "PointerToMap" field type to "map\[string\]string" in the struct "JSONFieldType"` | ||
| SliceOfInts []*int `json:"slice_of_ints,omitempty"` // want `change the "SliceOfInts" field type to "\[\]int" in the struct "JSONFieldType"` |
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.
Could you please also demonstrate that []*int64 and []*bool and []*float64 also get flagged properly since those are probably going to be the most common after int?
This PR renames
jsonfieldnamelinter tostructfieldand extends it to check struct tags.The linter enforces that:
jsonandurlmatch the struct field namejsonandurltags that includeomitemptymust use pointers types, except for slices, maps,json.RawMessage, andany.Example output for the
ListCursorOptionsstruct:go-github/github/github.go
Lines 262 to 267 in 671b8b0
Closes #3775