Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to find the index of an element in a list. #2704

Merged
merged 1 commit into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
"file": interpolationFuncFile(),
"format": interpolationFuncFormat(),
"formatlist": interpolationFuncFormatList(),
"index": interpolationFuncIndex(),
"join": interpolationFuncJoin(),
"length": interpolationFuncLength(),
"replace": interpolationFuncReplace(),
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ func TestInterpolateFuncFormatList(t *testing.T) {
})
}

func TestInterpolateFuncIndex(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${index("test", "")}`,
nil,
true,
},

{
fmt.Sprintf(`${index("%s", "foo")}`,
NewStringList([]string{"notfoo", "stillnotfoo", "bar"}).String()),
nil,
true,
},

{
fmt.Sprintf(`${index("%s", "foo")}`,
NewStringList([]string{"foo"}).String()),
"0",
false,
},

{
fmt.Sprintf(`${index("%s", "bar")}`,
NewStringList([]string{"foo", "spam", "bar", "eggs"}).String()),
"2",
false,
},
},
})
}

func TestInterpolateFuncJoin(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ The supported built-in functions are:
`formatlist("instance %v has private ip %v", aws_instance.foo.*.id, aws_instance.foo.*.private_ip)`.
Passing lists with different lengths to formatlist results in an error.

* `index(list, elem)` - Finds the index of a given element in a list. Example:
`index(aws_instance.foo.*.tags.Name, "foo-test")`

* `join(delim, list)` - Joins the list with the delimiter. A list is
only possible with splat variables from resources with a count
greater than one. Example: `join(",", aws_instance.foo.*.id)`
Expand Down