Skip to content

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

Merged
merged 3 commits into from
Feb 26, 2025

Conversation

tsuzu
Copy link
Contributor

@tsuzu tsuzu commented Feb 20, 2025

related: kubernetes-sigs/cluster-api#11870 (comment)

depends on #39

  • Don't check structs defined in functions
  • Don't check anonysmous structs with json:"-"
  • Check other anonysmous structs
  • Check unexported structs
    • Exported structs can depend on unexported structs

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unexported?

Copy link
Contributor Author

@tsuzu tsuzu Feb 21, 2025

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 !

@tsuzu tsuzu force-pushed the commentstart-anonymous-structs branch from ffe4dd5 to 3302613 Compare February 21, 2025 14:16
Copy link
Contributor

@JoelSpeed JoelSpeed left a 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

Comment on lines 64 to 66
if decl.Tok != token.TYPE {
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
Copy link
Contributor

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

Suggested change
return false
// Returning false here means we won't inspect the children of an ignored field.
return false

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

@tsuzu tsuzu Feb 25, 2025

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.

Copy link
Contributor

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"`
}

Copy link
Contributor Author

@tsuzu tsuzu Feb 25, 2025

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() {}

Copy link
Contributor

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 {
    ...
  }
}

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

@tsuzu tsuzu Feb 26, 2025

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.

@tsuzu tsuzu force-pushed the commentstart-anonymous-structs branch from 8272af4 to 941dbf6 Compare February 25, 2025 18:48
@tsuzu tsuzu force-pushed the commentstart-anonymous-structs branch from 941dbf6 to 32c2184 Compare February 25, 2025 18:50
@JoelSpeed JoelSpeed merged commit 1ad4d04 into kubernetes-sigs:main Feb 26, 2025
2 checks passed
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.

2 participants