Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 1 addition & 44 deletions pkg/analysis/helpers/inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"go/ast"
"go/token"
"go/types"
"slices"

astinspector "golang.org/x/tools/go/ast/inspector"
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
Expand Down Expand Up @@ -102,7 +101,7 @@ func (i *inspector) inspectFields(inspectField func(field *ast.Field, jsonTagInf
// The 0th node in the stack is the *ast.File.
file, ok := stack[0].(*ast.File)
if ok {
structName = getStructName(file, field)
structName = utils.GetStructNameFromFile(file, field)
}

if structName != "" {
Expand Down Expand Up @@ -202,45 +201,3 @@ func printDebugInfo(field *ast.Field) string {

return debug
}

func getStructName(file *ast.File, field *ast.Field) string {
var (
structName string
found bool
)

ast.Inspect(file, func(n ast.Node) bool {
if found {
return false
}

typeSpec, ok := n.(*ast.TypeSpec)
if !ok {
return true
}

structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
return true
}

structName = typeSpec.Name.Name

if structType.Fields == nil {
return true
}

if slices.Contains(structType.Fields.List, field) {
found = true
return false
}

return true
})

if found {
return structName
}

return ""
}
58 changes: 29 additions & 29 deletions pkg/analysis/integers/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,39 @@ type Integers struct {

ValidInt64Ptr *int64

InvalidInt int // want "field InvalidInt should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidInt int // want "field Integers.InvalidInt should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidIntPtr *int // want "field InvalidIntPtr pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidIntPtr *int // want "field Integers.InvalidIntPtr pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidInt8 int8 // want "field InvalidInt8 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidInt8 int8 // want "field Integers.InvalidInt8 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidInt16 int16 // want "field InvalidInt16 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidInt16 int16 // want "field Integers.InvalidInt16 should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidUInt uint // want "field InvalidUInt should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUInt uint // want "field Integers.InvalidUInt should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidUIntPtr uint // want "field InvalidUIntPtr should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUIntPtr uint // want "field Integers.InvalidUIntPtr should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidUInt8 uint8 // want "field InvalidUInt8 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUInt8 uint8 // want "field Integers.InvalidUInt8 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidUInt16 uint16 // want "field InvalidUInt16 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUInt16 uint16 // want "field Integers.InvalidUInt16 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidUInt32 uint32 // want "field InvalidUInt32 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUInt32 uint32 // want "field Integers.InvalidUInt32 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidUInt64 uint64 // want "field InvalidUInt64 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUInt64 uint64 // want "field Integers.InvalidUInt64 should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

ValidInt32Alias ValidInt32Alias

ValidInt32AliasPtr *ValidInt32Alias

InvalidIntAlias InvalidIntAlias // want "field InvalidIntAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidIntAlias InvalidIntAlias // want "field Integers.InvalidIntAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidIntAliasPtr *InvalidIntAlias // want "field InvalidIntAliasPtr pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidIntAliasPtr *InvalidIntAlias // want "field Integers.InvalidIntAliasPtr pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidUIntAlias InvalidUIntAlias // want "field InvalidUIntAlias type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUIntAlias InvalidUIntAlias // want "field Integers.InvalidUIntAlias type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidUIntAliasPtr *InvalidUIntAlias // want "field InvalidUIntAliasPtr pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidUIntAliasPtr *InvalidUIntAlias // want "field Integers.InvalidUIntAliasPtr pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidIntAliasAlias InvalidIntAliasAlias // want "field InvalidIntAliasAlias type InvalidIntAliasAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidIntAliasAlias InvalidIntAliasAlias // want "field Integers.InvalidIntAliasAlias type InvalidIntAliasAlias type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

ValidSliceInt32 []int32

Expand All @@ -55,41 +55,41 @@ type Integers struct {

ValidSliceInt64Ptr []*int64

InvalidSliceInt []int // want "field InvalidSliceInt array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidSliceInt []int // want "field Integers.InvalidSliceInt array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidSliceIntPtr []*int // want "field InvalidSliceIntPtr array element pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidSliceIntPtr []*int // want "field Integers.InvalidSliceIntPtr array element pointer should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidSliceUInt []uint // want "field InvalidSliceUInt array element should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidSliceUInt []uint // want "field Integers.InvalidSliceUInt array element should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidSliceUIntPtr []*uint // want "field InvalidSliceUIntPtr array element pointer should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidSliceUIntPtr []*uint // want "field Integers.InvalidSliceUIntPtr array element pointer should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidSliceIntAlias []InvalidIntAlias // want "field InvalidSliceIntAlias array element type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidSliceIntAlias []InvalidIntAlias // want "field Integers.InvalidSliceIntAlias array element type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidSliceIntAliasPtr []*InvalidIntAlias // want "field InvalidSliceIntAliasPtr array element pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidSliceIntAliasPtr []*InvalidIntAlias // want "field Integers.InvalidSliceIntAliasPtr array element pointer type InvalidIntAlias should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidSliceUIntAlias []InvalidUIntAlias // want "field InvalidSliceUIntAlias array element type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidSliceUIntAlias []InvalidUIntAlias // want "field Integers.InvalidSliceUIntAlias array element type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidSliceUIntAliasPtr []*InvalidUIntAlias // want "field InvalidSliceUIntAliasPtr array element pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidSliceUIntAliasPtr []*InvalidUIntAlias // want "field Integers.InvalidSliceUIntAliasPtr array element pointer type InvalidUIntAlias should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

ValidMapStringToInt32 map[string]int32

ValidMapStringToInt64 map[string]int64

InvalidMapStringToInt map[string]int // want "field InvalidMapStringToInt map value should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidMapStringToInt map[string]int // want "field Integers.InvalidMapStringToInt map value should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidMapStringToUInt map[string]uint // want "field InvalidMapStringToUInt map value should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidMapStringToUInt map[string]uint // want "field Integers.InvalidMapStringToUInt map value should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

ValidMapInt32ToString map[int32]string

ValidMapInt64ToString map[int64]string

InvalidMapIntToString map[int]string // want "field InvalidMapIntToString map key should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidMapIntToString map[int]string // want "field Integers.InvalidMapIntToString map key should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidMapUIntToString map[uint]string // want "field InvalidMapUIntToString map key should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"
InvalidMapUIntToString map[uint]string // want "field Integers.InvalidMapUIntToString map key should not use unsigned integers, use only int32 or int64 and apply validation to ensure the value is positive"

InvalidIntFromAnotherFile IntB // want "field InvalidIntFromAnotherFile type IntB should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidIntFromAnotherFile IntB // want "field Integers.InvalidIntFromAnotherFile type IntB should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"

InvalidSliceIntAliasFromAnotherFile InvalidSliceIntAliasB // want "field InvalidSliceIntAliasFromAnotherFile type InvalidSliceIntAliasB array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
InvalidSliceIntAliasFromAnotherFile InvalidSliceIntAliasB // want "field Integers.InvalidSliceIntAliasFromAnotherFile type InvalidSliceIntAliasB array element should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
}

// DoNothing is used to check that the analyser doesn't report on methods.
Expand Down
28 changes: 14 additions & 14 deletions pkg/analysis/nobools/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ type Bools struct {

ValidInt64 int64

InvalidBool bool // want "field InvalidBool should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBool bool // want "field Bools.InvalidBool should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolPtr *bool // want "field InvalidBoolPtr pointer should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolPtr *bool // want "field Bools.InvalidBoolPtr pointer should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolSlice []bool // want "field InvalidBoolSlice array element should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolSlice []bool // want "field Bools.InvalidBoolSlice array element should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolPtrSlice []*bool // want "field InvalidBoolPtrSlice array element pointer should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolPtrSlice []*bool // want "field Bools.InvalidBoolPtrSlice array element pointer should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolAlias BoolAlias // want "field InvalidBoolAlias type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolAlias BoolAlias // want "field Bools.InvalidBoolAlias type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolPtrAlias *BoolAlias // want "field InvalidBoolPtrAlias pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolPtrAlias *BoolAlias // want "field Bools.InvalidBoolPtrAlias pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolSliceAlias []BoolAlias // want "field InvalidBoolSliceAlias array element type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolSliceAlias []BoolAlias // want "field Bools.InvalidBoolSliceAlias array element type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolPtrSliceAlias []*BoolAlias // want "field InvalidBoolPtrSliceAlias array element pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolPtrSliceAlias []*BoolAlias // want "field Bools.InvalidBoolPtrSliceAlias array element pointer type BoolAlias should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidMapStringToBool map[string]bool // want "field InvalidMapStringToBool map value should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidMapStringToBool map[string]bool // want "field Bools.InvalidMapStringToBool map value should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidMapStringToBoolPtr map[string]*bool // want "field InvalidMapStringToBoolPtr map value pointer should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidMapStringToBoolPtr map[string]*bool // want "field Bools.InvalidMapStringToBoolPtr map value pointer should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidMapBoolToString map[bool]string // want "field InvalidMapBoolToString map key should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidMapBoolToString map[bool]string // want "field Bools.InvalidMapBoolToString map key should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidMapBoolPtrToString map[*bool]string // want "field InvalidMapBoolPtrToString map key pointer should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidMapBoolPtrToString map[*bool]string // want "field Bools.InvalidMapBoolPtrToString map key pointer should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolAliasFromAnotherFile BoolAliasB // want "field InvalidBoolAliasFromAnotherFile type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolAliasFromAnotherFile BoolAliasB // want "field Bools.InvalidBoolAliasFromAnotherFile type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."

InvalidBoolPtrAliasFromAnotherFile *BoolAliasB // want "field InvalidBoolPtrAliasFromAnotherFile pointer type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
InvalidBoolPtrAliasFromAnotherFile *BoolAliasB // want "field Bools.InvalidBoolPtrAliasFromAnotherFile pointer type BoolAliasB should not use a bool. Use a string type with meaningful constant values as an enum."
}

// DoNothing is used to check that the analyser doesn't report on methods.
Expand Down
Loading