From b11b9ea4d443f67fc51943dd3d84ef43e1ec26bd Mon Sep 17 00:00:00 2001 From: Garrett Cheadle <67297250+gcheadle-vmware@users.noreply.github.com> Date: Tue, 26 Apr 2022 11:59:45 -0700 Subject: [PATCH] Bug fix: decode TOML array of tables (#652) --- pkg/orderedmap/convert.go | 9 +++++++++ pkg/yamltemplate/filetests/ytt-library/toml.tpltest | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/orderedmap/convert.go b/pkg/orderedmap/convert.go index c29db025..61ea5d1f 100644 --- a/pkg/orderedmap/convert.go +++ b/pkg/orderedmap/convert.go @@ -75,6 +75,15 @@ func (c Conversion) fromUnorderedMaps(object interface{}) interface{} { } return typedObj + // some sources (e.g. toml library) yield specific type of slices. + // slices in Go are not covariant, so each flavor of slice must have its own case, here. + // process these exactly the same way we do for generic slices (prior case) + case []map[string]interface{}: + resultArray := make([]interface{}, len(typedObj)) + for i, item := range typedObj { + resultArray[i] = c.fromUnorderedMaps(item) + } + return resultArray default: return typedObj } diff --git a/pkg/yamltemplate/filetests/ytt-library/toml.tpltest b/pkg/yamltemplate/filetests/ytt-library/toml.tpltest index 38039a14..24b7a6f4 100644 --- a/pkg/yamltemplate/filetests/ytt-library/toml.tpltest +++ b/pkg/yamltemplate/filetests/ytt-library/toml.tpltest @@ -6,6 +6,14 @@ fragment: - piece2 #@ end +#@ arrayOfTables = ''' +#@ [[stuff]] +#@ a = 1 +#@ +#@ [[stuff]] +#@ b = 2 +#@ ''' + encode: test1: #@ toml.encode({}) test2: #@ toml.encode({"a": [1,2,3,456], "b": "str"}) @@ -17,6 +25,7 @@ encode: decode: test1: #@ toml.decode("") test2: #@ toml.decode('a = [ 1, 2, 3, 456 ]\nb = "str"') + test3: #@ toml.decode(arrayOfTables) +++ @@ -55,3 +64,7 @@ decode: - 3 - 456 b: str + test3: + stuff: + - a: 1 + - b: 2