diff --git a/pkg/yqlib/decoder_toml.go b/pkg/yqlib/decoder_toml.go index 1ceb9c30..68181596 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 - } - - hasValue := dec.parser.NextExpression() - if !hasValue { - return false, fmt.Errorf("error retrieving table %v value: %w", fullPath, dec.parser.Error()) - } - tableNodeValue := &CandidateNode{ - Kind: MappingNode, - Tag: "!!map", + Kind: MappingNode, + Tag: "!!map", + Content: make([]*CandidateNode, 0), } - runAgainstCurrentExp, err := dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue) - log.Debugf("table node err: %w", err) - if err != nil && !errors.Is(io.EOF, err) { - return false, err + var tableValue *toml.Node + runAgainstCurrentExp := false + var err error + hasValue := dec.parser.NextExpression() + // 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 + } + + runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue) + 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 65ed5348..72773383 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 9c594073..fcbea8a1 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 2e3b8673..d94bc508 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