|
|
|
|
@ -24,6 +24,7 @@ func init() {
|
|
|
|
|
"file": interpolationFuncFile(),
|
|
|
|
|
"format": interpolationFuncFormat(),
|
|
|
|
|
"formatlist": interpolationFuncFormatList(),
|
|
|
|
|
"index": interpolationFuncIndex(),
|
|
|
|
|
"join": interpolationFuncJoin(),
|
|
|
|
|
"length": interpolationFuncLength(),
|
|
|
|
|
"replace": interpolationFuncReplace(),
|
|
|
|
|
@ -178,6 +179,25 @@ func interpolationFuncFormatList() ast.Function {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// interpolationFuncIndex implements the "index" function that allows one to
|
|
|
|
|
// find the index of a specific element in a list
|
|
|
|
|
func interpolationFuncIndex() ast.Function {
|
|
|
|
|
return ast.Function{
|
|
|
|
|
ArgTypes: []ast.Type{ast.TypeString, ast.TypeString},
|
|
|
|
|
ReturnType: ast.TypeInt,
|
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
|
haystack := StringList(args[0].(string)).Slice()
|
|
|
|
|
needle := args[1].(string)
|
|
|
|
|
for index, element := range haystack {
|
|
|
|
|
if needle == element {
|
|
|
|
|
return index, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("Could not find '%s' in '%s'", needle, haystack)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// interpolationFuncJoin implements the "join" function that allows
|
|
|
|
|
// multi-variable values to be joined by some character.
|
|
|
|
|
func interpolationFuncJoin() ast.Function {
|
|
|
|
|
|