Created
April 20, 2025 03:22
-
-
Save aaron-prindle/f55ef060bf5c5380b6eb6cc822c0f2ff to your computer and use it in GitHub Desktop.
+k8s:unionMember only has "1-level-deep" union context example
This file contains hidden or 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
/* | |
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 multiple | |
import "k8s.io/code-generator/cmd/validation-gen/testscheme" | |
var localSchemeBuilder = testscheme.New() | |
// Two discriminated unions in the same struct | |
type Struct struct { | |
TypeMeta int | |
// +k8s:unionDiscriminator={"union": "union1"} | |
D1 D `json:"d1"` | |
// +k8s:unionMember={"union": "union1"} | |
// +k8s:optional | |
U1M1 *M1 `json:"u1m1"` | |
// +k8s:unionMember={"union": "union1"} | |
// +k8s:optional | |
U1M2 *M2 `json:"u1m2"` | |
// +k8s:unionDiscriminator={"union": "union2"} | |
D2 D `json:"d2"` | |
// +k8s:unionMember={"union": "union2"} | |
// +k8s:optional | |
U2M1 *M1 `json:"u2m1"` | |
// +k8s:unionMember={"union": "union2"} | |
// +k8s:optional | |
U2M2 *M2 `json:"u2m2"` | |
SubStruct *SubStruct `json:"s1"` | |
} | |
type SubStruct struct { | |
TypeMeta int | |
// +k8s:unionMember={"union": "union1"} | |
// +k8s:optional | |
U1M3 *M3 `json:"u1m2"` | |
} | |
type D string | |
const ( | |
U1M1 D = "U1M1" | |
U1M2 D = "U1M2" | |
U2M1 D = "U2M1" | |
U2M2 D = "U2M2" | |
) | |
type M1 struct{} | |
type M2 struct{} | |
type M3 struct{} |
This file contains hidden or 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
//go:build !ignore_autogenerated | |
// +build !ignore_autogenerated | |
/* | |
Copyright 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. | |
*/ | |
// Code generated by validation-gen. DO NOT EDIT. | |
package multiple | |
import ( | |
context "context" | |
operation "k8s.io/apimachinery/pkg/api/operation" | |
safe "k8s.io/apimachinery/pkg/api/safe" | |
validate "k8s.io/apimachinery/pkg/api/validate" | |
field "k8s.io/apimachinery/pkg/util/validation/field" | |
testscheme "k8s.io/code-generator/cmd/validation-gen/testscheme" | |
) | |
func init() { localSchemeBuilder.Register(RegisterValidations) } | |
// RegisterValidations adds validation functions to the given scheme. | |
// Public to allow building arbitrary schemes. | |
func RegisterValidations(scheme *testscheme.Scheme) error { | |
scheme.AddValidationFunc((*Struct)(nil), func(ctx context.Context, op operation.Operation, obj, oldObj interface{}) field.ErrorList { | |
return Validate_Struct(ctx, op, nil /* fldPath */, obj.(*Struct), safe.Cast[*Struct](oldObj)) | |
}) | |
scheme.AddValidationFunc((*SubStruct)(nil), func(ctx context.Context, op operation.Operation, obj, oldObj interface{}) field.ErrorList { | |
return Validate_SubStruct(ctx, op, nil /* fldPath */, obj.(*SubStruct), safe.Cast[*SubStruct](oldObj)) | |
}) | |
return nil | |
} | |
var unionMembershipForStructunion1 = validate.NewDiscriminatedUnionMembership("d1", [2]string{"u1m1", "U1M1"}, [2]string{"u1m2", "U1M2"}) | |
var unionMembershipForStructunion2 = validate.NewDiscriminatedUnionMembership("d2", [2]string{"u2m1", "U2M1"}, [2]string{"u2m2", "U2M2"}) | |
func Validate_Struct(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *Struct) (errs field.ErrorList) { | |
// type Struct | |
errs = append(errs, validate.DiscriminatedUnion(ctx, op, fldPath, obj, oldObj, unionMembershipForStructunion1, obj.D1, obj.U1M1, obj.U1M2)...) | |
errs = append(errs, validate.DiscriminatedUnion(ctx, op, fldPath, obj, oldObj, unionMembershipForStructunion2, obj.D2, obj.U2M1, obj.U2M2)...) | |
// field Struct.TypeMeta has no validation | |
// field Struct.D1 has no validation | |
// field Struct.U1M1 | |
errs = append(errs, | |
func(fldPath *field.Path, obj, oldObj *M1) (errs field.ErrorList) { | |
if e := validate.OptionalPointer(ctx, op, fldPath, obj, oldObj); len(e) != 0 { | |
return // do not proceed | |
} | |
return | |
}(fldPath.Child("u1m1"), obj.U1M1, safe.Field(oldObj, func(oldObj *Struct) *M1 { return oldObj.U1M1 }))...) | |
// field Struct.U1M2 | |
errs = append(errs, | |
func(fldPath *field.Path, obj, oldObj *M2) (errs field.ErrorList) { | |
if e := validate.OptionalPointer(ctx, op, fldPath, obj, oldObj); len(e) != 0 { | |
return // do not proceed | |
} | |
return | |
}(fldPath.Child("u1m2"), obj.U1M2, safe.Field(oldObj, func(oldObj *Struct) *M2 { return oldObj.U1M2 }))...) | |
// field Struct.D2 has no validation | |
// field Struct.U2M1 | |
errs = append(errs, | |
func(fldPath *field.Path, obj, oldObj *M1) (errs field.ErrorList) { | |
if e := validate.OptionalPointer(ctx, op, fldPath, obj, oldObj); len(e) != 0 { | |
return // do not proceed | |
} | |
return | |
}(fldPath.Child("u2m1"), obj.U2M1, safe.Field(oldObj, func(oldObj *Struct) *M1 { return oldObj.U2M1 }))...) | |
// field Struct.U2M2 | |
errs = append(errs, | |
func(fldPath *field.Path, obj, oldObj *M2) (errs field.ErrorList) { | |
if e := validate.OptionalPointer(ctx, op, fldPath, obj, oldObj); len(e) != 0 { | |
return // do not proceed | |
} | |
return | |
}(fldPath.Child("u2m2"), obj.U2M2, safe.Field(oldObj, func(oldObj *Struct) *M2 { return oldObj.U2M2 }))...) | |
// field Struct.SubStruct | |
errs = append(errs, | |
func(fldPath *field.Path, obj, oldObj *SubStruct) (errs field.ErrorList) { | |
errs = append(errs, Validate_SubStruct(ctx, op, fldPath, obj, oldObj)...) | |
return | |
}(fldPath.Child("s1"), obj.SubStruct, safe.Field(oldObj, func(oldObj *Struct) *SubStruct { return oldObj.SubStruct }))...) | |
return errs | |
} | |
var unionMembershipForSubStructunion1 = validate.NewUnionMembership([2]string{"u1m2", "U1M3"}) | |
func Validate_SubStruct(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *SubStruct) (errs field.ErrorList) { | |
// type SubStruct | |
errs = append(errs, validate.Union(ctx, op, fldPath, obj, oldObj, unionMembershipForSubStructunion1, obj.U1M3)...) | |
// field SubStruct.TypeMeta has no validation | |
// field SubStruct.U1M3 | |
errs = append(errs, | |
func(fldPath *field.Path, obj, oldObj *M3) (errs field.ErrorList) { | |
if e := validate.OptionalPointer(ctx, op, fldPath, obj, oldObj); len(e) != 0 { | |
return // do not proceed | |
} | |
return | |
}(fldPath.Child("u1m2"), obj.U1M3, safe.Field(oldObj, func(oldObj *SubStruct) *M3 { return oldObj.U1M3 }))...) | |
return errs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment