Skip to content

Commit 46670a1

Browse files
committed
address comments: simplify struct and field name retrieval; enhance documentation for dependency types
1 parent 89d83bc commit 46670a1

File tree

4 files changed

+14
-23
lines changed

4 files changed

+14
-23
lines changed

pkg/analysis/dependenttags/analyzer.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
7676
if _, ok := fieldMarkers[rule.Identifier]; ok {
7777
switch rule.Type {
7878
case DependencyTypeAny:
79-
handleAny(pass, field, rule, fieldMarkers)
79+
handleAny(pass, field, rule, fieldMarkers, qualifiedFieldName)
8080
case DependencyTypeAll:
81-
handleAll(pass, field, rule, fieldMarkers)
81+
handleAll(pass, field, rule, fieldMarkers, qualifiedFieldName)
8282
default:
8383
panic(fmt.Sprintf("unknown dependency type %s", rule.Type))
8484
}
@@ -88,7 +88,7 @@ func (a *analyzer) run(pass *analysis.Pass) (any, error) {
8888

8989
return nil, nil //nolint:nilnil
9090
}
91-
func handleAll(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers markers.MarkerSet) {
91+
func handleAll(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers markers.MarkerSet, qualifiedFieldName string) {
9292
missing := make([]string, 0, len(rule.Dependents))
9393

9494
for _, dependent := range rule.Dependents {
@@ -98,12 +98,11 @@ func handleAll(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers ma
9898
}
9999

100100
if len(missing) > 0 {
101-
structName, fieldName := utils.GetStructAndFieldName(pass, field)
102-
pass.Reportf(field.Pos(), "field %s.%s with marker +%s is missing required marker(s): %s", structName, fieldName, rule.Identifier, strings.Join(missing, ", "))
101+
pass.Reportf(field.Pos(), "field %s with marker +%s is missing required marker(s): %s", qualifiedFieldName, rule.Identifier, strings.Join(missing, ", "))
103102
}
104103
}
105104

106-
func handleAny(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers markers.MarkerSet) {
105+
func handleAny(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers markers.MarkerSet, qualifiedFieldName string) {
107106
found := false
108107

109108
for _, dependent := range rule.Dependents {
@@ -119,7 +118,6 @@ func handleAny(pass *analysis.Pass, field *ast.Field, rule Rule, fieldMarkers ma
119118
dependents[i] = fmt.Sprintf("+%s", d)
120119
}
121120

122-
structName, fieldName := utils.GetStructAndFieldName(pass, field)
123-
pass.Reportf(field.Pos(), "field %s.%s with marker +%s requires at least one of the following markers, but none were found: %s", structName, fieldName, rule.Identifier, strings.Join(dependents, ", "))
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, ", "))
124122
}
125123
}

pkg/analysis/dependenttags/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Rule struct {
3939
// Dependents are the markers that are required by Main.
4040
Dependents []string `mapstructure:"dependents"`
4141
// Type defines how to interpret the dependents list.
42-
// Can be 'all' (default) or 'any'.
42+
// When set to All, every dependent in the list must be present when the identifier is present on a field or type.
43+
// When set to Any, at least one of the listed dependents must be present when the identifier is present on a field or type.
4344
Type DependencyType `mapstructure:"type,omitempty"`
4445
}

pkg/analysis/dependenttags/doc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,30 @@ limitations under the License.
2121
// a set of other markers (dependent tags) are also present. This is useful for enforcing API
2222
// contracts where certain markers imply the presence of others.
2323
//
24-
// For example, a field marked with `+k-8s:unionMember` must also be marked with `+k8s:optional`.
24+
// For example, a field marked with `+k8s:unionMember` must also be marked with `+k8s:optional`.
2525
//
2626
// # Configuration
2727
//
2828
// The linter is configured with a list of rules. Each rule specifies an identifier marker and a list of
2929
// dependent markers. The `type` field is required and specifies how to interpret the dependents list:
30-
// - `all`: all dependent markers are required.
31-
// - `any`: at least one of the dependent markers is required.
30+
// - `All`: all dependent markers are required.
31+
// - `Any`: at least one of the dependent markers is required.
3232
//
3333
// This linter only checks for the presence or absence of markers; it does not inspect or enforce specific values within those markers. It also does not provide automatic fixes.
3434
//
3535
// linters:
3636
// dependenttags:
3737
// rules:
3838
// - identifier: "k8s:unionMember"
39-
// type: "all"
39+
// type: "All"
4040
// dependents:
4141
// - "k8s:optional"
4242
// - identifier: "listType"
43-
// type: "all"
43+
// type: "All"
4444
// dependents:
4545
// - "k8s:listType"
4646
// - identifier: "example:any"
47-
// type: "any"
47+
// type: "Any"
4848
// dependents:
4949
// - "dep1"
5050
// - "dep2"

pkg/analysis/utils/utils.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,3 @@ func getFieldTypeName(field *ast.Field) string {
491491

492492
return ""
493493
}
494-
495-
// GetStructAndFieldName returns the name of the struct and the field name for a given field.
496-
func GetStructAndFieldName(pass *analysis.Pass, field *ast.Field) (string, string) {
497-
structName := GetStructNameForField(pass, field)
498-
fieldName := FieldName(field)
499-
500-
return structName, fieldName
501-
}

0 commit comments

Comments
 (0)