Skip to content

Commit

Permalink
feat(intrinsics): Add SubVars to Sub with replacement variables (awsl…
Browse files Browse the repository at this point in the history
  • Loading branch information
mheffner committed Nov 22, 2021
1 parent c9616b1 commit 0940790
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cloudformation/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ func Sub(value interface{}) string {
return encode(fmt.Sprintf(`{ "Fn::Sub" : %q }`, value))
}

// SubVars works like Sub(), except it accepts a map of variable values to replace
func SubVars(value interface{}, variables map[string]interface{}) string {
pairs := make([]string, 0, len(variables))
for key, val := range variables {
pairs = append(pairs, fmt.Sprintf(`%q : %q`, key, val))
}

return encode(fmt.Sprintf(`{ "Fn::Sub" : [ %q, { %s } ] }`, value, strings.Join(pairs, ",")))
}

// (str, str) -> str

// GetAtt returns the value of an attribute from a resource in the template.
Expand Down
12 changes: 12 additions & 0 deletions goformation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,18 @@ var _ = Describe("Goformation", func() {
"Fn::Sub": "test-sub",
},
},
{
Name: "Fn::SubVars",
Input: cloudformation.SubVars("test-sub", map[string]interface{}{"foo": "bar"}),
Expected: map[string]interface{}{
"Fn::Sub": []interface{}{
"test-sub",
map[string]interface{}{
"foo": "bar",
},
},
},
},
{
Name: "Fn::And",
Input: cloudformation.And([]string{"test-and-first", "test-and-second", "test-and-third"}),
Expand Down

0 comments on commit 0940790

Please sign in to comment.