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
This commit is contained in:
Tim Hockin 2025-02-21 14:15:44 -08:00 committed by Joe Betz
parent b90ff89ed6
commit 141e98ed05
2 changed files with 39 additions and 2 deletions

View File

@ -1174,6 +1174,9 @@ func emitCallsToValidators(c *generator.Context, validations []validators.Functi
}
}
for _, comment := range v.Comments() {
sw.Do("// $.$\n", comment)
}
if isShortCircuit {
sw.Do("if e := ", nil)
emitCall()
@ -1214,11 +1217,15 @@ func (g *genValidations) emitValidationVariables(c *generator.Context, t *types.
return cmp.Compare(a.Var().Name, b.Var().Name)
})
for _, variable := range variables {
supportInitFn, supportInitArgs := variable.Init().SignatureAndArgs()
fn := variable.Init()
supportInitFn, supportInitArgs := fn.SignatureAndArgs()
targs := generator.Args{
"varName": c.Universe.Type(types.Name(variable.Var())),
"initFn": c.Universe.Type(supportInitFn),
}
for _, comment := range fn.Comments() {
sw.Do("// $.$\n", comment)
}
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
typeArgs := variable.Init().TypeArgs()
if len(typeArgs) > 0 {
@ -1277,6 +1284,9 @@ func toGolangSourceDataLiteral(sw *generator.SnippetWriter, c *generator.Context
targs := generator.Args{
"funcName": c.Universe.Type(fn),
}
for _, comment := range v.Function.Comments() {
sw.Do("// $.$\n", comment)
}
sw.Do("$.funcName|raw$", targs)
} else {
// If the function to be wrapped has additional arguments, we need

View File

@ -302,6 +302,10 @@ type FunctionGen interface {
// Conditions returns the conditions that must true for a resource to be
// validated by this function.
Conditions() Conditions
// Comments returns optional comments that should be added to the generated
// code (without the leading "//").
Comments() []string
}
// Conditions defines what conditions must be true for a resource to be validated.
@ -356,14 +360,34 @@ func GenericFunction(tagName string, flags FunctionFlags, function types.Name, t
return &functionGen{tagName: tagName, flags: flags, function: function, extraArgs: anyArgs, typeArgs: typeArgs}
}
// WithCondition adds a condition to a FunctionGen.
func WithCondition(fn FunctionGen, conditions Conditions) FunctionGen {
name, args := fn.SignatureAndArgs()
return &functionGen{
tagName: fn.TagName(), flags: fn.Flags(), function: name, extraArgs: args, typeArgs: fn.TypeArgs(),
tagName: fn.TagName(),
flags: fn.Flags(),
function: name,
extraArgs: args,
typeArgs: fn.TypeArgs(),
comments: fn.Comments(),
conditions: conditions,
}
}
// WithComment adds a comment to a FunctionGen.
func WithComment(fn FunctionGen, comment string) FunctionGen {
name, args := fn.SignatureAndArgs()
return &functionGen{
tagName: fn.TagName(),
flags: fn.Flags(),
function: name,
extraArgs: args,
typeArgs: fn.TypeArgs(),
comments: append(fn.Comments(), comment),
conditions: fn.Conditions(),
}
}
type functionGen struct {
tagName string
function types.Name
@ -371,6 +395,7 @@ type functionGen struct {
typeArgs []types.Name
flags FunctionFlags
conditions Conditions
comments []string
}
func (v *functionGen) TagName() string {
@ -389,6 +414,8 @@ func (v *functionGen) Flags() FunctionFlags {
func (v *functionGen) Conditions() Conditions { return v.conditions }
func (v *functionGen) Comments() []string { return v.comments }
// Variable creates a VariableGen for a given function name and extraArgs.
func Variable(variable PrivateVar, init FunctionGen) VariableGen {
return &variableGen{