Skip to content

Commit

Permalink
feat(schema): Improve cloudformation If to accept structs (awslabs#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrn committed Apr 17, 2021
1 parent 5e2e556 commit 3c1bcd8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cloudformation/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudformation

import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -180,8 +181,19 @@ func FindInMap(mapName, topLevelKey, secondLevelKey interface{}) string {
}

// If returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false. Currently, AWS CloudFormation supports the Fn::If intrinsic function in the metadata attribute, update policy attribute, and property values in the Resources section and Outputs sections of a template. You can use the AWS::NoValue pseudo parameter as a return value to remove the corresponding property.
func If(value, ifEqual, ifNotEqual interface{}) string {
return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %q, %q ] }`, value, ifEqual, ifNotEqual))
func If(value, ifEqual interface{}, ifNotEqual interface{}) string {

equal, err := json.Marshal(ifEqual)
if err != nil {
panic(err)
}

notEqual, err := json.Marshal(ifNotEqual)
if err != nil {
panic(err)
}

return encode(fmt.Sprintf(`{ "Fn::If" : [ %q, %v, %v ] }`, value, string(equal), string(notEqual)))
}

// (str, []str) -> str
Expand Down

0 comments on commit 3c1bcd8

Please sign in to comment.