Skip to content

Commit 4e3d114

Browse files
committed
Refactor VariableGen - no interface needed
1 parent 6a59dcf commit 4e3d114

File tree

2 files changed

+16
-40
lines changed

2 files changed

+16
-40
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,21 +1211,20 @@ func (g *genValidations) emitValidationVariables(c *generator.Context, t *types.
12111211
tn := g.discovered.typeNodes[t]
12121212

12131213
variables := tn.typeValidations.Variables
1214-
slices.SortFunc(variables, func(a, b validators.VariableGen) int {
1215-
return cmp.Compare(a.Var().Name, b.Var().Name)
1214+
slices.SortFunc(variables, func(a, b *validators.VariableGen) int {
1215+
return cmp.Compare(a.Variable.Name, b.Variable.Name)
12161216
})
12171217
for _, variable := range variables {
1218-
fn := variable.Init()
1218+
fn := variable.InitFunc
12191219
targs := generator.Args{
1220-
"varName": c.Universe.Type(types.Name(variable.Var())),
1220+
"varName": c.Universe.Type(types.Name(variable.Variable)),
12211221
"initFn": c.Universe.Type(fn.Function),
12221222
}
12231223
for _, comment := range fn.Comments {
12241224
sw.Do("// $.$\n", comment)
12251225
}
12261226
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
1227-
typeArgs := variable.Init().TypeArgs
1228-
if len(typeArgs) > 0 {
1227+
if typeArgs := fn.TypeArgs; len(typeArgs) > 0 {
12291228
sw.Do("[", nil)
12301229
for i, typeArg := range typeArgs {
12311230
sw.Do("$.|raw$", c.Universe.Type(typeArg))

staging/src/k8s.io/code-generator/cmd/validation-gen/validators/validators.go

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ type Validations struct {
232232

233233
// Variables holds any variables which must be generated to perform
234234
// validation. Variables are not permitted in every context.
235-
Variables []VariableGen
235+
Variables []*VariableGen
236236

237237
// Comments holds comments to emit (without the leanding "//").
238238
Comments []string
@@ -263,7 +263,7 @@ func (v *Validations) AddFunction(f FunctionGen) {
263263
v.Functions = append(v.Functions, f)
264264
}
265265

266-
func (v *Validations) AddVariable(variable VariableGen) {
266+
func (v *Validations) AddVariable(variable *VariableGen) {
267267
v.Variables = append(v.Variables, variable)
268268
}
269269

@@ -325,20 +325,6 @@ type Identifier types.Name
325325
// PrivateVars are generated using the PrivateNamer strategy.
326326
type PrivateVar types.Name
327327

328-
// VariableGen provides validation-gen with the information needed to generate variable.
329-
// Variables typically support generated functions by providing static information such
330-
// as the list of supported symbols for an enum.
331-
type VariableGen interface {
332-
// TagName returns the tag which triggers this validator.
333-
TagName() string
334-
335-
// Var returns the variable identifier.
336-
Var() PrivateVar
337-
338-
// Init generates the function call that the variable is assigned to.
339-
Init() FunctionGen
340-
}
341-
342328
// Function creates a FunctionGen for a given function name and extraArgs.
343329
func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) FunctionGen {
344330
return FunctionGen{
@@ -405,28 +391,19 @@ func (fg FunctionGen) WithComment(comment string) FunctionGen {
405391
}
406392

407393
// Variable creates a VariableGen for a given function name and extraArgs.
408-
func Variable(variable PrivateVar, init FunctionGen) VariableGen {
409-
return &variableGen{
410-
variable: variable,
411-
init: init,
394+
func Variable(variable PrivateVar, initFunc FunctionGen) *VariableGen {
395+
return &VariableGen{
396+
Variable: variable,
397+
InitFunc: initFunc,
412398
}
413399
}
414400

415-
type variableGen struct {
416-
variable PrivateVar
417-
init FunctionGen
418-
}
419-
420-
func (v variableGen) TagName() string {
421-
return v.init.TagName
422-
}
423-
424-
func (v variableGen) Var() PrivateVar {
425-
return v.variable
426-
}
401+
type VariableGen struct {
402+
// Variable holds the variable identifier.
403+
Variable PrivateVar
427404

428-
func (v variableGen) Init() FunctionGen {
429-
return v.init
405+
// InitFunc describes the function call that the variable is assigned to.
406+
InitFunc FunctionGen
430407
}
431408

432409
// WrapperFunction describes a function literal which has the fingerprint of a

0 commit comments

Comments
 (0)