-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add exhaustruct linter
#2667
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89f665c
feat: add `exhaustruct` linter
xobotyi 47ec81a
review: tests
ldez fa6858d
review: fix go.mod and go.sum
ldez 91ee4cd
feat: update go-exhaustruct to v2 and adapt to API changes
xobotyi 9006f56
review
ldez d029d10
review: error management
ldez 83571a2
review: update linter
ldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: add
exhaustruct linter
This linter can be called a successor of `exhaustivestruct`, and: - it is at least **2.5+ times faster**, due to better algorithm; - can receive `include` and/or `exclude` patterns; - expects received patterns to be RegExp, therefore this package is not api-compatible with `exhaustivestruct`. Also: deprecate `exhaustivestruct` linter
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package golinters | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/GaijinEntertainment/go-exhaustruct/pkg/analyzer" | ||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/golangci/golangci-lint/pkg/config" | ||
| "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
| ) | ||
|
|
||
| func NewExhaustruct(settings *config.ExhaustructSettings) *goanalysis.Linter { | ||
| a := analyzer.Analyzer | ||
|
|
||
| var cfg map[string]map[string]interface{} | ||
| if settings != nil { | ||
| cfg = map[string]map[string]interface{}{ | ||
| a.Name: { | ||
| "include": strings.Join(settings.Include, ","), | ||
| "exclude": strings.Join(settings.Exclude, ","), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| return goanalysis.NewLinter( | ||
| a.Name, | ||
| a.Doc, | ||
| []*analysis.Analyzer{a}, | ||
| cfg, | ||
| ).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //args: -Eexhaustruct | ||
| package testdata | ||
|
|
||
| import "time" | ||
|
|
||
| type Test struct { | ||
| A string | ||
| B int | ||
| c bool // private field inside the same package are not ignored | ||
| D float64 | ||
| E time.Time | ||
| } | ||
|
|
||
| var pass = Test{ | ||
| A: "a", | ||
| B: 0, | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failPrivate = Test{ // ERROR "c is missing in Test" | ||
| A: "a", | ||
| B: 0, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var fail = Test{ // ERROR "B is missing in Test" | ||
| A: "a", | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failMultiple = Test{ // ERROR "B, D are missing in Test" | ||
| A: "a", | ||
| c: false, | ||
| E: time.Now(), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| //args: -Eexhaustruct | ||
| //config: linters-settings.exhaustruct.include=.*\.Test1$ | ||
| //config: linters-settings.exhaustruct.exclude=.*\.Test3$ | ||
| package testdata | ||
|
|
||
| import "time" | ||
|
|
||
| type Test1 struct { | ||
| A string | ||
| B int | ||
| c bool // private field inside the same package are not ignored | ||
| D float64 | ||
| E time.Time | ||
| } | ||
|
|
||
| var passTest1 = Test1{ | ||
| A: "a", | ||
| B: 0, | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failTest1 = Test1{ // ERROR "B is missing in Test" | ||
| A: "a", | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failMultipleTest1 = Test1{ // ERROR "B, D are missing in Test" | ||
| A: "a", | ||
| c: false, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failPrivateTest1 = Test1{ // ERROR "c is missing in Test" | ||
| A: "a", | ||
| B: 0, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| type Test2 struct { | ||
| A string | ||
| B int | ||
| c bool // private field inside the same package are not ignored | ||
| D float64 | ||
| E time.Time | ||
| } | ||
|
|
||
| var passTest2 = Test1{ | ||
| A: "a", | ||
| B: 0, | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failTest2 = Test2{ | ||
| A: "a", | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failMultipleTest2 = Test2{ | ||
| A: "a", | ||
| c: false, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failPrivateTest2 = Test2{ | ||
| A: "a", | ||
| B: 0, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| type Test3 struct { | ||
| A string | ||
| B int | ||
| c bool // private field inside the same package are not ignored | ||
| D float64 | ||
| E time.Time | ||
| } | ||
|
|
||
| var passTest3 = Test3{ | ||
| A: "a", | ||
| B: 0, | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failTest3 = Test3{ | ||
| A: "a", | ||
| c: false, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failMultipleTest3 = Test3{ | ||
| A: "a", | ||
| c: false, | ||
| E: time.Now(), | ||
| } | ||
|
|
||
| var failPrivateTest3 = Test3{ | ||
| A: "a", | ||
| B: 0, | ||
| D: 1.0, | ||
| E: time.Now(), | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.