-
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 all 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,28 @@ | ||
/* | ||
Copyright 2025 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestValidation(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Utils") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2025 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package utils_test | ||
|
||
import ( | ||
"go/ast" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils" | ||
) | ||
|
||
var _ = Describe("FieldName", func() { | ||
type fieldNameInput struct { | ||
field *ast.Field | ||
want string | ||
} | ||
|
||
DescribeTable("Should extract the field name", func(in fieldNameInput) { | ||
Expect(utils.FieldName(in.field)).To(Equal(in.want), "expect to match the extracted field name") | ||
}, | ||
Entry("field has Names", fieldNameInput{ | ||
field: &ast.Field{ | ||
Names: []*ast.Ident{ | ||
{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
want: "foo", | ||
}), | ||
Entry("field has no Names, but is an Ident", fieldNameInput{ | ||
field: &ast.Field{ | ||
Type: &ast.Ident{ | ||
Name: "foo", | ||
}, | ||
}, | ||
want: "foo", | ||
}), | ||
Entry("field has no Names, but is a StarExpr with an Ident", fieldNameInput{ | ||
field: &ast.Field{ | ||
Type: &ast.StarExpr{ | ||
X: &ast.Ident{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
want: "foo", | ||
}), | ||
Entry("field has no Names, and is not an Ident or StarExpr", fieldNameInput{ | ||
field: &ast.Field{ | ||
Type: &ast.ArrayType{ | ||
Elt: &ast.Ident{ | ||
Name: "foo", | ||
}, | ||
}, | ||
}, | ||
want: "", | ||
}), | ||
) | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.