mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 18:54:06 +00:00
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:
parent
b90ff89ed6
commit
141e98ed05
@ -1174,6 +1174,9 @@ func emitCallsToValidators(c *generator.Context, validations []validators.Functi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, comment := range v.Comments() {
|
||||||
|
sw.Do("// $.$\n", comment)
|
||||||
|
}
|
||||||
if isShortCircuit {
|
if isShortCircuit {
|
||||||
sw.Do("if e := ", nil)
|
sw.Do("if e := ", nil)
|
||||||
emitCall()
|
emitCall()
|
||||||
@ -1214,11 +1217,15 @@ func (g *genValidations) emitValidationVariables(c *generator.Context, t *types.
|
|||||||
return cmp.Compare(a.Var().Name, b.Var().Name)
|
return cmp.Compare(a.Var().Name, b.Var().Name)
|
||||||
})
|
})
|
||||||
for _, variable := range variables {
|
for _, variable := range variables {
|
||||||
supportInitFn, supportInitArgs := variable.Init().SignatureAndArgs()
|
fn := variable.Init()
|
||||||
|
supportInitFn, supportInitArgs := fn.SignatureAndArgs()
|
||||||
targs := generator.Args{
|
targs := generator.Args{
|
||||||
"varName": c.Universe.Type(types.Name(variable.Var())),
|
"varName": c.Universe.Type(types.Name(variable.Var())),
|
||||||
"initFn": c.Universe.Type(supportInitFn),
|
"initFn": c.Universe.Type(supportInitFn),
|
||||||
}
|
}
|
||||||
|
for _, comment := range fn.Comments() {
|
||||||
|
sw.Do("// $.$\n", comment)
|
||||||
|
}
|
||||||
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
|
sw.Do("var $.varName|private$ = $.initFn|raw$", targs)
|
||||||
typeArgs := variable.Init().TypeArgs()
|
typeArgs := variable.Init().TypeArgs()
|
||||||
if len(typeArgs) > 0 {
|
if len(typeArgs) > 0 {
|
||||||
@ -1277,6 +1284,9 @@ func toGolangSourceDataLiteral(sw *generator.SnippetWriter, c *generator.Context
|
|||||||
targs := generator.Args{
|
targs := generator.Args{
|
||||||
"funcName": c.Universe.Type(fn),
|
"funcName": c.Universe.Type(fn),
|
||||||
}
|
}
|
||||||
|
for _, comment := range v.Function.Comments() {
|
||||||
|
sw.Do("// $.$\n", comment)
|
||||||
|
}
|
||||||
sw.Do("$.funcName|raw$", targs)
|
sw.Do("$.funcName|raw$", targs)
|
||||||
} else {
|
} else {
|
||||||
// If the function to be wrapped has additional arguments, we need
|
// If the function to be wrapped has additional arguments, we need
|
||||||
|
@ -302,6 +302,10 @@ type FunctionGen interface {
|
|||||||
// Conditions returns the conditions that must true for a resource to be
|
// Conditions returns the conditions that must true for a resource to be
|
||||||
// validated by this function.
|
// validated by this function.
|
||||||
Conditions() Conditions
|
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.
|
// 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}
|
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 {
|
func WithCondition(fn FunctionGen, conditions Conditions) FunctionGen {
|
||||||
name, args := fn.SignatureAndArgs()
|
name, args := fn.SignatureAndArgs()
|
||||||
return &functionGen{
|
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,
|
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 {
|
type functionGen struct {
|
||||||
tagName string
|
tagName string
|
||||||
function types.Name
|
function types.Name
|
||||||
@ -371,6 +395,7 @@ type functionGen struct {
|
|||||||
typeArgs []types.Name
|
typeArgs []types.Name
|
||||||
flags FunctionFlags
|
flags FunctionFlags
|
||||||
conditions Conditions
|
conditions Conditions
|
||||||
|
comments []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *functionGen) TagName() 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) Conditions() Conditions { return v.conditions }
|
||||||
|
|
||||||
|
func (v *functionGen) Comments() []string { return v.comments }
|
||||||
|
|
||||||
// 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, init FunctionGen) VariableGen {
|
||||||
return &variableGen{
|
return &variableGen{
|
||||||
|
Loading…
Reference in New Issue
Block a user