mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Non-pointer FunctionGen
This commit is contained in:
parent
0b29555323
commit
6a59dcfa1d
@ -1093,11 +1093,11 @@ func (g *genValidations) emitCallToOtherTypeFunc(c *generator.Context, node *typ
|
||||
// Emitted code assumes that the value in question is always a pair of nilable
|
||||
// variables named "obj" and "oldObj", and the field path to this value is
|
||||
// named "fldPath".
|
||||
func emitCallsToValidators(c *generator.Context, validations []*validators.FunctionGen, sw *generator.SnippetWriter) {
|
||||
func emitCallsToValidators(c *generator.Context, validations []validators.FunctionGen, sw *generator.SnippetWriter) {
|
||||
// Helper func
|
||||
sort := func(in []*validators.FunctionGen) []*validators.FunctionGen {
|
||||
sooner := make([]*validators.FunctionGen, 0, len(in))
|
||||
later := make([]*validators.FunctionGen, 0, len(in))
|
||||
sort := func(in []validators.FunctionGen) []validators.FunctionGen {
|
||||
sooner := make([]validators.FunctionGen, 0, len(in))
|
||||
later := make([]validators.FunctionGen, 0, len(in))
|
||||
|
||||
for _, fg := range in {
|
||||
isShortCircuit := (fg.Flags.IsSet(validators.ShortCircuit))
|
||||
|
@ -250,8 +250,8 @@ func (evtv eachValTagValidator) getValidations(fldPath *field.Path, t *types.Typ
|
||||
|
||||
// ForEachVal returns a validation that applies a function to each element of
|
||||
// a list or map.
|
||||
func ForEachVal(fldPath *field.Path, t *types.Type, fn *FunctionGen) (Validations, error) {
|
||||
return globalEachVal.getValidations(fldPath, t, Validations{Functions: []*FunctionGen{fn}})
|
||||
func ForEachVal(fldPath *field.Path, t *types.Type, fn FunctionGen) (Validations, error) {
|
||||
return globalEachVal.getValidations(fldPath, t, Validations{Functions: []FunctionGen{fn}})
|
||||
}
|
||||
|
||||
func (evtv eachValTagValidator) getListValidations(fldPath *field.Path, t *types.Type, validations Validations) (Validations, error) {
|
||||
@ -377,8 +377,8 @@ func (ektv eachKeyTagValidator) getValidations(t *types.Type, validations Valida
|
||||
|
||||
// ForEachKey returns a validation that applies a function to each key of
|
||||
// a map.
|
||||
func ForEachKey(_ *field.Path, t *types.Type, fn *FunctionGen) (Validations, error) {
|
||||
return globalEachKey.getValidations(t, Validations{Functions: []*FunctionGen{fn}})
|
||||
func ForEachKey(_ *field.Path, t *types.Type, fn FunctionGen) (Validations, error) {
|
||||
return globalEachKey.getValidations(t, Validations{Functions: []FunctionGen{fn}})
|
||||
}
|
||||
|
||||
func (ektv eachKeyTagValidator) Docs() TagDoc {
|
||||
|
@ -96,11 +96,11 @@ func (rtv requirednessTagValidator) doRequired(context Context) (Validations, er
|
||||
// do manual dispatch here.
|
||||
switch context.Type.Kind {
|
||||
case types.Slice:
|
||||
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredSliceValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredSliceValidator)}}, nil
|
||||
case types.Map:
|
||||
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredMapValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredMapValidator)}}, nil
|
||||
case types.Pointer:
|
||||
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredPointerValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredPointerValidator)}}, nil
|
||||
case types.Struct:
|
||||
// The +k8s:required tag on a non-pointer struct is not supported.
|
||||
// If you encounter this error and believe you have a valid use case
|
||||
@ -109,7 +109,7 @@ func (rtv requirednessTagValidator) doRequired(context Context) (Validations, er
|
||||
// this behavior or provide alternative validation mechanisms.
|
||||
return Validations{}, fmt.Errorf("non-pointer structs cannot use the %q tag", requiredTagName)
|
||||
}
|
||||
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredValueValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredValueValidator)}}, nil
|
||||
}
|
||||
|
||||
var (
|
||||
@ -156,7 +156,7 @@ func (rtv requirednessTagValidator) doOptional(context Context) (Validations, er
|
||||
return Validations{}, err
|
||||
}
|
||||
for i, fn := range validations.Functions {
|
||||
validations.Functions[i] = WithComment(fn, "optional fields with default values are effectively required")
|
||||
validations.Functions[i] = fn.WithComment("optional fields with default values are effectively required")
|
||||
}
|
||||
return validations, nil
|
||||
}
|
||||
@ -167,11 +167,11 @@ func (rtv requirednessTagValidator) doOptional(context Context) (Validations, er
|
||||
// do manual dispatch here.
|
||||
switch context.Type.Kind {
|
||||
case types.Slice:
|
||||
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalSliceValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalSliceValidator)}}, nil
|
||||
case types.Map:
|
||||
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalMapValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalMapValidator)}}, nil
|
||||
case types.Pointer:
|
||||
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalPointerValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalPointerValidator)}}, nil
|
||||
case types.Struct:
|
||||
// The +k8s:optional tag on a non-pointer struct is not supported.
|
||||
// If you encounter this error and believe you have a valid use case
|
||||
@ -180,7 +180,7 @@ func (rtv requirednessTagValidator) doOptional(context Context) (Validations, er
|
||||
// this behavior or provide alternative validation mechanisms.
|
||||
return Validations{}, fmt.Errorf("non-pointer structs cannot use the %q tag", optionalTagName)
|
||||
}
|
||||
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalValueValidator)}}, nil
|
||||
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalValueValidator)}}, nil
|
||||
}
|
||||
|
||||
// hasZeroDefault returns whether the field has a default value and whether
|
||||
@ -268,21 +268,21 @@ func (requirednessTagValidator) doForbidden(context Context) (Validations, error
|
||||
switch context.Type.Kind {
|
||||
case types.Slice:
|
||||
return Validations{
|
||||
Functions: []*FunctionGen{
|
||||
Functions: []FunctionGen{
|
||||
Function(forbiddenTagName, ShortCircuit, forbiddenSliceValidator),
|
||||
Function(forbiddenTagName, ShortCircuit|NonError, optionalSliceValidator),
|
||||
},
|
||||
}, nil
|
||||
case types.Map:
|
||||
return Validations{
|
||||
Functions: []*FunctionGen{
|
||||
Functions: []FunctionGen{
|
||||
Function(forbiddenTagName, ShortCircuit, forbiddenMapValidator),
|
||||
Function(forbiddenTagName, ShortCircuit|NonError, optionalMapValidator),
|
||||
},
|
||||
}, nil
|
||||
case types.Pointer:
|
||||
return Validations{
|
||||
Functions: []*FunctionGen{
|
||||
Functions: []FunctionGen{
|
||||
Function(forbiddenTagName, ShortCircuit, forbiddenPointerValidator),
|
||||
Function(forbiddenTagName, ShortCircuit|NonError, optionalPointerValidator),
|
||||
},
|
||||
@ -296,7 +296,7 @@ func (requirednessTagValidator) doForbidden(context Context) (Validations, error
|
||||
return Validations{}, fmt.Errorf("non-pointer structs cannot use the %q tag", forbiddenTagName)
|
||||
}
|
||||
return Validations{
|
||||
Functions: []*FunctionGen{
|
||||
Functions: []FunctionGen{
|
||||
Function(forbiddenTagName, ShortCircuit, forbiddenValueValidator),
|
||||
Function(forbiddenTagName, ShortCircuit|NonError, optionalValueValidator),
|
||||
},
|
||||
|
@ -228,7 +228,7 @@ type Validations struct {
|
||||
// Args[N] <Args[N]Type>)
|
||||
//
|
||||
// The standard arguments are not included in the FunctionGen.Args list.
|
||||
Functions []*FunctionGen
|
||||
Functions []FunctionGen
|
||||
|
||||
// Variables holds any variables which must be generated to perform
|
||||
// validation. Variables are not permitted in every context.
|
||||
@ -259,7 +259,7 @@ func (v *Validations) Len() int {
|
||||
return len(v.Functions) + len(v.Variables) + len(v.Comments)
|
||||
}
|
||||
|
||||
func (v *Validations) AddFunction(f *FunctionGen) {
|
||||
func (v *Validations) AddFunction(f FunctionGen) {
|
||||
v.Functions = append(v.Functions, f)
|
||||
}
|
||||
|
||||
@ -336,12 +336,12 @@ type VariableGen interface {
|
||||
Var() PrivateVar
|
||||
|
||||
// Init generates the function call that the variable is assigned to.
|
||||
Init() *FunctionGen
|
||||
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{
|
||||
func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) FunctionGen {
|
||||
return FunctionGen{
|
||||
TagName: tagName,
|
||||
Flags: flags,
|
||||
Function: function,
|
||||
@ -386,26 +386,26 @@ type FunctionGen struct {
|
||||
Comments []string
|
||||
}
|
||||
|
||||
// WithTypeArgs sets the type arguments for a FunctionGen.
|
||||
func (fg *FunctionGen) WithTypeArgs(typeArgs ...types.Name) *FunctionGen {
|
||||
// WithTypeArgs returns a derived FunctionGen with type arguments.
|
||||
func (fg FunctionGen) WithTypeArgs(typeArgs ...types.Name) FunctionGen {
|
||||
fg.TypeArgs = typeArgs
|
||||
return fg
|
||||
}
|
||||
|
||||
// WithConditions sets the conditions for a FunctionGen.
|
||||
func (fg *FunctionGen) WithConditions(conditions Conditions) *FunctionGen {
|
||||
// WithConditions returns a derived FunctionGen with conditions.
|
||||
func (fg FunctionGen) WithConditions(conditions Conditions) FunctionGen {
|
||||
fg.Conditions = conditions
|
||||
return fg
|
||||
}
|
||||
|
||||
// AddComment adds a comment to a FunctionGen.
|
||||
func (fg *FunctionGen) AddComment(comment string) *FunctionGen {
|
||||
// WithComment returns a new FunctionGen with a comment.
|
||||
func (fg FunctionGen) WithComment(comment string) FunctionGen {
|
||||
fg.Comments = append(fg.Comments, comment)
|
||||
return fg
|
||||
}
|
||||
|
||||
// Variable creates a VariableGen for a given function name and extraArgs.
|
||||
func Variable(variable PrivateVar, init *FunctionGen) VariableGen {
|
||||
func Variable(variable PrivateVar, init FunctionGen) VariableGen {
|
||||
return &variableGen{
|
||||
variable: variable,
|
||||
init: init,
|
||||
@ -414,7 +414,7 @@ func Variable(variable PrivateVar, init *FunctionGen) VariableGen {
|
||||
|
||||
type variableGen struct {
|
||||
variable PrivateVar
|
||||
init *FunctionGen
|
||||
init FunctionGen
|
||||
}
|
||||
|
||||
func (v variableGen) TagName() string {
|
||||
@ -425,7 +425,7 @@ func (v variableGen) Var() PrivateVar {
|
||||
return v.variable
|
||||
}
|
||||
|
||||
func (v variableGen) Init() *FunctionGen {
|
||||
func (v variableGen) Init() FunctionGen {
|
||||
return v.init
|
||||
}
|
||||
|
||||
@ -433,7 +433,7 @@ func (v variableGen) Init() *FunctionGen {
|
||||
// regular validation function (op, fldPath, obj, oldObj) and calls another
|
||||
// validation function with the same signature, plus extra args if needed.
|
||||
type WrapperFunction struct {
|
||||
Function *FunctionGen
|
||||
Function FunctionGen
|
||||
ObjType *types.Type
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user