@@ -24,6 +24,7 @@ func init() {
2424 "file" : interpolationFuncFile (),
2525 "format" : interpolationFuncFormat (),
2626 "formatlist" : interpolationFuncFormatList (),
27+ "index" : interpolationFuncIndex (),
2728 "join" : interpolationFuncJoin (),
2829 "length" : interpolationFuncLength (),
2930 "replace" : interpolationFuncReplace (),
@@ -178,6 +179,25 @@ func interpolationFuncFormatList() ast.Function {
178179 }
179180}
180181
182+ // interpolationFuncIndex implements the "index" function that allows one to
183+ // find the index of a specific element in a list
184+ func interpolationFuncIndex () ast.Function {
185+ return ast.Function {
186+ ArgTypes : []ast.Type {ast .TypeString , ast .TypeString },
187+ ReturnType : ast .TypeInt ,
188+ Callback : func (args []interface {}) (interface {}, error ) {
189+ haystack := StringList (args [0 ].(string )).Slice ()
190+ needle := args [1 ].(string )
191+ for index , element := range haystack {
192+ if needle == element {
193+ return index , nil
194+ }
195+ }
196+ return nil , fmt .Errorf ("Could not find '%s' in '%s'" , needle , haystack )
197+ },
198+ }
199+ }
200+
181201// interpolationFuncJoin implements the "join" function that allows
182202// multi-variable values to be joined by some character.
183203func interpolationFuncJoin () ast.Function {
0 commit comments