Skip to content

Commit 141e98e

Browse files
thockinjpbetz
authored andcommitted
Add comments to FunctionGen
Now we can emit comments which stick to functions instead of coming before or after the functions when emitting code. For followup: I think we can simplify FunctionGen and ValidationGen
1 parent b90ff89 commit 141e98e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ func emitCallsToValidators(c *generator.Context, validations []validators.Functi
11741174
}
11751175
}
11761176

1177+
for _, comment := range v.Comments() {
1178+
sw.Do("// $.$\n", comment)
1179+
}
11771180
if isShortCircuit {
11781181
sw.Do("if e := ", nil)
11791182
emitCall()
@@ -1214,11 +1217,15 @@ func (g *genValidations) emitValidationVariables(c *generator.Context, t *types.
12141217
return cmp.Compare(a.Var().Name, b.Var().Name)
12151218
})
12161219
for _, variable := range variables {
1217-
supportInitFn, supportInitArgs := variable.Init().SignatureAndArgs()
1220+
fn := variable.Init()
1221+
supportInitFn, supportInitArgs := fn.SignatureAndArgs()
12181222
targs := generator.Args{
12191223
"varName": c.Universe.Type(types.Name(variable.Var())),
12201224
"initFn": c.Universe.Type(supportInitFn),
12211225
}
1226+
for _, comment := range fn.Comments() {
1227+
sw.Do("// $.$\n", comment)
1228+
}
12221229
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
12231230
typeArgs := variable.Init().TypeArgs()
12241231
if len(typeArgs) > 0 {
@@ -1277,6 +1284,9 @@ func toGolangSourceDataLiteral(sw *generator.SnippetWriter, c *generator.Context
12771284
targs := generator.Args{
12781285
"funcName": c.Universe.Type(fn),
12791286
}
1287+
for _, comment := range v.Function.Comments() {
1288+
sw.Do("// $.$\n", comment)
1289+
}
12801290
sw.Do("$.funcName|raw$", targs)
12811291
} else {
12821292
// If the function to be wrapped has additional arguments, we need

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ type FunctionGen interface {
302302
// Conditions returns the conditions that must true for a resource to be
303303
// validated by this function.
304304
Conditions() Conditions
305+
306+
// Comments returns optional comments that should be added to the generated
307+
// code (without the leading "//").
308+
Comments() []string
305309
}
306310

307311
// Conditions defines what conditions must be true for a resource to be validated.
@@ -356,21 +360,42 @@ func GenericFunction(tagName string, flags FunctionFlags, function types.Name, t
356360
return &functionGen{tagName: tagName, flags: flags, function: function, extraArgs: anyArgs, typeArgs: typeArgs}
357361
}
358362

363+
// WithCondition adds a condition to a FunctionGen.
359364
func WithCondition(fn FunctionGen, conditions Conditions) FunctionGen {
360365
name, args := fn.SignatureAndArgs()
361366
return &functionGen{
362-
tagName: fn.TagName(), flags: fn.Flags(), function: name, extraArgs: args, typeArgs: fn.TypeArgs(),
367+
tagName: fn.TagName(),
368+
flags: fn.Flags(),
369+
function: name,
370+
extraArgs: args,
371+
typeArgs: fn.TypeArgs(),
372+
comments: fn.Comments(),
363373
conditions: conditions,
364374
}
365375
}
366376

377+
// WithComment adds a comment to a FunctionGen.
378+
func WithComment(fn FunctionGen, comment string) FunctionGen {
379+
name, args := fn.SignatureAndArgs()
380+
return &functionGen{
381+
tagName: fn.TagName(),
382+
flags: fn.Flags(),
383+
function: name,
384+
extraArgs: args,
385+
typeArgs: fn.TypeArgs(),
386+
comments: append(fn.Comments(), comment),
387+
conditions: fn.Conditions(),
388+
}
389+
}
390+
367391
type functionGen struct {
368392
tagName string
369393
function types.Name
370394
extraArgs []any
371395
typeArgs []types.Name
372396
flags FunctionFlags
373397
conditions Conditions
398+
comments []string
374399
}
375400

376401
func (v *functionGen) TagName() string {
@@ -389,6 +414,8 @@ func (v *functionGen) Flags() FunctionFlags {
389414

390415
func (v *functionGen) Conditions() Conditions { return v.conditions }
391416

417+
func (v *functionGen) Comments() []string { return v.comments }
418+
392419
// Variable creates a VariableGen for a given function name and extraArgs.
393420
func Variable(variable PrivateVar, init FunctionGen) VariableGen {
394421
return &variableGen{

0 commit comments

Comments
 (0)