diff --git a/convert_types.go b/cloudformation/convert_types.go similarity index 99% rename from convert_types.go rename to cloudformation/convert_types.go index 3173073ad6..cf4b3fca6c 100644 --- a/convert_types.go +++ b/cloudformation/convert_types.go @@ -1,4 +1,4 @@ -package goformation +package cloudformation import "time" diff --git a/convert_types_test.go b/cloudformation/convert_types_test.go similarity index 99% rename from convert_types_test.go rename to cloudformation/convert_types_test.go index d9728dbb86..1966b76495 100644 --- a/convert_types_test.go +++ b/cloudformation/convert_types_test.go @@ -1,4 +1,4 @@ -package goformation +package cloudformation import ( "reflect" diff --git a/cloudformation/utils/utils.go b/cloudformation/utils/utils.go new file mode 100644 index 0000000000..f220a5ae28 --- /dev/null +++ b/cloudformation/utils/utils.go @@ -0,0 +1,37 @@ +package utils + +import ( + "encoding/json" +) + +// ByJSONLength implements the sort interface and enables sorting by JSON length +type ByJSONLength []interface{} + +func (s ByJSONLength) Len() int { + return len(s) +} + +func (s ByJSONLength) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func (s ByJSONLength) Less(i, j int) bool { + // Nil is always at the end + if s[i] == nil { + return false + } + if s[j] == nil { + return true + } + jsoni, _ := json.Marshal(s[i]) + jsonj, _ := json.Marshal(s[j]) + + if jsoni == nil { + return false + } + if jsonj == nil { + return true + } + + return len(jsoni) > len(jsonj) +}