From d4e16a413ec570c869cc6741dd61e628570c4429 Mon Sep 17 00:00:00 2001 From: Elijah Lopez Date: Wed, 7 Feb 2024 03:59:50 -0500 Subject: [PATCH] fix: empty TOML table (#1936) --- pkg/yqlib/decoder_toml.go | 12 ++++++------ pkg/yqlib/toml_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/yqlib/decoder_toml.go b/pkg/yqlib/decoder_toml.go index d94d1bf723..1ceb9c30f5 100644 --- a/pkg/yqlib/decoder_toml.go +++ b/pkg/yqlib/decoder_toml.go @@ -267,6 +267,12 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) { fullPath := dec.getFullPath(currentNode.Child()) log.Debug("!!!fullpath: %v", fullPath) + tableValue := dec.parser.Expression() + if tableValue.Kind != toml.KeyValue { + log.Debug("got an empty table, returning") + return true, nil + } + hasValue := dec.parser.NextExpression() if !hasValue { return false, fmt.Errorf("error retrieving table %v value: %w", fullPath, dec.parser.Error()) @@ -277,12 +283,6 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) { Tag: "!!map", } - tableValue := dec.parser.Expression() - if tableValue.Kind != toml.KeyValue { - log.Debug("got an empty table, returning") - return true, nil - } - runAgainstCurrentExp, err := dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue) log.Debugf("table node err: %w", err) if err != nil && !errors.Is(io.EOF, err) { diff --git a/pkg/yqlib/toml_test.go b/pkg/yqlib/toml_test.go index bf17d6f995..9c59407356 100644 --- a/pkg/yqlib/toml_test.go +++ b/pkg/yqlib/toml_test.go @@ -48,6 +48,12 @@ var sampleArrayTableExpected = `owner: suburb: nice ` +var emptyTable = ` +[dependencies] +` + +var emptyTableExpected = `dependencies: []` + var sampleWithHeader = ` [servers] @@ -199,6 +205,12 @@ var tomlScenarios = []formatScenario{ expected: sampleArrayTableExpected, scenarioType: "decode", }, + { + description: "Parse: Empty Table", + input: emptyTable, + expected: emptyTableExpected, + scenarioType: "decode", + }, { description: "Parse: with header", skipDoc: true,