Skip to content

Commit 6a59dcf

Browse files
committed
Non-pointer FunctionGen
1 parent 0b29555 commit 6a59dcf

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,11 +1093,11 @@ func (g *genValidations) emitCallToOtherTypeFunc(c *generator.Context, node *typ
10931093
// Emitted code assumes that the value in question is always a pair of nilable
10941094
// variables named "obj" and "oldObj", and the field path to this value is
10951095
// named "fldPath".
1096-
func emitCallsToValidators(c *generator.Context, validations []*validators.FunctionGen, sw *generator.SnippetWriter) {
1096+
func emitCallsToValidators(c *generator.Context, validations []validators.FunctionGen, sw *generator.SnippetWriter) {
10971097
// Helper func
1098-
sort := func(in []*validators.FunctionGen) []*validators.FunctionGen {
1099-
sooner := make([]*validators.FunctionGen, 0, len(in))
1100-
later := make([]*validators.FunctionGen, 0, len(in))
1098+
sort := func(in []validators.FunctionGen) []validators.FunctionGen {
1099+
sooner := make([]validators.FunctionGen, 0, len(in))
1100+
later := make([]validators.FunctionGen, 0, len(in))
11011101

11021102
for _, fg := range in {
11031103
isShortCircuit := (fg.Flags.IsSet(validators.ShortCircuit))

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ func (evtv eachValTagValidator) getValidations(fldPath *field.Path, t *types.Typ
250250

251251
// ForEachVal returns a validation that applies a function to each element of
252252
// a list or map.
253-
func ForEachVal(fldPath *field.Path, t *types.Type, fn *FunctionGen) (Validations, error) {
254-
return globalEachVal.getValidations(fldPath, t, Validations{Functions: []*FunctionGen{fn}})
253+
func ForEachVal(fldPath *field.Path, t *types.Type, fn FunctionGen) (Validations, error) {
254+
return globalEachVal.getValidations(fldPath, t, Validations{Functions: []FunctionGen{fn}})
255255
}
256256

257257
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
377377

378378
// ForEachKey returns a validation that applies a function to each key of
379379
// a map.
380-
func ForEachKey(_ *field.Path, t *types.Type, fn *FunctionGen) (Validations, error) {
381-
return globalEachKey.getValidations(t, Validations{Functions: []*FunctionGen{fn}})
380+
func ForEachKey(_ *field.Path, t *types.Type, fn FunctionGen) (Validations, error) {
381+
return globalEachKey.getValidations(t, Validations{Functions: []FunctionGen{fn}})
382382
}
383383

384384
func (ektv eachKeyTagValidator) Docs() TagDoc {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ func (rtv requirednessTagValidator) doRequired(context Context) (Validations, er
9696
// do manual dispatch here.
9797
switch context.Type.Kind {
9898
case types.Slice:
99-
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredSliceValidator)}}, nil
99+
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredSliceValidator)}}, nil
100100
case types.Map:
101-
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredMapValidator)}}, nil
101+
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredMapValidator)}}, nil
102102
case types.Pointer:
103-
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredPointerValidator)}}, nil
103+
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredPointerValidator)}}, nil
104104
case types.Struct:
105105
// The +k8s:required tag on a non-pointer struct is not supported.
106106
// 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
109109
// this behavior or provide alternative validation mechanisms.
110110
return Validations{}, fmt.Errorf("non-pointer structs cannot use the %q tag", requiredTagName)
111111
}
112-
return Validations{Functions: []*FunctionGen{Function(requiredTagName, ShortCircuit, requiredValueValidator)}}, nil
112+
return Validations{Functions: []FunctionGen{Function(requiredTagName, ShortCircuit, requiredValueValidator)}}, nil
113113
}
114114

115115
var (
@@ -156,7 +156,7 @@ func (rtv requirednessTagValidator) doOptional(context Context) (Validations, er
156156
return Validations{}, err
157157
}
158158
for i, fn := range validations.Functions {
159-
validations.Functions[i] = WithComment(fn, "optional fields with default values are effectively required")
159+
validations.Functions[i] = fn.WithComment("optional fields with default values are effectively required")
160160
}
161161
return validations, nil
162162
}
@@ -167,11 +167,11 @@ func (rtv requirednessTagValidator) doOptional(context Context) (Validations, er
167167
// do manual dispatch here.
168168
switch context.Type.Kind {
169169
case types.Slice:
170-
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalSliceValidator)}}, nil
170+
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalSliceValidator)}}, nil
171171
case types.Map:
172-
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalMapValidator)}}, nil
172+
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalMapValidator)}}, nil
173173
case types.Pointer:
174-
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalPointerValidator)}}, nil
174+
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalPointerValidator)}}, nil
175175
case types.Struct:
176176
// The +k8s:optional tag on a non-pointer struct is not supported.
177177
// 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
180180
// this behavior or provide alternative validation mechanisms.
181181
return Validations{}, fmt.Errorf("non-pointer structs cannot use the %q tag", optionalTagName)
182182
}
183-
return Validations{Functions: []*FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalValueValidator)}}, nil
183+
return Validations{Functions: []FunctionGen{Function(optionalTagName, ShortCircuit|NonError, optionalValueValidator)}}, nil
184184
}
185185

186186
// hasZeroDefault returns whether the field has a default value and whether
@@ -268,21 +268,21 @@ func (requirednessTagValidator) doForbidden(context Context) (Validations, error
268268
switch context.Type.Kind {
269269
case types.Slice:
270270
return Validations{
271-
Functions: []*FunctionGen{
271+
Functions: []FunctionGen{
272272
Function(forbiddenTagName, ShortCircuit, forbiddenSliceValidator),
273273
Function(forbiddenTagName, ShortCircuit|NonError, optionalSliceValidator),
274274
},
275275
}, nil
276276
case types.Map:
277277
return Validations{
278-
Functions: []*FunctionGen{
278+
Functions: []FunctionGen{
279279
Function(forbiddenTagName, ShortCircuit, forbiddenMapValidator),
280280
Function(forbiddenTagName, ShortCircuit|NonError, optionalMapValidator),
281281
},
282282
}, nil
283283
case types.Pointer:
284284
return Validations{
285-
Functions: []*FunctionGen{
285+
Functions: []FunctionGen{
286286
Function(forbiddenTagName, ShortCircuit, forbiddenPointerValidator),
287287
Function(forbiddenTagName, ShortCircuit|NonError, optionalPointerValidator),
288288
},
@@ -296,7 +296,7 @@ func (requirednessTagValidator) doForbidden(context Context) (Validations, error
296296
return Validations{}, fmt.Errorf("non-pointer structs cannot use the %q tag", forbiddenTagName)
297297
}
298298
return Validations{
299-
Functions: []*FunctionGen{
299+
Functions: []FunctionGen{
300300
Function(forbiddenTagName, ShortCircuit, forbiddenValueValidator),
301301
Function(forbiddenTagName, ShortCircuit|NonError, optionalValueValidator),
302302
},

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ type Validations struct {
228228
// Args[N] <Args[N]Type>)
229229
//
230230
// The standard arguments are not included in the FunctionGen.Args list.
231-
Functions []*FunctionGen
231+
Functions []FunctionGen
232232

233233
// Variables holds any variables which must be generated to perform
234234
// validation. Variables are not permitted in every context.
@@ -259,7 +259,7 @@ func (v *Validations) Len() int {
259259
return len(v.Functions) + len(v.Variables) + len(v.Comments)
260260
}
261261

262-
func (v *Validations) AddFunction(f *FunctionGen) {
262+
func (v *Validations) AddFunction(f FunctionGen) {
263263
v.Functions = append(v.Functions, f)
264264
}
265265

@@ -336,12 +336,12 @@ type VariableGen interface {
336336
Var() PrivateVar
337337

338338
// Init generates the function call that the variable is assigned to.
339-
Init() *FunctionGen
339+
Init() FunctionGen
340340
}
341341

342342
// Function creates a FunctionGen for a given function name and extraArgs.
343-
func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) *FunctionGen {
344-
return &FunctionGen{
343+
func Function(tagName string, flags FunctionFlags, function types.Name, extraArgs ...any) FunctionGen {
344+
return FunctionGen{
345345
TagName: tagName,
346346
Flags: flags,
347347
Function: function,
@@ -386,26 +386,26 @@ type FunctionGen struct {
386386
Comments []string
387387
}
388388

389-
// WithTypeArgs sets the type arguments for a FunctionGen.
390-
func (fg *FunctionGen) WithTypeArgs(typeArgs ...types.Name) *FunctionGen {
389+
// WithTypeArgs returns a derived FunctionGen with type arguments.
390+
func (fg FunctionGen) WithTypeArgs(typeArgs ...types.Name) FunctionGen {
391391
fg.TypeArgs = typeArgs
392392
return fg
393393
}
394394

395-
// WithConditions sets the conditions for a FunctionGen.
396-
func (fg *FunctionGen) WithConditions(conditions Conditions) *FunctionGen {
395+
// WithConditions returns a derived FunctionGen with conditions.
396+
func (fg FunctionGen) WithConditions(conditions Conditions) FunctionGen {
397397
fg.Conditions = conditions
398398
return fg
399399
}
400400

401-
// AddComment adds a comment to a FunctionGen.
402-
func (fg *FunctionGen) AddComment(comment string) *FunctionGen {
401+
// WithComment returns a new FunctionGen with a comment.
402+
func (fg FunctionGen) WithComment(comment string) FunctionGen {
403403
fg.Comments = append(fg.Comments, comment)
404404
return fg
405405
}
406406

407407
// Variable creates a VariableGen for a given function name and extraArgs.
408-
func Variable(variable PrivateVar, init *FunctionGen) VariableGen {
408+
func Variable(variable PrivateVar, init FunctionGen) VariableGen {
409409
return &variableGen{
410410
variable: variable,
411411
init: init,
@@ -414,7 +414,7 @@ func Variable(variable PrivateVar, init *FunctionGen) VariableGen {
414414

415415
type variableGen struct {
416416
variable PrivateVar
417-
init *FunctionGen
417+
init FunctionGen
418418
}
419419

420420
func (v variableGen) TagName() string {
@@ -425,15 +425,15 @@ func (v variableGen) Var() PrivateVar {
425425
return v.variable
426426
}
427427

428-
func (v variableGen) Init() *FunctionGen {
428+
func (v variableGen) Init() FunctionGen {
429429
return v.init
430430
}
431431

432432
// WrapperFunction describes a function literal which has the fingerprint of a
433433
// regular validation function (op, fldPath, obj, oldObj) and calls another
434434
// validation function with the same signature, plus extra args if needed.
435435
type WrapperFunction struct {
436-
Function *FunctionGen
436+
Function FunctionGen
437437
ObjType *types.Type
438438
}
439439

0 commit comments

Comments
 (0)