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

feat(intrinsics): support nested intrinsics #571

Merged
merged 1 commit into from
Mar 30, 2023
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
24 changes: 15 additions & 9 deletions cloudformation/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var EncoderIntrinsics = map[string]intrinsics.IntrinsicHandler{
"Fn::GetAZs": strWrap(GetAZs),
"Fn::ImportValue": strWrap(ImportValue),
"Fn::Join": str2Wrap(Join),
"Fn::Select": str2AWrap(Select),
"Fn::Select": str2Wrap(Select),
"Fn::Split": str2Wrap(Split),
"Fn::Sub": strWrap(Sub),
"Ref": strWrap(Ref),
Expand Down Expand Up @@ -159,7 +159,7 @@ func GetAZsPtr(region interface{}) *string {

// Sub substitutes variables in an input string with values that you specify. In your templates, you can use this function to construct commands or outputs that include values that aren't available until you create or update a stack.
func Sub(value interface{}) string {
return encode(fmt.Sprintf(`{ "Fn::Sub" : %q }`, value))
return encode(fmt.Sprintf(`{ "Fn::Sub" : %s }`, toJSON(value)))
}

func SubPtr(value interface{}) *string {
Expand Down Expand Up @@ -193,7 +193,7 @@ func GetAttPtr(logicalName string, attribute string) *string {

// Split splits a string into a list of string values so that you can select an element from the resulting string list, use the Fn::Split intrinsic function. Specify the location of splits with a delimiter, such as , (a comma). After you split a string, use the Fn::Select function to pick a specific element.
func Split(delimiter, source interface{}) string {
return encode(fmt.Sprintf(`{ "Fn::Split" : [ %q, %q ] }`, delimiter, source))
return encode(fmt.Sprintf(`{ "Fn::Split" : [ %q, %s ] }`, delimiter, toJSON(source)))
}

func SplitPtr(delimiter, source interface{}) *string {
Expand Down Expand Up @@ -287,14 +287,11 @@ func JoinPtr(delimiter interface{}, value interface{}) *string {
}

// Select returns a single object from a list of objects by index.
func Select(index interface{}, list []string) string {
if len(list) == 1 {
return encode(fmt.Sprintf(`{ "Fn::Select": [ %q, %q ] }`, index, list[0]))
}
return encode(fmt.Sprintf(`{ "Fn::Select": [ %q, [ %v ] ] }`, index, printList(list)))
func Select(index , list interface{}) string {
return encode(fmt.Sprintf(`{ "Fn::Select": [ %s, %s ] }`, toJSON(index), toJSON(list)))
}

func SelectPtr(index interface{}, list []string) *string {
func SelectPtr(index, list interface{}) *string {
return String(Select(index, list))
}

Expand Down Expand Up @@ -368,3 +365,12 @@ func isBase64(s string) bool {
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}

func toJSON(value interface{}) string {
j, err := json.Marshal(value)
if err != nil {
fmt.Printf("Unsupported type for intrinsic JSON: %v\n", err)
return fmt.Sprintf("%q", value)
}
return string(j)
}
22 changes: 22 additions & 0 deletions cloudformation/intrinsics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,33 @@ var _ = Describe("Goformation", func() {
Input: `Description: !Join [a, [b, c]]`,
Expected: `{"Description":{"Fn::Join":["a",["b","c"]]}}`,
},
{
Name: "Join Nested",
Input: `Description:
Fn::Join:
- ''
- - !Sub '${AWS::Region}'
- Fn::If:
- 'A'
- 'A1'
- !ImportValue 'B'`,
Expected: `{"Description":{"Fn::Join":["",[{"Fn::Sub":"${AWS::Region}"},{"Fn::If":["A","A1",{"Fn::ImportValue":"B"}]}]]}}`,
},
{
Name: "Select",
Input: `Description: !Select [a, [b, c]]`,
Expected: `{"Description":{"Fn::Select":["a",["b","c"]]}}`,
},
{
Name: "Select Nested",
Input: `Description:
Fn::Select:
- 2
- Fn::Split:
- '/'
- !GetAtt: 'site.url'`,
Expected: `{"Description":{"Fn::Select":[2,{"Fn::Split":["/","site.url"]}]}}`,
},
{
Name: "And",
Input: `Description: !And [a, b, c]`,
Expand Down