forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from yongruilin/validation-gen-plus-yongrlin-m…
…axitem feat: Support `maxItems` validation
- Loading branch information
Showing
14 changed files
with
939 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../k8s.io/code-generator/cmd/validation-gen/output_tests/maxitems/slice_of_primitive/doc.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2024 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. | ||
*/ | ||
|
||
// +k8s:validation-gen=TypeMeta | ||
// +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme | ||
|
||
// This is a test package. | ||
package sliceofprimitive | ||
|
||
import "k8s.io/code-generator/cmd/validation-gen/testscheme" | ||
|
||
var localSchemeBuilder = testscheme.New() | ||
|
||
// Maxitems | ||
type M struct { | ||
TypeMeta int | ||
|
||
// slice-of-primitive | ||
// +k8s:validation:maxItems=1 | ||
M0 []int `json:"m0"` | ||
|
||
// slice-of-pointer-to-primitive | ||
// Validation on field only. | ||
// +k8s:validation:maxItems=1 | ||
M1 []*int `json:"m1"` | ||
} |
42 changes: 42 additions & 0 deletions
42
...o/code-generator/cmd/validation-gen/output_tests/maxitems/slice_of_primitive/docs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright 2024 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 sliceofprimitive | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
st := localSchemeBuilder.Test(t) | ||
|
||
st.Value(&M{M0: []int{0, 0}}). | ||
ExpectInvalid( | ||
field.TooMany(field.NewPath("m0"), 2, 1), | ||
) | ||
|
||
st.Value(&M{M0: []int{0}}). | ||
ExpectValid() | ||
|
||
st.Value(&M{M1: []*int{new(int), new(int)}}). | ||
ExpectInvalid( | ||
field.TooMany(field.NewPath("m1"), 2, 1), | ||
) | ||
|
||
st.Value(&M{M1: []*int{new(int)}}). | ||
ExpectValid() | ||
} |
72 changes: 72 additions & 0 deletions
72
...r/cmd/validation-gen/output_tests/maxitems/slice_of_primitive/zz_generated.validations.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
.../src/k8s.io/code-generator/cmd/validation-gen/output_tests/maxitems/slice_of_slice/doc.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2024 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. | ||
*/ | ||
|
||
// +k8s:validation-gen=TypeMeta | ||
// +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme | ||
|
||
// This is a test package. | ||
package sliceofslice | ||
|
||
import "k8s.io/code-generator/cmd/validation-gen/testscheme" | ||
|
||
var localSchemeBuilder = testscheme.New() | ||
|
||
// Maxitems | ||
type M struct { | ||
TypeMeta int | ||
|
||
// slice-of-slice-of-value | ||
// +k8s:validation:maxItems=1 | ||
M0 [][]int `json:"m0"` | ||
|
||
// slice-of-typedef-of-slice-of-value | ||
// +k8s:validation:maxItems=1 | ||
M1 []IntSlice `json:"m1"` | ||
|
||
// slice-of-slice-of-pointer | ||
// +k8s:validation:maxItems=1 | ||
M2 [][]*int `json:"m2"` | ||
|
||
// slice-of-typedef-of-slice-of-pointer | ||
// Validation on field only. | ||
// +k8s:validation:maxItems=1 | ||
M3 []IntPtrSlice `json:"m3"` | ||
} | ||
|
||
// Note: no limit here | ||
type IntSlice []int | ||
|
||
// Note: no limit here | ||
type IntPtrSlice []*int |
58 changes: 58 additions & 0 deletions
58
...8s.io/code-generator/cmd/validation-gen/output_tests/maxitems/slice_of_slice/docs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2024 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 sliceofslice | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
st := localSchemeBuilder.Test(t) | ||
|
||
st.Value(&M{M0: [][]int{{0, 0}, {0, 0}}}). | ||
ExpectInvalid( | ||
field.TooMany(field.NewPath("m0"), 2, 1), | ||
) | ||
|
||
st.Value(&M{M0: [][]int{{0, 0}}}). | ||
ExpectValid() | ||
|
||
st.Value(&M{M1: []IntSlice{{0}}}). | ||
ExpectValid() | ||
|
||
st.Value(&M{M1: []IntSlice{{}, {}}}). | ||
ExpectInvalid( | ||
field.TooMany(field.NewPath("m1"), 2, 1), | ||
) | ||
st.Value(&M{M2: [][]*int{}}). | ||
ExpectValid() | ||
|
||
st.Value(&M{M2: [][]*int{{}, {}}}). | ||
ExpectInvalid( | ||
field.TooMany(field.NewPath("m2"), 2, 1), | ||
) | ||
|
||
st.Value(&M{M3: []IntPtrSlice{}}). | ||
ExpectValid() | ||
|
||
st.Value(&M{M3: []IntPtrSlice{{}, {}}}). | ||
ExpectInvalid( | ||
field.TooMany(field.NewPath("m3"), 2, 1), | ||
) | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
...rator/cmd/validation-gen/output_tests/maxitems/slice_of_slice/zz_generated.validations.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.