Empty TOML table is an empty object

This commit is contained in:
Mike Farah 2024-02-08 13:31:56 +11:00
parent d4e16a413e
commit 5513ac8a7d
4 changed files with 44 additions and 19 deletions

View File

@ -267,29 +267,32 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
fullPath := dec.getFullPath(currentNode.Child()) fullPath := dec.getFullPath(currentNode.Child())
log.Debug("!!!fullpath: %v", fullPath) 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{ tableNodeValue := &CandidateNode{
Kind: MappingNode, Kind: MappingNode,
Tag: "!!map", Tag: "!!map",
Content: make([]*CandidateNode, 0),
} }
runAgainstCurrentExp, err := dec.decodeKeyValuesIntoMap(tableNodeValue, tableValue) var tableValue *toml.Node
log.Debugf("table node err: %w", err) runAgainstCurrentExp := false
if err != nil && !errors.Is(io.EOF, err) { var err error
return false, err 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 := Context{}
c = c.SingleChildContext(dec.rootMap) c = c.SingleChildContext(dec.rootMap)
err = dec.d.DeeplyAssign(c, fullPath, tableNodeValue) err = dec.d.DeeplyAssign(c, fullPath, tableNodeValue)
if err != nil { if err != nil {

View File

@ -104,3 +104,19 @@ owner:
suburb: nice suburb: nice
``` ```
## Parse: Empty Table
Given a sample.toml file of:
```toml
[dependencies]
```
then
```bash
yq -oy '.' sample.toml
```
will output
```yaml
dependencies: {}
```

View File

@ -52,7 +52,7 @@ var emptyTable = `
[dependencies] [dependencies]
` `
var emptyTableExpected = `dependencies: []` var emptyTableExpected = "dependencies: {}\n"
var sampleWithHeader = ` var sampleWithHeader = `
[servers] [servers]

View File

@ -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: 4.40.5:
- Fixing seg fault on bad XML #1888 - Fixing seg fault on bad XML #1888
- Fixed handling of --- #1890, #1896 - Fixed handling of --- #1890, #1896