From 6cc1cd329047227674caaf1b546066e3043c6616 Mon Sep 17 00:00:00 2001 From: Marcin Rabenda Date: Sun, 11 Oct 2020 10:48:59 +0200 Subject: [PATCH] fix(intrinsics): Join function to allow to use parameters of type `List<>` (#309) * Fix Join function * Only if Fn::Ref * Decode string * New function JoinListParameter * Final implementation of Join based on type check * Fix spelling * Fix tests * Cover all test cases * Use printList --- cloudformation/intrinsics.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cloudformation/intrinsics.go b/cloudformation/intrinsics.go index ca65efac44..f95ce3a426 100644 --- a/cloudformation/intrinsics.go +++ b/cloudformation/intrinsics.go @@ -110,7 +110,7 @@ var EncoderIntrinsics = map[string]intrinsics.IntrinsicHandler{ "Fn::GetAtt": strSplit2Wrap(GetAtt), "Fn::GetAZs": strWrap(GetAZs), "Fn::ImportValue": strWrap(ImportValue), - "Fn::Join": str2AWrap(Join), + "Fn::Join": str2Wrap(Join), "Fn::Select": str2AWrap(Select), "Fn::Split": str2Wrap(Split), "Fn::Sub": strWrap(Sub), @@ -182,8 +182,18 @@ func If(value, ifEqual, ifNotEqual interface{}) string { // (str, []str) -> str // Join appends a set of values into a single value, separated by the specified delimiter. If a delimiter is the empty string, the set of values are concatenated with no delimiter. -func Join(delimiter interface{}, values []string) string { - return encode(fmt.Sprintf(`{ "Fn::Join": [ %q, [ %v ] ] }`, delimiter, printList(values))) +func Join(delimiter interface{}, value interface{}) string { + switch v := value.(type) { + case []string: + return encode(fmt.Sprintf(`{ "Fn::Join": [ %q, [ %v ] ] }`, delimiter, printList(value.([]string)))) + case []interface{}: + return encode(fmt.Sprintf(`{ "Fn::Join": [ %q, [ %v ] ] }`, delimiter, printList(interfaceAtostrA(value.([]interface{}))))) + case string: + return encode(fmt.Sprintf(`{ "Fn::Join": [ %q, %q ] }`, delimiter, value)) + default: + fmt.Printf("Unsupported type for Join: %T\n", v) + return encode(fmt.Sprintf(`{ "Fn::Join": [ %q, %q ] }`, delimiter, value)) + } } // Select returns a single object from a list of objects by index.