mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-03 10:55:36 +00:00
Preserve empty TOML arrays in tables (#2686)
Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com>
This commit is contained in:
parent
1a433d1035
commit
cfe2eee7e6
@ -195,6 +195,18 @@ func (te *tomlEncoder) encodeTopLevelEntry(w io.Writer, path []string, node *Can
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isTomlArrayOfTables(seq *CandidateNode) bool {
|
||||||
|
if len(seq.Content) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, it := range seq.Content {
|
||||||
|
if it.Kind != MappingNode {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (te *tomlEncoder) writeAttribute(w io.Writer, key string, value *CandidateNode) error {
|
func (te *tomlEncoder) writeAttribute(w io.Writer, key string, value *CandidateNode) error {
|
||||||
te.wroteRootAttr = true // Mark that we wrote a root attribute
|
te.wroteRootAttr = true // Mark that we wrote a root attribute
|
||||||
|
|
||||||
@ -462,15 +474,7 @@ func (te *tomlEncoder) encodeSeparateMapping(w io.Writer, path []string, m *Cand
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if v.Kind == SequenceNode {
|
if v.Kind == SequenceNode {
|
||||||
// Check if it's NOT an array of tables
|
if !isTomlArrayOfTables(v) {
|
||||||
allMaps := true
|
|
||||||
for _, it := range v.Content {
|
|
||||||
if it.Kind != MappingNode {
|
|
||||||
allMaps = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !allMaps {
|
|
||||||
hasAttrs = true
|
hasAttrs = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -500,14 +504,7 @@ func (te *tomlEncoder) encodeSeparateMapping(w io.Writer, path []string, m *Cand
|
|||||||
}
|
}
|
||||||
case SequenceNode:
|
case SequenceNode:
|
||||||
// If sequence of maps, emit [[path.k]] per element
|
// If sequence of maps, emit [[path.k]] per element
|
||||||
allMaps := true
|
if isTomlArrayOfTables(v) {
|
||||||
for _, it := range v.Content {
|
|
||||||
if it.Kind != MappingNode {
|
|
||||||
allMaps = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if allMaps {
|
|
||||||
key := tomlDottedKey(append(append([]string{}, path...), k))
|
key := tomlDottedKey(append(append([]string{}, path...), k))
|
||||||
for _, it := range v.Content {
|
for _, it := range v.Content {
|
||||||
if _, err := w.Write([]byte("[[" + key + "]]\n")); err != nil {
|
if _, err := w.Write([]byte("[[" + key + "]]\n")); err != nil {
|
||||||
@ -545,14 +542,7 @@ func (te *tomlEncoder) encodeMappingBodyWithPath(w io.Writer, path []string, m *
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case SequenceNode:
|
case SequenceNode:
|
||||||
allMaps := true
|
if !isTomlArrayOfTables(v) {
|
||||||
for _, it := range v.Content {
|
|
||||||
if it.Kind != MappingNode {
|
|
||||||
allMaps = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !allMaps {
|
|
||||||
if err := te.writeArrayAttribute(w, k, v); err != nil {
|
if err := te.writeArrayAttribute(w, k, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -565,14 +555,7 @@ func (te *tomlEncoder) encodeMappingBodyWithPath(w io.Writer, path []string, m *
|
|||||||
k := m.Content[i].Value
|
k := m.Content[i].Value
|
||||||
v := m.Content[i+1]
|
v := m.Content[i+1]
|
||||||
if v.Kind == SequenceNode {
|
if v.Kind == SequenceNode {
|
||||||
allMaps := true
|
if isTomlArrayOfTables(v) {
|
||||||
for _, it := range v.Content {
|
|
||||||
if it.Kind != MappingNode {
|
|
||||||
allMaps = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if allMaps {
|
|
||||||
dotted := tomlDottedKey(append(append([]string{}, path...), k))
|
dotted := tomlDottedKey(append(append([]string{}, path...), k))
|
||||||
for _, it := range v.Content {
|
for _, it := range v.Content {
|
||||||
if _, err := w.Write([]byte("[[" + dotted + "]]\n")); err != nil {
|
if _, err := w.Write([]byte("[[" + dotted + "]]\n")); err != nil {
|
||||||
|
|||||||
@ -209,6 +209,34 @@ address = "12 cat st"
|
|||||||
var rtEmptyArray = `A = []
|
var rtEmptyArray = `A = []
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var rtEmptyArrayInTable = `[features]
|
||||||
|
my-feature = []
|
||||||
|
`
|
||||||
|
|
||||||
|
var rtMixedEmptyArraysInTable = `[features]
|
||||||
|
my-other-feature = []
|
||||||
|
my-feature = ["my-other-feature"]
|
||||||
|
`
|
||||||
|
|
||||||
|
var yamlEmptyArrayInTable = `features:
|
||||||
|
my-feature: []
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedTomlEmptyArrayInTable = `[features]
|
||||||
|
my-feature = []
|
||||||
|
`
|
||||||
|
|
||||||
|
var yamlMixedEmptyArraysInTable = `features:
|
||||||
|
my-other-feature: []
|
||||||
|
my-feature:
|
||||||
|
- my-other-feature
|
||||||
|
`
|
||||||
|
|
||||||
|
var expectedTomlMixedEmptyArraysInTable = `[features]
|
||||||
|
my-other-feature = []
|
||||||
|
my-feature = ["my-other-feature"]
|
||||||
|
`
|
||||||
|
|
||||||
var rtSampleTable = `var = "x"
|
var rtSampleTable = `var = "x"
|
||||||
|
|
||||||
[owner.contact]
|
[owner.contact]
|
||||||
@ -563,6 +591,36 @@ var tomlScenarios = []formatScenario{
|
|||||||
expected: rtEmptyArray,
|
expected: rtEmptyArray,
|
||||||
scenarioType: "roundtrip",
|
scenarioType: "roundtrip",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "Issue #2674: roundtrip empty array in table",
|
||||||
|
input: rtEmptyArrayInTable,
|
||||||
|
expression: ".",
|
||||||
|
expected: rtEmptyArrayInTable,
|
||||||
|
scenarioType: "roundtrip",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "Issue #2674: roundtrip mixed empty and non-empty arrays in table",
|
||||||
|
input: rtMixedEmptyArraysInTable,
|
||||||
|
expression: ".",
|
||||||
|
expected: rtMixedEmptyArraysInTable,
|
||||||
|
scenarioType: "roundtrip",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "Issue #2674: encode empty array in table",
|
||||||
|
input: yamlEmptyArrayInTable,
|
||||||
|
expected: expectedTomlEmptyArrayInTable,
|
||||||
|
scenarioType: "encode",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "Issue #2674: encode mixed empty and non-empty arrays in table",
|
||||||
|
input: yamlMixedEmptyArraysInTable,
|
||||||
|
expected: expectedTomlMixedEmptyArraysInTable,
|
||||||
|
scenarioType: "encode",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Roundtrip: sample table",
|
description: "Roundtrip: sample table",
|
||||||
input: rtSampleTable,
|
input: rtSampleTable,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user