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:
James Shubin
2023-03-03 14:12:09 -05:00
parent 8366cf0873
commit 5d664855de
35 changed files with 365 additions and 39 deletions

View File

@@ -26,14 +26,17 @@ import (
"github.com/purpleidea/mgmt/lang/types"
)
const (
// AbsPathFuncName is the name this function is registered as.
AbsPathFuncName = "abspath"
pathArg = "path"
)
func init() {
funcs.ModuleRegister(ModuleName, "abspath", func() interfaces.Func { return &AbsPathFunc{} }) // must register the func and name
}
const (
pathArg = "path"
)
// AbsPathFunc is a function that returns the absolute, full path in the deploy
// from an input path that is relative to the calling file. If you pass it an
// empty string, you'll just get the absolute deploy directory path that you're
@@ -49,6 +52,12 @@ type AbsPathFunc 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 *AbsPathFunc) String() string {
return AbsPathFuncName
}
// SetData is used by the language to pass our function some code-level context.
func (obj *AbsPathFunc) SetData(data *interfaces.FuncData) {
obj.data = data