From 5513ac8a7d0136c125f685a243495a8055a28d79 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 8 Feb 2024 13:31:56 +1100 Subject: [PATCH] Empty TOML table is an empty object --- pkg/yqlib/decoder_toml.go | 35 +++++++++++++++++++---------------- pkg/yqlib/doc/usage/toml.md | 16 ++++++++++++++++ pkg/yqlib/toml_test.go | 2 +- release_notes.txt | 6 ++++++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/pkg/yqlib/decoder_toml.go b/pkg/yqlib/decoder_toml.go index 1ceb9c30f5c..681815966ba 100644 --- a/pkg/yqlib/decoder_toml.go +++ b/pkg/yqlib/decoder_toml.go @@ -267,29 +267,32 @@ 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 + tableNodeValue := &CandidateNode{ + Kind: MappingNode, + Tag: "!!map", + Content: make([]*CandidateNode, 0), } + var tableValue *toml.Node + runAgainstCurrentExp := false + var err error hasValue := dec.parser.NextExpression() - if !hasValue { - return false, fmt.Errorf("error retrieving table %v value: %w", fullPath, dec.parser.Error()) - } + // check to see if there is any table data + if hasValue { + tableValue = dec.parser.Expression() + // next expression is not table data, so we are done + if tableValue.Kind != toml.KeyValue { + log.Debug("got an empty table, returning") + return true, nil + } - tableNodeValue := &CandidateNode{ - Kind: MappingNode, - Tag: "!!map", + runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue) + if err != nil && !errors.Is(io.EOF, err) { + return false, err + } } - runAgainstCurrentExp, err := dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue) - log.Debugf("table node err: %w", err) - if err != nil && !errors.Is(io.EOF, err) { - return false, err - } c := Context{} - c = c.SingleChildContext(dec.rootMap) err = dec.d.DeeplyAssign(c, fullPath, tableNodeValue) if err != nil { diff --git a/pkg/yqlib/doc/usage/toml.md b/pkg/yqlib/doc/usage/toml.md index 65ed534828b..72773383ecc 100644 --- a/pkg/yqlib/doc/usage/toml.md +++ b/pkg/yqlib/doc/usage/toml.md @@ -104,3 +104,19 @@ owner: suburb: nice ``` +## Parse: Empty Table +Given a sample.toml file of: +```toml + +[dependencies] + +``` +then +```bash +yq -oy '.' sample.toml +``` +will output +```yaml +dependencies: {} +``` + diff --git a/pkg/yqlib/toml_test.go b/pkg/yqlib/toml_test.go index 9c59407356f..fcbea8a1966 100644 --- a/pkg/yqlib/toml_test.go +++ b/pkg/yqlib/toml_test.go @@ -52,7 +52,7 @@ var emptyTable = ` [dependencies] ` -var emptyTableExpected = `dependencies: []` +var emptyTableExpected = "dependencies: {}\n" var sampleWithHeader = ` [servers] diff --git a/release_notes.txt b/release_notes.txt index 2e3b8673fb7..d94bc508ba8 100644 --- a/release_notes.txt +++ b/release_notes.txt @@ -1,3 +1,9 @@ +4.40.6: + - Fix: empty TOML table #1924 - Thanks @elibroftw + - Fixed "all" error message #1845 + - Fixed to_entries[] + - Bumped dependencies + 4.40.5: - Fixing seg fault on bad XML #1888 - Fixed handling of --- #1890, #1896