Skip to content

Commit

Permalink
Empty TOML table is an empty object
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 8, 2024
1 parent d4e16a4 commit 5513ac8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
35 changes: 19 additions & 16 deletions pkg/yqlib/decoder_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/yqlib/doc/usage/toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
```
2 changes: 1 addition & 1 deletion pkg/yqlib/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var emptyTable = `
[dependencies]
`

var emptyTableExpected = `dependencies: []`
var emptyTableExpected = "dependencies: {}\n"

var sampleWithHeader = `
[servers]
Expand Down
6 changes: 6 additions & 0 deletions release_notes.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 5513ac8

Please sign in to comment.