|
| 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 | + |
| 17 | +package dependenttags |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "go/ast" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "golang.org/x/tools/go/analysis" |
| 25 | + |
| 26 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 29 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 30 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 31 | +) |
| 32 | + |
| 33 | +// analyzer implements the dependenttags linter. |
| 34 | +type analyzer struct { |
| 35 | + cfg Config |
| 36 | +} |
| 37 | + |
| 38 | +// newAnalyzer creates a new analyzer. |
| 39 | +func newAnalyzer(cfg Config) *analysis.Analyzer { |
| 40 | + // Register markers from configuration |
| 41 | + for _, rule := range cfg.Rules { |
| 42 | + markers.DefaultRegistry().Register(rule.Identifier) |
| 43 | + |
| 44 | + for _, dep := range rule.Dependents { |
| 45 | + markers.DefaultRegistry().Register(dep) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + a := &analyzer{ |
| 50 | + cfg: cfg, |
| 51 | + } |
| 52 | + |
| 53 | + return &analysis.Analyzer{ |
| 54 | + Name: name, |
| 55 | + Doc: "Enforces dependencies between markers.", |
| 56 | + Run: a.run, |
| 57 | + Requires: []*analysis.Analyzer{inspector.Analyzer, markers.Analyzer}, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// run is the main function for the analyzer. |
| 62 | +func (a *analyzer) run(pass *analysis.Pass) (any, error) { |
| 63 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 64 | + if !ok { |
| 65 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 66 | + } |
| 67 | + |
| 68 | + inspect.InspectFields(func(field *ast.Field, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers, qualifiedFieldName string) { |
| 69 | + if field.Doc == nil { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + fieldMarkers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) |
| 74 | + |
| 75 | + for _, rule := range a.cfg.Rules { |
| 76 | + if _, ok := fieldMarkers[rule.Identifier]; ok { |
| 77 | + switch rule.Type { |
| 78 | + case DependencyTypeAny: |
| 79 | + handleAny(pass, field, rule, fieldMarkers, qualifiedFieldName) |
| 80 | + case DependencyTypeAll: |
| 81 | + handleAll(pass, field, rule, fieldMarkers, qualifiedFieldName) |
| 82 | + default: |
| 83 | + panic(fmt.Sprintf("unknown dependency type %s", rule.Type)) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + return nil, nil //nolint:nilnil |
| 90 | +} |
| 91 | +func handleAll(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers markers.MarkerSet, qualifiedFieldName string) { |
| 92 | + missing := make([]string, 0, len(rule.Dependents)) |
| 93 | + |
| 94 | + for _, dependent := range rule.Dependents { |
| 95 | + if _, depOk := fieldMarkers[dependent]; !depOk { |
| 96 | + missing = append(missing, fmt.Sprintf("+%s", dependent)) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if len(missing) > 0 { |
| 101 | + pass.Reportf(field.Pos(), "field %s with marker +%s is missing required marker(s): %s", qualifiedFieldName, rule.Identifier, strings.Join(missing, ", ")) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func handleAny(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers markers.MarkerSet, qualifiedFieldName string) { |
| 106 | + found := false |
| 107 | + |
| 108 | + for _, dependent := range rule.Dependents { |
| 109 | + if _, depOk := fieldMarkers[dependent]; depOk { |
| 110 | + found = true |
| 111 | + break |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if !found { |
| 116 | + dependents := make([]string, len(rule.Dependents)) |
| 117 | + for i, d := range rule.Dependents { |
| 118 | + dependents[i] = fmt.Sprintf("+%s", d) |
| 119 | + } |
| 120 | + |
| 121 | + pass.Reportf(field.Pos(), "field %s with marker +%s requires at least one of the following markers, but none were found: %s", qualifiedFieldName, rule.Identifier, strings.Join(dependents, ", ")) |
| 122 | + } |
| 123 | +} |
0 commit comments