Skip to content

Commit d1dff46

Browse files
authored
fix: preserve TOML inline table array scope (#2694)
Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com>
1 parent cb97935 commit d1dff46

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

pkg/yqlib/encoder_toml.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func isTomlArrayOfTables(seq *CandidateNode) bool {
203203
return false
204204
}
205205
for _, it := range seq.Content {
206-
if it.Kind != MappingNode {
206+
if it.Kind != MappingNode || it.EncodeHint == EncodeHintInline {
207207
return false
208208
}
209209
}
@@ -535,7 +535,7 @@ func (te *tomlEncoder) encodeSeparateMapping(w io.Writer, path []string, m *Cand
535535

536536
// encodeMappingBodyWithPath encodes attributes and nested arrays of tables using full dotted path context
537537
func (te *tomlEncoder) encodeMappingBodyWithPath(w io.Writer, path []string, m *CandidateNode) error {
538-
// First, attributes (scalars and non-map arrays)
538+
// First, attributes (scalars, inline mappings, and non-map arrays)
539539
for i := 0; i < len(m.Content); i += 2 {
540540
k := m.Content[i].Value
541541
v := m.Content[i+1]
@@ -544,6 +544,12 @@ func (te *tomlEncoder) encodeMappingBodyWithPath(w io.Writer, path []string, m *
544544
if err := te.writeAttribute(w, k, v); err != nil {
545545
return err
546546
}
547+
case MappingNode:
548+
if v.EncodeHint == EncodeHintInline {
549+
if err := te.writeInlineTableAttribute(w, k, v); err != nil {
550+
return err
551+
}
552+
}
547553
case SequenceNode:
548554
if !isTomlArrayOfTables(v) {
549555
if err := te.writeArrayAttribute(w, k, v); err != nil {
@@ -572,21 +578,15 @@ func (te *tomlEncoder) encodeMappingBodyWithPath(w io.Writer, path []string, m *
572578
}
573579
}
574580

575-
// Finally, child mappings: inline-hint ones become inline table attributes,
581+
// Finally, child mappings: inline-hint ones were emitted above as attributes,
576582
// while all others are emitted as separate sub-table sections.
577583
for i := 0; i < len(m.Content); i += 2 {
578584
k := m.Content[i].Value
579585
v := m.Content[i+1]
580-
if v.Kind == MappingNode {
581-
if v.EncodeHint == EncodeHintInline {
582-
if err := te.writeInlineTableAttribute(w, k, v); err != nil {
583-
return err
584-
}
585-
} else {
586-
subPath := append(append([]string{}, path...), k)
587-
if err := te.encodeSeparateMapping(w, subPath, v); err != nil {
588-
return err
589-
}
586+
if v.Kind == MappingNode && v.EncodeHint != EncodeHintInline {
587+
subPath := append(append([]string{}, path...), k)
588+
if err := te.encodeSeparateMapping(w, subPath, v); err != nil {
589+
return err
590590
}
591591
}
592592
}

pkg/yqlib/toml_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ my-other-feature = []
237237
my-feature = ["my-other-feature"]
238238
`
239239

240+
var issue2688SampleToml = `[project]
241+
name = "some-project"
242+
version = "0.5.1"
243+
authors = [{name = "Author", email = "author@example.com"}]
244+
license = { file = "LICENSE" }
245+
readme = "README.md"
246+
`
247+
248+
var issue2688SampleExpected = `[project]
249+
name = "some-project"
250+
version = "0.5.2"
251+
authors = [{ name = "Author", email = "author@example.com" }]
252+
license = { file = "LICENSE" }
253+
readme = "README.md"
254+
`
255+
240256
var rtSampleTable = `var = "x"
241257
242258
[owner.contact]
@@ -726,6 +742,14 @@ var tomlScenarios = []formatScenario{
726742
expected: "[arg]\nhello = \"foo\"\n",
727743
scenarioType: "encode-json",
728744
},
745+
{
746+
skipDoc: true,
747+
description: "Issue 2688: inline table arrays do not change following table scope",
748+
input: issue2688SampleToml,
749+
expression: `.project.version = "0.5.2"`,
750+
expected: issue2688SampleExpected,
751+
scenarioType: "roundtrip",
752+
},
729753
{
730754
skipDoc: true,
731755
description: "Roundtrip: key with special characters in inline table",

0 commit comments

Comments
 (0)