Skip to content

Commit

Permalink
fix(intrinsics): Join function to allow to use parameters of type `Li…
Browse files Browse the repository at this point in the history
…st<>` (awslabs#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
  • Loading branch information
xrn committed Oct 11, 2020
1 parent 583d7ee commit 6cc1cd3
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions cloudformation/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6cc1cd3

Please sign in to comment.