-
Notifications
You must be signed in to change notification settings - Fork 9
add FieldName API #79
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
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,26 @@ func LookupTypeSpec(pass *analysis.Pass, ident *ast.Ident) (*ast.TypeSpec, bool) | |
return nil, false | ||
} | ||
|
||
// FieldName returns the name of the field. If the field has a name, it returns that name. | ||
// If the field is embedded and it can be converted to an identifier, it returns the name of the identifier. | ||
// If it doesn't have a name and can't be converted to an identifier, it returns an empty string. | ||
func FieldName(field *ast.Field) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the expected fieldName for pointer embedded fields? e.g.
Under current implementation, it would be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Thanks, fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we create some test cases for this function that demonstrate it's functionality? Make sure if we make updates here in the future we don't forget why we are handling StarExpr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added test cases |
||
if len(field.Names) > 0 && field.Names[0] != nil { | ||
return field.Names[0].Name | ||
} | ||
|
||
switch typ := field.Type.(type) { | ||
case *ast.Ident: | ||
return typ.Name | ||
case *ast.StarExpr: | ||
if ident, ok := typ.X.(*ast.Ident); ok { | ||
return ident.Name | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func getFilesForType(pass *analysis.Pass, ident *ast.Ident) (*token.File, *ast.File) { | ||
namedType, ok := pass.TypesInfo.TypeOf(ident).(*types.Named) | ||
if !ok { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package utils_test | ||
|
||
import ( | ||
"go/ast" | ||
"testing" | ||
|
||
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils" | ||
) | ||
|
||
func TestFieldName(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := map[string]struct { | ||
field *ast.Field | ||
want string | ||
}{ | ||
"field has Names": { | ||
field: &ast.Field{ | ||
Names: []*ast.Ident{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
want: "foo", | ||
}, | ||
"filed has no Names, but is an Ident": { | ||
field: &ast.Field{ | ||
Type: &ast.Ident{ | ||
Name: "foo", | ||
}, | ||
}, | ||
want: "foo", | ||
}, | ||
"field has no Names, but is a StarExpr with an Ident": { | ||
field: &ast.Field{ | ||
Type: &ast.StarExpr{ | ||
X: &ast.Ident{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
want: "foo", | ||
}, | ||
"field has no Names, and is not an Ident or StarExpr": { | ||
field: &ast.Field{ | ||
Type: &ast.ArrayType{ | ||
Elt: &ast.Ident{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
want: "", | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := utils.FieldName(tc.field) | ||
if got != tc.want { | ||
t.Errorf("got %q, want %q", got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use ginkgo? Since other tests are using ginkgo already. The example below would show how this would look
The actual TestUtils conventionally goes in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I rewrote the test case' |
Uh oh!
There was an error while loading. Please reload this page.