Skip to content

Add custom structfield linter to check struct field names and tags - #3843

Merged
gmlewis merged 5 commits into
google:masterfrom
alexandear-org:chore/structfield-linter
Nov 26, 2025
Merged

Add custom structfield linter to check struct field names and tags#3843
gmlewis merged 5 commits into
google:masterfrom
alexandear-org:chore/structfield-linter

Conversation

@alexandear

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

typeListCursorOptionsstruct {
// For paginated result sets, page of results to retrieve.
Pagestring`url:"page,omitempty"`
// For paginated result sets, the number of results to include per page.
PerPageint`url:"per_page,omitempty"`

$ ./script/lint.shgithub/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
alexandearforce-pushed the chore/structfield-linter branch from fd07588 to e7e528bCompareNovember 24, 2025 16:54
@codecov

codecovBot commented Nov 24, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.42%. Comparing base (903265b) to head (e83e366).
⚠️ Report is 3 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.

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment threadtools/structfield/structfield.go Outdated
Comment thread.custom-gcl.yml Outdated
Comment threadtools/structfield/testdata/src/has-warnings/main.go
Comment threadtools/structfield/testdata/src/has-warnings/main.go

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @alexandear!
LGTM.
Merging.

@gmlewis
gmlewis merged commit 3afe183 into google:masterNov 26, 2025
7 checks passed
jlaportebot added a commit to jlaportebot/go-github that referenced this pull request Jun 28, 2026
Sign up for freeto 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.

2 participants

@alexandear@gmlewis