diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go index 4cbbaafe63c..ed03a579c52 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go @@ -1211,21 +1211,20 @@ func (g *genValidations) emitValidationVariables(c *generator.Context, t *types. tn := g.discovered.typeNodes[t] variables := tn.typeValidations.Variables - slices.SortFunc(variables, func(a, b validators.VariableGen) int { - return cmp.Compare(a.Var().Name, b.Var().Name) + slices.SortFunc(variables, func(a, b *validators.VariableGen) int { + return cmp.Compare(a.Variable.Name, b.Variable.Name) }) for _, variable := range variables { - fn := variable.Init() + fn := variable.InitFunc targs := generator.Args{ - "varName": c.Universe.Type(types.Name(variable.Var())), + "varName": c.Universe.Type(types.Name(variable.Variable)), "initFn": c.Universe.Type(fn.Function), } for _, comment := range fn.Comments { sw.Do("// $.$\n", comment) } sw.Do("var $.varName|private$ = $.initFn|raw$", targs) - typeArgs := variable.Init().TypeArgs - if len(typeArgs) > 0 { + if typeArgs := fn.TypeArgs; len(typeArgs) > 0 { sw.Do("[", nil) for i, typeArg := range typeArgs { sw.Do("$.|raw$", c.Universe.Type(typeArg)) diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/validators/validators.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/validators/validators.go index 48a9e6ca643..4839d2d673e 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/validators/validators.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/validators/validators.go @@ -232,7 +232,7 @@ type Validations struct { // Variables holds any variables which must be generated to perform // validation. Variables are not permitted in every context. - Variables []VariableGen + Variables []*VariableGen // Comments holds comments to emit (without the leanding "//"). Comments []string @@ -263,7 +263,7 @@ func (v *Validations) AddFunction(f FunctionGen) { v.Functions = append(v.Functions, f) } -func (v *Validations) AddVariable(variable VariableGen) { +func (v *Validations) AddVariable(variable *VariableGen) { v.Variables = append(v.Variables, variable) } @@ -325,20 +325,6 @@ type Identifier types.Name // PrivateVars are generated using the PrivateNamer strategy. type PrivateVar types.Name -// VariableGen provides validation-gen with the information needed to generate variable. -// Variables typically support generated functions by providing static information such -// as the list of supported symbols for an enum. -type VariableGen interface { - // TagName returns the tag which triggers this validator. - TagName() string - - // Var returns the variable identifier. - Var() PrivateVar - - // Init generates the function call that the variable is assigned to. - Init() FunctionGen -} - // Function creates a FunctionGen for a given function name and extraArgs. func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) FunctionGen { return FunctionGen{ @@ -405,28 +391,19 @@ func (fg FunctionGen) WithComment(comment string) FunctionGen { } // Variable creates a VariableGen for a given function name and extraArgs. -func Variable(variable PrivateVar, init FunctionGen) VariableGen { - return &variableGen{ - variable: variable, - init: init, +func Variable(variable PrivateVar, initFunc FunctionGen) *VariableGen { + return &VariableGen{ + Variable: variable, + InitFunc: initFunc, } } -type variableGen struct { - variable PrivateVar - init FunctionGen -} +type VariableGen struct { + // Variable holds the variable identifier. + Variable PrivateVar -func (v variableGen) TagName() string { - return v.init.TagName -} - -func (v variableGen) Var() PrivateVar { - return v.variable -} - -func (v variableGen) Init() FunctionGen { - return v.init + // InitFunc describes the function call that the variable is assigned to. + InitFunc FunctionGen } // WrapperFunction describes a function literal which has the fingerprint of a