Skip to content

Conversation

@alexandear
Copy link
Contributor

This PR renames jsonfieldname linter to structfield and extends it to check struct tags.

The linter enforces that:

  • The tag names for json and url match the struct field name
  • Fields with json and url tags that include omitempty must use pointers types, except for slices, maps, json.RawMessage, and any.

Example output for the ListCursorOptions struct:

go-github/github/github.go

Lines 262 to 267 in 671b8b0

type ListCursorOptions struct {
// For paginated result sets, page of results to retrieve.
Page string `url:"page,omitempty"`
// For paginated result sets, the number of results to include per page.
PerPage int `url:"per_page,omitempty"`

$ ./script/lint.sh
github/github.go:264:7: change the "Page" field type to "*string" in the struct "ListCursorOptions" because its tag uses "omitempty" (structfield)
        Page string `url:"page,omitempty"`
             ^
github/github.go:267:10: change the "PerPage" field type to "*int" in the struct "ListCursorOptions" because its tag uses "omitempty" (structfield)
        PerPage int `url:"per_page,omitempty"`
                ^
...

Closes #3775

@alexandear alexandear force-pushed the chore/structfield-linter branch from fd07588 to e7e528b Compare November 24, 2025 16:54
@codecov
Copy link

codecov bot commented Nov 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.42%. Comparing base (903265b) to head (e83e366).
⚠️ Report is 2 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@gmlewis gmlewis left a 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"`
}
Copy link
Collaborator

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>

Copy link
Contributor Author

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@alexandear alexandear requested a review from gmlewis November 25, 2025 16:53
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"`
Copy link
Collaborator

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?

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.

linter for detect json:"omitempty" usage with value types

2 participants