lang: interfaces, funcs: Implement fmt.Stringer for functions
This adds the requirement that all function implementations provider a String() string method so that these can be used as vertices in the pgraph library. If we eventually move to generics for the pgraph DAG, then this might not matter, but it's not bad that these have names either.
This commit is contained in:
@@ -27,15 +27,18 @@ import (
|
||||
"github.com/purpleidea/mgmt/util/errwrap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// FIXME: should this be named sprintf instead?
|
||||
funcs.ModuleRegister(ModuleName, "printf", func() interfaces.Func { return &PrintfFunc{} })
|
||||
}
|
||||
|
||||
const (
|
||||
// PrintfFuncName is the name this function is registered as.
|
||||
// FIXME: should this be named sprintf instead?
|
||||
PrintfFuncName = "printf"
|
||||
|
||||
formatArgName = "format" // name of the first arg
|
||||
)
|
||||
|
||||
func init() {
|
||||
funcs.ModuleRegister(ModuleName, PrintfFuncName, func() interfaces.Func { return &PrintfFunc{} })
|
||||
}
|
||||
|
||||
// PrintfFunc is a static polymorphic function that compiles a format string and
|
||||
// returns the output as a string. It bases its output on the values passed in
|
||||
// to it. It examines the type of the arguments at compile time and then
|
||||
@@ -56,6 +59,12 @@ type PrintfFunc struct {
|
||||
closeChan chan struct{}
|
||||
}
|
||||
|
||||
// String returns a simple name for this function. This is needed so this struct
|
||||
// can satisfy the pgraph.Vertex interface.
|
||||
func (obj *PrintfFunc) String() string {
|
||||
return PrintfFuncName
|
||||
}
|
||||
|
||||
// ArgGen returns the Nth arg name for this function.
|
||||
func (obj *PrintfFunc) ArgGen(index int) (string, error) {
|
||||
if index == 0 {
|
||||
|
||||
Reference in New Issue
Block a user