File: names_match.go

package info (click to toggle)
golang-k8s-kube-openapi 0.0~git20241212.2c72e55-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,396 kB
  • sloc: sh: 50; makefile: 5
file content (182 lines) | stat: -rw-r--r-- 5,846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rules

import (
	"reflect"
	"strings"

	"k8s.io/kube-openapi/pkg/util/sets"

	"k8s.io/gengo/v2/types"
)

var (
	// Blacklist of JSON tags that should skip match evaluation
	jsonTagBlacklist = sets.NewString(
		// Omitted field is ignored by the package
		"-",
	)

	// List of substrings that aren't allowed in Go name and JSON name
	disallowedNameSubstrings = sets.NewString(
		// Underscore is not allowed in either name
		"_",
		// Dash is not allowed in either name. Note that since dash is a valid JSON tag, this should be checked
		// after JSON tag blacklist check.
		"-",
	)
)

/*
NamesMatch implements APIRule interface.
Go field names must be CamelCase. JSON field names must be camelCase. Other than capitalization of the
initial letter, the two should almost always match. No underscores nor dashes in either.
This rule verifies the convention "Other than capitalization of the initial letter, the two should almost always match."
Examples (also in unit test):

	Go name      | JSON name    | match
	               podSpec        false
	PodSpec        podSpec        true
	PodSpec        PodSpec        false
	podSpec        podSpec        false
	PodSpec        spec           false
	Spec           podSpec        false
	JSONSpec       jsonSpec       true
	JSONSpec       jsonspec       false
	HTTPJSONSpec   httpJSONSpec   true

NOTE: this validator cannot tell two sequential all-capital words from one word, therefore the case below
is also considered matched.

	HTTPJSONSpec   httpjsonSpec   true

NOTE: an empty JSON name is valid only for inlined structs or pointer to structs.
It cannot be empty for anything else because capitalization must be set explicitly.

NOTE: metav1.ListMeta and metav1.ObjectMeta by convention must have "metadata" as name.
Other fields may have that JSON name if the field name matches.
*/
type NamesMatch struct{}

// Name returns the name of APIRule
func (n *NamesMatch) Name() string {
	return "names_match"
}

// Validate evaluates API rule on type t and returns a list of field names in
// the type that violate the rule. Empty field name [""] implies the entire
// type violates the rule.
func (n *NamesMatch) Validate(t *types.Type) ([]string, error) {
	fields := make([]string, 0)

	// Only validate struct type and ignore the rest
	switch t.Kind {
	case types.Struct:
		for _, m := range t.Members {
			goName := m.Name
			jsonTag, ok := reflect.StructTag(m.Tags).Lookup("json")
			// Distinguish empty JSON tag and missing JSON tag. Empty JSON tag / name is
			// allowed (in JSON name blacklist) but missing JSON tag is invalid.
			if !ok {
				fields = append(fields, goName)
				continue
			}
			if jsonTagBlacklist.Has(jsonTag) {
				continue
			}
			jsonName := strings.Split(jsonTag, ",")[0]
			if !nameIsOkay(m, jsonName) {
				fields = append(fields, goName)
			}
		}
	}
	return fields, nil
}

func nameIsOkay(member types.Member, jsonName string) bool {
	if jsonName == "" {
		return member.Type.Kind == types.Struct ||
			member.Type.Kind == types.Pointer && member.Type.Elem.Kind == types.Struct
	}

	typeName := member.Type.String()
	switch typeName {
	case "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta",
		"k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta":
		return jsonName == "metadata"
	}

	return namesMatch(member.Name, jsonName)
}

// namesMatch evaluates if goName and jsonName match the API rule
// TODO: Use an off-the-shelf CamelCase solution instead of implementing this logic. The following existing
//
//	      packages have been tried out:
//			github.com/markbates/inflect
//			github.com/segmentio/go-camelcase
//			github.com/iancoleman/strcase
//			github.com/fatih/camelcase
//		 Please see https://github.com/kubernetes/kube-openapi/pull/83#issuecomment-400842314 for more details
//		 about why they don't satisfy our need. What we need can be a function that detects an acronym at the
//		 beginning of a string.
func namesMatch(goName, jsonName string) bool {
	if !isAllowedName(goName) || !isAllowedName(jsonName) {
		return false
	}
	if !strings.EqualFold(goName, jsonName) {
		return false
	}
	// Go field names must be CamelCase. JSON field names must be camelCase.
	if !isCapital(goName[0]) || isCapital(jsonName[0]) {
		return false
	}
	for i := 0; i < len(goName); i++ {
		if goName[i] == jsonName[i] {
			// goName[0:i-1] is uppercase and jsonName[0:i-1] is lowercase, goName[i:]
			// and jsonName[i:] should match;
			// goName[i] should be lowercase if i is equal to 1, e.g.:
			//	goName   | jsonName
			//	PodSpec     podSpec
			// or uppercase if i is greater than 1, e.g.:
			//      goname   | jsonName
			//      JSONSpec   jsonSpec
			// This is to rule out cases like:
			//      goname   | jsonName
			//      JSONSpec   jsonspec
			return goName[i:] == jsonName[i:] && (i == 1 || isCapital(goName[i]))
		}
	}
	return true
}

// isCapital returns true if one character is capital
func isCapital(b byte) bool {
	return b >= 'A' && b <= 'Z'
}

// isAllowedName checks the list of disallowedNameSubstrings and returns true if name doesn't contain
// any disallowed substring.
func isAllowedName(name string) bool {
	for _, substr := range disallowedNameSubstrings.UnsortedList() {
		if strings.Contains(name, substr) {
			return false
		}
	}
	return true
}