|
| 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 notimestamp |
| 18 | + |
| 19 | +import ( |
| 20 | + "go/ast" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "golang.org/x/tools/go/analysis" |
| 24 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 25 | + "golang.org/x/tools/go/ast/inspector" |
| 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/utils" |
| 29 | +) |
| 30 | + |
| 31 | +const name = "notimestamp" |
| 32 | + |
| 33 | +// Analyzer is the analyzer for the notimestamp package. |
| 34 | +// It checks that no struct fields named 'timestamp', or that contain timestamp as a |
| 35 | +// substring are present. |
| 36 | +var Analyzer = &analysis.Analyzer{ |
| 37 | + Name: name, |
| 38 | + Doc: "check that non of the struct field named timestamp or contain timestamp as a substring", |
| 39 | + Run: run, |
| 40 | + Requires: []*analysis.Analyzer{inspect.Analyzer, extractjsontags.Analyzer}, |
| 41 | +} |
| 42 | + |
| 43 | +func run(pass *analysis.Pass) (any, error) { |
| 44 | + inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 45 | + if !ok { |
| 46 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 47 | + } |
| 48 | + |
| 49 | + jsonTags, ok := pass.ResultOf[extractjsontags.Analyzer].(extractjsontags.StructFieldTags) |
| 50 | + if !ok { |
| 51 | + return nil, kalerrors.ErrCouldNotGetJSONTags |
| 52 | + } |
| 53 | + |
| 54 | + // Filter to fields so that we can iterate over fields in a struct. |
| 55 | + nodeFilter := []ast.Node{ |
| 56 | + (*ast.Field)(nil), |
| 57 | + } |
| 58 | + |
| 59 | + // Preorder visits all the nodes of the AST in depth-first order. It calls |
| 60 | + // f(n) for each node n before it visits n's children. |
| 61 | + // |
| 62 | + // We use the filter defined above, ensuring we only look at struct fields. |
| 63 | + inspect.Preorder(nodeFilter, func(n ast.Node) { |
| 64 | + field, ok := n.(*ast.Field) |
| 65 | + if !ok { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if field == nil || len(field.Names) == 0 { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + fieldName := utils.FieldName(field) |
| 74 | + |
| 75 | + // First check if the struct field name contains 'timestamp' |
| 76 | + if strings.Contains(strings.ToLower(fieldName), "timestamp") { |
| 77 | + pass.Reportf(field.Pos(), |
| 78 | + "field %s: fields with timestamp substring should be avoided", |
| 79 | + fieldName, |
| 80 | + ) |
| 81 | + |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + // Then check if the json serialization of the field contains 'timestamp' |
| 86 | + tagInfo := jsonTags.FieldTags(field) |
| 87 | + |
| 88 | + if strings.Contains(strings.ToLower(tagInfo.Name), "timestamp") { |
| 89 | + pass.Reportf(field.Pos(), |
| 90 | + "field %s: fields with timestamp substring should be avoided", |
| 91 | + fieldName, |
| 92 | + ) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return nil, nil //nolint:nilnil |
| 97 | +} |
0 commit comments