|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package nonpointerstructs |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "go/ast" |
| 21 | + |
| 22 | + "golang.org/x/tools/go/analysis" |
| 23 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 24 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 26 | + markershelper "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/markers" |
| 29 | +) |
| 30 | + |
| 31 | +const name = "nonpointerstructs" |
| 32 | + |
| 33 | +func newAnalyzer() *analysis.Analyzer { |
| 34 | + return &analysis.Analyzer{ |
| 35 | + Name: name, |
| 36 | + Doc: "Checks that non-pointer structs that contain required fields are marked as required. Non-pointer structs that contain no required fields are marked as optional.", |
| 37 | + Run: run, |
| 38 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func run(pass *analysis.Pass) (any, error) { |
| 43 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 44 | + if !ok { |
| 45 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 46 | + } |
| 47 | + |
| 48 | + inspect.InspectFields(func(field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markershelper.Markers, qualifiedFieldName string) { |
| 49 | + checkField(pass, field, markersAccess, jsonTagInfo, qualifiedFieldName) |
| 50 | + }) |
| 51 | + |
| 52 | + return nil, nil //nolint:nilnil |
| 53 | +} |
| 54 | + |
| 55 | +func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markershelper.Markers, jsonTagInfo extractjsontags.FieldTagInfo, qualifiedFieldName string) { |
| 56 | + if field.Type == nil { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if jsonTagInfo.Inline { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + structType, ok := asNonPointerStruct(pass, field.Type) |
| 65 | + if !ok { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + hasRequiredField := hasRequiredField(structType, markersAccess) |
| 70 | + isOptional := utils.IsFieldOptional(field, markersAccess) |
| 71 | + isRequired := utils.IsFieldRequired(field, markersAccess) |
| 72 | + |
| 73 | + switch { |
| 74 | + case hasRequiredField && isRequired, !hasRequiredField && isOptional: |
| 75 | + // This is the desired case. |
| 76 | + case hasRequiredField: |
| 77 | + pass.Reportf(field.Pos(), "field %s is a non-pointer struct with required fields. It must be marked as required.", qualifiedFieldName) |
| 78 | + case !hasRequiredField: |
| 79 | + pass.Reportf(field.Pos(), "field %s is a non-pointer struct with no required fields. It must be marked as optional.", qualifiedFieldName) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func asNonPointerStruct(pass *analysis.Pass, field ast.Expr) (*ast.StructType, bool) { |
| 84 | + switch typ := field.(type) { |
| 85 | + case *ast.StructType: |
| 86 | + return typ, true |
| 87 | + case *ast.Ident: |
| 88 | + typeSpec, ok := utils.LookupTypeSpec(pass, typ) |
| 89 | + if !ok { |
| 90 | + return nil, false |
| 91 | + } |
| 92 | + |
| 93 | + return asNonPointerStruct(pass, typeSpec.Type) |
| 94 | + default: |
| 95 | + return nil, false |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func hasRequiredField(structType *ast.StructType, markersAccess markershelper.Markers) bool { |
| 100 | + for _, field := range structType.Fields.List { |
| 101 | + if utils.IsFieldRequired(field, markersAccess) { |
| 102 | + return true |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + structMarkers := markersAccess.StructMarkers(structType) |
| 107 | + |
| 108 | + if structMarkers.Has(markers.KubebuilderMinPropertiesMarker) && !structMarkers.HasWithValue(fmt.Sprintf("%s=0", markers.KubebuilderMinPropertiesMarker)) { |
| 109 | + // A non-zero min properties marker means that the struct is validated to have at least one field. |
| 110 | + // This means it can be treated the same as having a required field. |
| 111 | + return true |
| 112 | + } |
| 113 | + |
| 114 | + return false |
| 115 | +} |
0 commit comments