-
Notifications
You must be signed in to change notification settings - Fork 8
fix: anonymous/inaccessible structs for commentstart #40
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
fix: anonymous/inaccessible structs for commentstart #40
Conversation
@@ -32,3 +46,13 @@ type CommentStartTestStruct struct { | |||
|
|||
// DoNothing is used to check that the analyser doesn't report on methods. | |||
func (CommentStartTestStruct) DoNothing() {} | |||
|
|||
type unexpectedStruct struct { |
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.
Unexported?
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.
Sorry.
That's right. Fixed at 3302613 !
ffe4dd5
to
3302613
Compare
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.
I think this is a good start to solving these issues. I am wondering if, in the future, we might need to look at using similar logic in other linters too. I'm thinking perhaps we will need to create a util
from what you've done here, but I think we can cross that bridge when we come to it rather than over-optimising now
Could you please review my comments and also rebase onto the latest main branch please
if decl.Tok != token.TYPE { | ||
return false |
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.
What does this do?
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.
We ignore non-type declarations like var
const
import
.
Let me add a comment here.
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.
Added
tagInfo := jsonTags.FieldTags(field) | ||
if tagInfo.Ignored { | ||
return false |
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.
Can you add a comment here please
return false | |
// Returning false here means we won't inspect the children of an ignored field. | |
return false |
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.
Added
if field == nil || len(field.Names) == 0 { | ||
return | ||
return false |
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.
Can you explain why this one is false?
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.
In Go, we cannot declare anonymous structs in structs without field names, so we can safely ignore fields without names.
type Struct struct {
struct { // ./prog.go:9:2: syntax error: unexpected keyword struct, expected field name or embedded type
NoComment string `json:"noComment"`
} `json:",inline"` // ./prog.go:11:4: syntax error: unexpected literal `json:",inline"` after top level declaration
}
https://go.dev/play/p/WSMdxhyTsa1
I also add a comment here.
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.
And I guess if you embed another struct, inlining it, then that struct has a name? Do we have a test case that represents this case?
type Foo struct {
Bar `json:",inline"
}
type Bar struct {
NoComment `json:"noComment"`
}
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.
Bar
has type
declarations itself, so it'll be checked separately from Foo
.
But I'll add a test of it for future changes.
Update: Added
@@ -34,3 +46,13 @@ type CommentStartTestStruct struct { | |||
|
|||
// DoNothing is used to check that the analyser doesn't report on methods. | |||
func (CommentStartTestStruct) DoNothing() {} | |||
|
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.
If you were to include a definition like below, do you think your logic would catch it? I suspect it may need a small tweak in checking the stack right? WDYT?
type {
Foo struct {
...
}
}
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.
It seems that inspect.WithStack
will traverse all structs even if we have multiple declarations in one type
, so it'll be checked.
Do you think we need a test for this?
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.
Yeah, could you anyway please, as in the future we may change the way the linters traverse to look only at objects accessible from the kubebuilder root annotation and I'd like to make sure we don't regress. Better to capture the case now while we are thinking about it than forget about it and regress in the future
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.
I got it! Thanks!
Sorry. I haven't added this test yet.
Let me prepare a PR to just add it.
8272af4
to
941dbf6
Compare
941dbf6
to
32c2184
Compare
related: kubernetes-sigs/cluster-api#11870 (comment)
depends on #39
json:"-"