mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 18:24:07 +00:00
Refactor VariableGen - no interface needed
This commit is contained in:
parent
6a59dcfa1d
commit
4e3d114c26
@ -1211,21 +1211,20 @@ func (g *genValidations) emitValidationVariables(c *generator.Context, t *types.
|
|||||||
tn := g.discovered.typeNodes[t]
|
tn := g.discovered.typeNodes[t]
|
||||||
|
|
||||||
variables := tn.typeValidations.Variables
|
variables := tn.typeValidations.Variables
|
||||||
slices.SortFunc(variables, func(a, b validators.VariableGen) int {
|
slices.SortFunc(variables, func(a, b *validators.VariableGen) int {
|
||||||
return cmp.Compare(a.Var().Name, b.Var().Name)
|
return cmp.Compare(a.Variable.Name, b.Variable.Name)
|
||||||
})
|
})
|
||||||
for _, variable := range variables {
|
for _, variable := range variables {
|
||||||
fn := variable.Init()
|
fn := variable.InitFunc
|
||||||
targs := generator.Args{
|
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),
|
"initFn": c.Universe.Type(fn.Function),
|
||||||
}
|
}
|
||||||
for _, comment := range fn.Comments {
|
for _, comment := range fn.Comments {
|
||||||
sw.Do("// $.$\n", comment)
|
sw.Do("// $.$\n", comment)
|
||||||
}
|
}
|
||||||
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
|
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
|
||||||
typeArgs := variable.Init().TypeArgs
|
if typeArgs := fn.TypeArgs; len(typeArgs) > 0 {
|
||||||
if len(typeArgs) > 0 {
|
|
||||||
sw.Do("[", nil)
|
sw.Do("[", nil)
|
||||||
for i, typeArg := range typeArgs {
|
for i, typeArg := range typeArgs {
|
||||||
sw.Do("$.|raw$", c.Universe.Type(typeArg))
|
sw.Do("$.|raw$", c.Universe.Type(typeArg))
|
||||||
|
@ -232,7 +232,7 @@ type Validations struct {
|
|||||||
|
|
||||||
// Variables holds any variables which must be generated to perform
|
// Variables holds any variables which must be generated to perform
|
||||||
// validation. Variables are not permitted in every context.
|
// validation. Variables are not permitted in every context.
|
||||||
Variables []VariableGen
|
Variables []*VariableGen
|
||||||
|
|
||||||
// Comments holds comments to emit (without the leanding "//").
|
// Comments holds comments to emit (without the leanding "//").
|
||||||
Comments []string
|
Comments []string
|
||||||
@ -263,7 +263,7 @@ func (v *Validations) AddFunction(f FunctionGen) {
|
|||||||
v.Functions = append(v.Functions, f)
|
v.Functions = append(v.Functions, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Validations) AddVariable(variable VariableGen) {
|
func (v *Validations) AddVariable(variable *VariableGen) {
|
||||||
v.Variables = append(v.Variables, variable)
|
v.Variables = append(v.Variables, variable)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,20 +325,6 @@ type Identifier types.Name
|
|||||||
// PrivateVars are generated using the PrivateNamer strategy.
|
// PrivateVars are generated using the PrivateNamer strategy.
|
||||||
type PrivateVar types.Name
|
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.
|
// Function creates a FunctionGen for a given function name and extraArgs.
|
||||||
func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) FunctionGen {
|
func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) FunctionGen {
|
||||||
return 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.
|
// Variable creates a VariableGen for a given function name and extraArgs.
|
||||||
func Variable(variable PrivateVar, init FunctionGen) VariableGen {
|
func Variable(variable PrivateVar, initFunc FunctionGen) *VariableGen {
|
||||||
return &variableGen{
|
return &VariableGen{
|
||||||
variable: variable,
|
Variable: variable,
|
||||||
init: init,
|
InitFunc: initFunc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type variableGen struct {
|
type VariableGen struct {
|
||||||
variable PrivateVar
|
// Variable holds the variable identifier.
|
||||||
init FunctionGen
|
Variable PrivateVar
|
||||||
}
|
|
||||||
|
|
||||||
func (v variableGen) TagName() string {
|
// InitFunc describes the function call that the variable is assigned to.
|
||||||
return v.init.TagName
|
InitFunc FunctionGen
|
||||||
}
|
|
||||||
|
|
||||||
func (v variableGen) Var() PrivateVar {
|
|
||||||
return v.variable
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v variableGen) Init() FunctionGen {
|
|
||||||
return v.init
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapperFunction describes a function literal which has the fingerprint of a
|
// WrapperFunction describes a function literal which has the fingerprint of a
|
||||||
|
Loading…
Reference in New Issue
Block a user