|
| 1 | +package maxlength |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "go/ast" |
| 7 | + |
| 8 | + "github.com/JoelSpeed/kal/pkg/analysis/helpers/markers" |
| 9 | + "golang.org/x/tools/go/analysis" |
| 10 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 11 | + "golang.org/x/tools/go/ast/inspector" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + name = "maxlength" |
| 16 | + |
| 17 | + kubebuilderMaxLength = "kubebuilder:validation:MaxLength" |
| 18 | + kubebuilderEnum = "kubebuilder:validation:Enum" |
| 19 | + kubebuilderFormat = "kubebuilder:validation:Format" |
| 20 | + |
| 21 | + kubebuilderItemsMaxLength = "kubebuilder:validation:items:MaxLength" |
| 22 | + kubebuilderItemsEnum = "kubebuilder:validation:items:Enum" |
| 23 | + kubebuilderItemsFormat = "kubebuilder:validation:items:Format" |
| 24 | + |
| 25 | + kubebuilderMaxItems = "kubebuilder:validation:MaxItems" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + errCouldNotGetInspector = errors.New("could not get inspector") |
| 30 | + errCouldNotGetMarkers = errors.New("could not get markers") |
| 31 | +) |
| 32 | + |
| 33 | +// Analyzer is the analyzer for the maxlength package. |
| 34 | +// It checks that strings and arrays have maximum lengths and maximum items respectively. |
| 35 | +var Analyzer = &analysis.Analyzer{ |
| 36 | + Name: name, |
| 37 | + Doc: "Checks that all strings formatted fields are marked with a maximum length, and that arrays are marked with max items.", |
| 38 | + Run: run, |
| 39 | + Requires: []*analysis.Analyzer{inspect.Analyzer, markers.Analyzer}, |
| 40 | +} |
| 41 | + |
| 42 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 43 | + inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 44 | + if !ok { |
| 45 | + return nil, errCouldNotGetInspector |
| 46 | + } |
| 47 | + |
| 48 | + markersAccess, ok := pass.ResultOf[markers.Analyzer].(markers.Markers) |
| 49 | + if !ok { |
| 50 | + return nil, errCouldNotGetMarkers |
| 51 | + } |
| 52 | + |
| 53 | + // Filter to structs so that we can iterate over the fields in a struct. |
| 54 | + nodeFilter := []ast.Node{ |
| 55 | + (*ast.StructType)(nil), |
| 56 | + } |
| 57 | + |
| 58 | + inspect.Preorder(nodeFilter, func(n ast.Node) { |
| 59 | + sTyp, ok := n.(*ast.StructType) |
| 60 | + if !ok { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if sTyp.Fields == nil { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + for _, field := range sTyp.Fields.List { |
| 69 | + checkField(pass, field, markersAccess) |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + return nil, nil //nolint:nilnil |
| 74 | +} |
| 75 | + |
| 76 | +func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers) { |
| 77 | + if len(field.Names) == 0 || field.Names[0] == nil { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + fieldName := field.Names[0].Name |
| 82 | + prefix := fmt.Sprintf("field %s", fieldName) |
| 83 | + |
| 84 | + checkTypeExpr(pass, field.Type, field, nil, markersAccess, prefix, kubebuilderMaxLength, needsStringMaxLength) |
| 85 | +} |
| 86 | + |
| 87 | +func checkIdent(pass *analysis.Pass, ident *ast.Ident, node ast.Node, aliases []*ast.TypeSpec, markersAccess markers.Markers, prefix, marker string, needsMaxLength func(markers.MarkerSet) bool) { |
| 88 | + if ident.Obj == nil { // Built-in type |
| 89 | + if ident.Name == "string" { |
| 90 | + markers := getCombinedMarkers(markersAccess, node, aliases) |
| 91 | + |
| 92 | + if needsMaxLength(markers) { |
| 93 | + pass.Reportf(node.Pos(), "%s must have a maximum length, add %s marker", prefix, marker) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + tSpec, ok := ident.Obj.Decl.(*ast.TypeSpec) |
| 101 | + if !ok { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + checkTypeSpec(pass, tSpec, node, append(aliases, tSpec), markersAccess, fmt.Sprintf("%s type", prefix), marker, needsMaxLength) |
| 106 | +} |
| 107 | + |
| 108 | +func checkTypeSpec(pass *analysis.Pass, tSpec *ast.TypeSpec, node ast.Node, aliases []*ast.TypeSpec, markersAccess markers.Markers, prefix, marker string, needsMaxLength func(markers.MarkerSet) bool) { |
| 109 | + if tSpec.Name == nil { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + typeName := tSpec.Name.Name |
| 114 | + prefix = fmt.Sprintf("%s %s", prefix, typeName) |
| 115 | + |
| 116 | + checkTypeExpr(pass, tSpec.Type, node, aliases, markersAccess, prefix, marker, needsMaxLength) |
| 117 | +} |
| 118 | + |
| 119 | +func checkTypeExpr(pass *analysis.Pass, typeExpr ast.Expr, node ast.Node, aliases []*ast.TypeSpec, markersAccess markers.Markers, prefix, marker string, needsMaxLength func(markers.MarkerSet) bool) { |
| 120 | + switch typ := typeExpr.(type) { |
| 121 | + case *ast.Ident: |
| 122 | + checkIdent(pass, typ, node, aliases, markersAccess, prefix, marker, needsMaxLength) |
| 123 | + case *ast.StarExpr: |
| 124 | + checkTypeExpr(pass, typ.X, node, aliases, markersAccess, prefix, marker, needsMaxLength) |
| 125 | + case *ast.ArrayType: |
| 126 | + checkArrayType(pass, typ, node, aliases, markersAccess, prefix) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func checkArrayType(pass *analysis.Pass, arrayType *ast.ArrayType, node ast.Node, aliases []*ast.TypeSpec, markersAccess markers.Markers, prefix string) { |
| 131 | + if arrayType.Elt != nil { |
| 132 | + if ident, ok := arrayType.Elt.(*ast.Ident); ok { |
| 133 | + checkArrayElementIdent(pass, ident, node, aliases, markersAccess, fmt.Sprintf("%s array element", prefix)) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + markers := getCombinedMarkers(markersAccess, node, aliases) |
| 138 | + |
| 139 | + if !markers.Has(kubebuilderMaxItems) { |
| 140 | + pass.Reportf(node.Pos(), "%s must have a maximum items, add %s marker", prefix, kubebuilderMaxItems) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func checkArrayElementIdent(pass *analysis.Pass, ident *ast.Ident, node ast.Node, aliases []*ast.TypeSpec, markersAccess markers.Markers, prefix string) { |
| 145 | + if ident.Obj == nil { // Built-in type |
| 146 | + if ident.Name == "string" { |
| 147 | + markers := getCombinedMarkers(markersAccess, node, aliases) |
| 148 | + |
| 149 | + if needsItemsMaxLength(markers) { |
| 150 | + pass.Reportf(node.Pos(), "%s must have a maximum length, add %s marker", prefix, kubebuilderItemsMaxLength) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + tSpec, ok := ident.Obj.Decl.(*ast.TypeSpec) |
| 158 | + if !ok { |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + // If the array element wasn't directly a string, allow a string alias to be used |
| 163 | + // with either the items style markers or the on alias style markers. |
| 164 | + checkTypeSpec(pass, tSpec, node, append(aliases, tSpec), markersAccess, fmt.Sprintf("%s type", prefix), kubebuilderMaxLength, func(ms markers.MarkerSet) bool { |
| 165 | + return needsStringMaxLength(ms) && needsItemsMaxLength(ms) |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +func getCombinedMarkers(markersAccess markers.Markers, node ast.Node, aliases []*ast.TypeSpec) markers.MarkerSet { |
| 170 | + base := markers.NewMarkerSet(getMarkers(markersAccess, node).UnsortedList()...) |
| 171 | + |
| 172 | + for _, a := range aliases { |
| 173 | + base.Insert(getMarkers(markersAccess, a).UnsortedList()...) |
| 174 | + } |
| 175 | + |
| 176 | + return base |
| 177 | +} |
| 178 | + |
| 179 | +func getMarkers(markersAccess markers.Markers, node ast.Node) markers.MarkerSet { |
| 180 | + switch t := node.(type) { |
| 181 | + case *ast.Field: |
| 182 | + return markersAccess.FieldMarkers(t) |
| 183 | + case *ast.TypeSpec: |
| 184 | + return markersAccess.TypeMarkers(t) |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +// needsMaxLength returns true if the field needs a maximum length. |
| 191 | +// Fields do not need a maximum length if they are already marked with a maximum length, |
| 192 | +// or if they are an enum, or if they are a date, date-time or duration. |
| 193 | +func needsStringMaxLength(markerSet markers.MarkerSet) bool { |
| 194 | + switch { |
| 195 | + case markerSet.Has(kubebuilderMaxLength), |
| 196 | + markerSet.Has(kubebuilderEnum), |
| 197 | + markerSet.HasWithValue(kubebuilderFormatWithValue("date")), |
| 198 | + markerSet.HasWithValue(kubebuilderFormatWithValue("date-time")), |
| 199 | + markerSet.HasWithValue(kubebuilderFormatWithValue("duration")): |
| 200 | + return false |
| 201 | + } |
| 202 | + |
| 203 | + return true |
| 204 | +} |
| 205 | + |
| 206 | +func needsItemsMaxLength(markerSet markers.MarkerSet) bool { |
| 207 | + switch { |
| 208 | + case markerSet.Has(kubebuilderItemsMaxLength), |
| 209 | + markerSet.Has(kubebuilderItemsEnum), |
| 210 | + markerSet.HasWithValue(kubebuilderItemsFormatWithValue("date")), |
| 211 | + markerSet.HasWithValue(kubebuilderItemsFormatWithValue("date-time")), |
| 212 | + markerSet.HasWithValue(kubebuilderItemsFormatWithValue("duration")): |
| 213 | + return false |
| 214 | + } |
| 215 | + |
| 216 | + return true |
| 217 | +} |
| 218 | + |
| 219 | +func kubebuilderFormatWithValue(value string) string { |
| 220 | + return fmt.Sprintf("%s:=%s", kubebuilderFormat, value) |
| 221 | +} |
| 222 | + |
| 223 | +func kubebuilderItemsFormatWithValue(value string) string { |
| 224 | + return fmt.Sprintf("%s:=%s", kubebuilderItemsFormat, value) |
| 225 | +} |
0 commit comments