Skip to content

Commit b90ff89

Browse files
authored
Merge pull request kubernetes#130699 from thockin/master_validation-gen_odd_cases
Prevent validation-gen usage patterns we don't want to support
2 parents f5f9484 + 3460b22 commit b90ff89

File tree

115 files changed

+115
-4476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+115
-4476
lines changed

staging/src/k8s.io/apimachinery/pkg/api/validate/each.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,6 @@ func EachSliceVal[T any](ctx context.Context, op operation.Operation, fldPath *f
4343
return errs
4444
}
4545

46-
// EachSliceValNilable validates each element of newSlice with the specified
47-
// validation function. The comparison function is used to find the
48-
// corresponding value in oldSlice. The value-type of the slices is assumed to
49-
// be nilable.
50-
func EachSliceValNilable[T any](ctx context.Context, op operation.Operation, fldPath *field.Path, newSlice, oldSlice []T,
51-
cmp CompareFunc[T], validator ValidateFunc[T]) field.ErrorList {
52-
var errs field.ErrorList
53-
for i, val := range newSlice {
54-
var old T
55-
if cmp != nil && len(oldSlice) > 0 {
56-
p := lookup(oldSlice, val, cmp)
57-
if p != nil {
58-
old = *p
59-
}
60-
}
61-
errs = append(errs, validator(ctx, op, fldPath.Index(i), val, old)...)
62-
}
63-
return errs
64-
}
65-
6646
// lookup returns a pointer to the first element in the list that matches the
6747
// target, according to the provided comparison function, or else nil.
6848
func lookup[T any](list []T, target T, cmp func(T, T) bool) *T {
@@ -90,22 +70,6 @@ func EachMapVal[K ~string, V any](ctx context.Context, op operation.Operation, f
9070
return errs
9171
}
9272

93-
// EachMapValNilable validates each element of newMap with the specified
94-
// validation function and, if the corresponding key is found in oldMap, the
95-
// old value. The value-type of the slices is assumed to be nilable.
96-
func EachMapValNilable[K ~string, V any](ctx context.Context, op operation.Operation, fldPath *field.Path, newMap, oldMap map[K]V,
97-
validator ValidateFunc[V]) field.ErrorList {
98-
var errs field.ErrorList
99-
for key, val := range newMap {
100-
var old V
101-
if o, found := oldMap[key]; found {
102-
old = o
103-
}
104-
errs = append(errs, validator(ctx, op, fldPath.Key(string(key)), val, old)...)
105-
}
106-
return errs
107-
}
108-
10973
// EachMapKey validates each element of newMap with the specified
11074
// validation function. The oldMap argument is not used.
11175
func EachMapKey[K ~string, T any](ctx context.Context, op operation.Operation, fldPath *field.Path, newMap, oldMap map[K]T,

staging/src/k8s.io/apimachinery/pkg/api/validate/each_test.go

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"k8s.io/apimachinery/pkg/api/operation"
2727
"k8s.io/apimachinery/pkg/util/validation/field"
28-
"k8s.io/utils/ptr"
2928
)
3029

3130
type TestStruct struct {
@@ -102,107 +101,6 @@ func testEachSliceValUpdate[T any](t *testing.T, name string, input []T) {
102101
})
103102
}
104103

105-
func TestEachSliceValNilablePointer(t *testing.T) {
106-
testEachSliceValNilable(t, "valid", []*int{ptr.To(11), ptr.To(12), ptr.To(13)})
107-
testEachSliceValNilable(t, "valid", []*string{ptr.To("a"), ptr.To("b"), ptr.To("c")})
108-
testEachSliceValNilable(t, "valid", []*TestStruct{{11, "a"}, {12, "b"}, {13, "c"}})
109-
110-
testEachSliceValNilable(t, "empty", []*int{})
111-
testEachSliceValNilable(t, "empty", []*string{})
112-
testEachSliceValNilable(t, "empty", []*TestStruct{})
113-
114-
testEachSliceValNilable[int](t, "nil", nil)
115-
testEachSliceValNilable[string](t, "nil", nil)
116-
testEachSliceValNilable[TestStruct](t, "nil", nil)
117-
118-
testEachSliceValNilableUpdate(t, "valid", []*int{ptr.To(11), ptr.To(12), ptr.To(13)})
119-
testEachSliceValNilableUpdate(t, "valid", []*string{ptr.To("a"), ptr.To("b"), ptr.To("c")})
120-
testEachSliceValNilableUpdate(t, "valid", []*TestStruct{{11, "a"}, {12, "b"}, {13, "c"}})
121-
122-
testEachSliceValNilableUpdate(t, "empty", []*int{})
123-
testEachSliceValNilableUpdate(t, "empty", []*string{})
124-
testEachSliceValNilableUpdate(t, "empty", []*TestStruct{})
125-
126-
testEachSliceValNilableUpdate[int](t, "nil", nil)
127-
testEachSliceValNilableUpdate[string](t, "nil", nil)
128-
testEachSliceValNilableUpdate[TestStruct](t, "nil", nil)
129-
}
130-
131-
func TestEachSliceValNilableSlice(t *testing.T) {
132-
testEachSliceValNilable(t, "valid", [][]int{{11, 12, 13}, {12, 13, 14}, {13, 14, 15}})
133-
testEachSliceValNilable(t, "valid", [][]string{{"a", "b", "c"}, {"b", "c", "d"}, {"c", "d", "e"}})
134-
testEachSliceValNilable(t, "valid", [][]TestStruct{
135-
{{11, "a"}, {12, "b"}, {13, "c"}},
136-
{{12, "a"}, {13, "b"}, {14, "c"}},
137-
{{13, "a"}, {14, "b"}, {15, "c"}}})
138-
139-
testEachSliceValNilable(t, "empty", [][]int{{}, {}, {}})
140-
testEachSliceValNilable(t, "empty", [][]string{{}, {}, {}})
141-
testEachSliceValNilable(t, "empty", [][]TestStruct{{}, {}, {}})
142-
143-
testEachSliceValNilable[int](t, "nil", nil)
144-
testEachSliceValNilable[string](t, "nil", nil)
145-
testEachSliceValNilable[TestStruct](t, "nil", nil)
146-
147-
testEachSliceValNilableUpdate(t, "valid", [][]int{{11, 12, 13}, {12, 13, 14}, {13, 14, 15}})
148-
testEachSliceValNilableUpdate(t, "valid", [][]string{{"a", "b", "c"}, {"b", "c", "d"}, {"c", "d", "e"}})
149-
testEachSliceValNilableUpdate(t, "valid", [][]TestStruct{
150-
{{11, "a"}, {12, "b"}, {13, "c"}},
151-
{{12, "a"}, {13, "b"}, {14, "c"}},
152-
{{13, "a"}, {14, "b"}, {15, "c"}}})
153-
154-
testEachSliceValNilableUpdate(t, "empty", [][]int{{}, {}, {}})
155-
testEachSliceValNilableUpdate(t, "empty", [][]string{{}, {}, {}})
156-
testEachSliceValNilableUpdate(t, "empty", [][]TestStruct{{}, {}, {}})
157-
158-
testEachSliceValNilableUpdate[int](t, "nil", nil)
159-
testEachSliceValNilableUpdate[string](t, "nil", nil)
160-
testEachSliceValNilableUpdate[TestStruct](t, "nil", nil)
161-
}
162-
163-
func testEachSliceValNilable[T any](t *testing.T, name string, input []T) {
164-
var zero T
165-
t.Run(fmt.Sprintf("%s(%T)", name, zero), func(t *testing.T) {
166-
calls := 0
167-
vfn := func(ctx context.Context, op operation.Operation, fldPath *field.Path, newVal, oldVal T) field.ErrorList {
168-
if !reflect.DeepEqual(oldVal, zero) {
169-
t.Errorf("expected nil oldVal, got %v", oldVal)
170-
}
171-
calls++
172-
return nil
173-
}
174-
_ = EachSliceValNilable(context.Background(), operation.Operation{}, field.NewPath("test"), input, nil, nil, vfn)
175-
if calls != len(input) {
176-
t.Errorf("expected %d calls, got %d", len(input), calls)
177-
}
178-
})
179-
}
180-
181-
func testEachSliceValNilableUpdate[T any](t *testing.T, name string, input []T) {
182-
var zero T
183-
t.Run(fmt.Sprintf("%s(%T)", name, zero), func(t *testing.T) {
184-
calls := 0
185-
vfn := func(ctx context.Context, op operation.Operation, fldPath *field.Path, newVal, oldVal T) field.ErrorList {
186-
if reflect.DeepEqual(oldVal, zero) {
187-
t.Fatalf("expected non-nil oldVal")
188-
}
189-
if !reflect.DeepEqual(newVal, oldVal) {
190-
t.Errorf("expected oldVal == newVal, got %v, %v", oldVal, newVal)
191-
}
192-
calls++
193-
return nil
194-
}
195-
old := make([]T, len(input))
196-
copy(old, input)
197-
slices.Reverse(old)
198-
cmp := func(a, b T) bool { return reflect.DeepEqual(a, b) }
199-
_ = EachSliceValNilable(context.Background(), operation.Operation{}, field.NewPath("test"), input, old, cmp, vfn)
200-
if calls != len(input) {
201-
t.Errorf("expected %d calls, got %d", len(input), calls)
202-
}
203-
})
204-
}
205-
206104
func TestEachMapVal(t *testing.T) {
207105
testEachMapVal(t, "valid", map[string]int{"one": 11, "two": 12, "three": 13})
208106
testEachMapVal(t, "valid", map[string]string{"A": "a", "B": "b", "C": "c"})
@@ -235,61 +133,6 @@ func testEachMapVal[T any](t *testing.T, name string, input map[string]T) {
235133
})
236134
}
237135

238-
func TestEachMapValNilablePointer(t *testing.T) {
239-
testEachMapValNilable(t, "valid", map[string]*int{"one": ptr.To(11), "two": ptr.To(12), "three": ptr.To(13)})
240-
testEachMapValNilable(t, "valid", map[string]*string{"A": ptr.To("a"), "B": ptr.To("b"), "C": ptr.To("c")})
241-
testEachMapValNilable(t, "valid", map[string]*TestStruct{"one": {11, "a"}, "two": {12, "b"}, "three": {13, "c"}})
242-
243-
testEachMapValNilable(t, "empty", map[string]*int{})
244-
testEachMapValNilable(t, "empty", map[string]*string{})
245-
testEachMapValNilable(t, "empty", map[string]*TestStruct{})
246-
247-
testEachMapValNilable[int](t, "nil", nil)
248-
testEachMapValNilable[string](t, "nil", nil)
249-
testEachMapValNilable[TestStruct](t, "nil", nil)
250-
}
251-
252-
func TestEachMapValNilableSlice(t *testing.T) {
253-
testEachMapValNilable(t, "valid", map[string][]int{
254-
"one": {11, 12, 13},
255-
"two": {12, 13, 14},
256-
"three": {13, 14, 15}})
257-
testEachMapValNilable(t, "valid", map[string][]string{
258-
"A": {"a", "b", "c"},
259-
"B": {"b", "c", "d"},
260-
"C": {"c", "d", "e"}})
261-
testEachMapValNilable(t, "valid", map[string][]TestStruct{
262-
"one": {{11, "a"}, {12, "b"}, {13, "c"}},
263-
"two": {{12, "a"}, {13, "b"}, {14, "c"}},
264-
"three": {{13, "a"}, {14, "b"}, {15, "c"}}})
265-
266-
testEachMapValNilable(t, "empty", map[string][]int{"a": {}, "b": {}, "c": {}})
267-
testEachMapValNilable(t, "empty", map[string][]string{"a": {}, "b": {}, "c": {}})
268-
testEachMapValNilable(t, "empty", map[string][]TestStruct{"a": {}, "b": {}, "c": {}})
269-
270-
testEachMapValNilable[int](t, "nil", nil)
271-
testEachMapValNilable[string](t, "nil", nil)
272-
testEachMapValNilable[TestStruct](t, "nil", nil)
273-
}
274-
275-
func testEachMapValNilable[T any](t *testing.T, name string, input map[string]T) {
276-
var zero T
277-
t.Run(fmt.Sprintf("%s(%T)", name, zero), func(t *testing.T) {
278-
calls := 0
279-
vfn := func(ctx context.Context, op operation.Operation, fldPath *field.Path, newVal, oldVal T) field.ErrorList {
280-
if !reflect.DeepEqual(oldVal, zero) {
281-
t.Errorf("expected nil oldVal, got %v", oldVal)
282-
}
283-
calls++
284-
return nil
285-
}
286-
_ = EachMapValNilable(context.Background(), operation.Operation{}, field.NewPath("test"), input, nil, vfn)
287-
if calls != len(input) {
288-
t.Errorf("expected %d calls, got %d", len(input), calls)
289-
}
290-
})
291-
}
292-
293136
type StringType string
294137

295138
func TestEachMapKey(t *testing.T) {

staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/cross_pkg/doc.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,20 @@ type T1 struct {
9090
// +k8s:eachVal=+k8s:validateTrue="field T1.SliceOfOtherStruct values"
9191
// +k8s:eachVal=+k8s:opaqueType
9292
SliceOfOtherStruct []other.StructType `json:"sliceOfOtherStruct"`
93-
// +k8s:validateTrue="field T1.SliceOfOtherStructPtr"
94-
// +k8s:eachVal=+k8s:validateTrue="field T1.SliceOfOtherStructPtr values"
95-
// +k8s:eachVal=+k8s:opaqueType
96-
SliceOfOtherStructPtr []*other.StructType `json:"sliceOfOtherStructPtr"`
9793

9894
// +k8s:validateTrue="field T1.ListMapOfOtherStruct"
9995
// +k8s:eachVal=+k8s:validateTrue="field T1.SliceOfOtherStruct values"
10096
// +k8s:listType=map
10197
// +k8s:listMapKey=stringField
10298
// +k8s:eachVal=+k8s:opaqueType
10399
ListMapOfOtherStruct []other.StructType `json:"listMapOfOtherStruct"`
104-
// +k8s:validateTrue="field T1.ListMapOfOtherStructPtr"
105-
// +k8s:eachVal=+k8s:validateTrue="field T1.SliceOfOtherStructPtr values"
106-
// +k8s:listType=map
107-
// +k8s:listMapKey=stringField
108-
// +k8s:eachVal=+k8s:opaqueType
109-
ListMapOfOtherStructPtr []*other.StructType `json:"listMapOfOtherStructPtr"`
110100

111101
// +k8s:validateTrue="field T1.MapOfOtherStringToOtherStruct"
112102
// +k8s:eachKey=+k8s:validateTrue="field T1.MapOfOtherStringToOtherStruct keys"
113103
// +k8s:eachVal=+k8s:validateTrue="field T1.MapOfOtherStringToOtherStruct values"
114104
// +k8s:eachKey=+k8s:opaqueType
115105
// +k8s:eachVal=+k8s:opaqueType
116106
MapOfOtherStringToOtherStruct map[other.StringType]other.StructType `json:"mapOfOtherStringToOtherStruct"`
117-
// +k8s:validateTrue="field T1.MapOfOtherStringToOtherStructPtr"
118-
// +k8s:eachKey=+k8s:validateTrue="field T1.MapOfOtherStringToOtherStructPtr keys"
119-
// +k8s:eachVal=+k8s:validateTrue="field T1.MapOfOtherStringToOtherStructPtr values"
120-
// +k8s:eachKey=+k8s:opaqueType
121-
// +k8s:eachVal=+k8s:opaqueType
122-
MapOfOtherStringToOtherStructPtr map[other.StringType]*other.StructType `json:"mapOfOtherStringToOtherStructPtr"`
123107
}
124108

125109
// TODO: the validateFalse test fixture doesn't handle map and slice types, and

staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/cross_pkg/zz_generated.validations.go

Lines changed: 0 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/maps/map_of_map/deep/doc.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)