Skip to content

Fix message for imported embedded structs #50

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 1 commit into from
Mar 5, 2025

Conversation

tsuzu
Copy link
Contributor

@tsuzu tsuzu commented Mar 4, 2025

Using embedded structs from external packages, I got an error as below:

field  is missing godoc comment

@@ -24,6 +26,8 @@ type CommentStartTestStruct struct {

A `json:"a"` // want "field A is missing godoc comment"

pkg.Embedded `json:"embedded"` // want "field Embedded is missing godoc comment"
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 name this as well, ie, if we use a named type from another package, does the name also come up?

Copy link
Contributor Author

@tsuzu tsuzu Mar 4, 2025

Choose a reason for hiding this comment

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

Excuse me. Do you intend fields with names like below?

NoComment pkg.Struct `json:"noComment"`

@@ -51,6 +51,8 @@ func checkField(pass *analysis.Pass, field *ast.Field, tagInfo extractjsontags.F
fieldName = field.Names[0].Name
} else if ident, ok := field.Type.(*ast.Ident); ok {
fieldName = ident.Name
} else if selector, ok := field.Type.(*ast.SelectorExpr); ok {
fieldName = selector.Sel.Name
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels like it's going to be useful to many of the analyzers, probably want a helper at some point

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 this still can't handle pointer types like *Type. Let me check it.
If the logic gets complex, it would be good to prepare a helper now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

types.ExprString may work for this! Very useful function.
https://pkg.go.dev/go/types#ExprString

@tsuzu tsuzu force-pushed the fix-imported-embedded branch from 8fe1f67 to 240b5cc Compare March 4, 2025 18:52
@tsuzu
Copy link
Contributor Author

tsuzu commented Mar 4, 2025

Fixing an issue that golden files don't work in a sub-package a/pkg.

} else if ident, ok := field.Type.(*ast.Ident); ok {
fieldName = ident.Name
} else {
fieldName = types.ExprString(field.Type)
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh well that's a lot easier!

@JoelSpeed JoelSpeed merged commit abd233a into kubernetes-sigs:main Mar 5, 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