Skip to content

Commit eb4fde4

Browse files
committed
Pulling out common code
1 parent 06ea4cf commit eb4fde4

1 file changed

Lines changed: 18 additions & 24 deletions

File tree

pkg/yqlib/decoder_toml.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ func (dec *tomlDecoder) Init(reader io.Reader) error {
4747
return nil
4848
}
4949

50+
func (dec *tomlDecoder) attachOrphanedCommentsToNode(tableNodeValue *CandidateNode) {
51+
if len(dec.pendingComments) > 0 {
52+
comments := strings.Join(dec.pendingComments, "\n")
53+
if tableNodeValue.HeadComment == "" {
54+
tableNodeValue.HeadComment = comments
55+
} else {
56+
tableNodeValue.HeadComment = tableNodeValue.HeadComment + "\n" + comments
57+
}
58+
dec.pendingComments = make([]string, 0)
59+
}
60+
}
61+
5062
func (dec *tomlDecoder) getFullPath(tomlNode *toml.Node) []interface{} {
5163
path := make([]interface{}, 0)
5264
for {
@@ -344,14 +356,8 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
344356
if tableValue.Kind != toml.KeyValue {
345357
log.Debug("got an empty table (or reached next section)")
346358
// If the table had only comments, attach them to the table itself so they don't leak to the next node.
347-
if !sawKeyValue && len(dec.pendingComments) > 0 {
348-
comments := strings.Join(dec.pendingComments, "\n")
349-
if tableNodeValue.HeadComment == "" {
350-
tableNodeValue.HeadComment = comments
351-
} else {
352-
tableNodeValue.HeadComment = tableNodeValue.HeadComment + "\n" + comments
353-
}
354-
dec.pendingComments = make([]string, 0)
359+
if !sawKeyValue {
360+
dec.attachOrphanedCommentsToNode(tableNodeValue)
355361
}
356362
runAgainstCurrentExp = true
357363
break
@@ -366,14 +372,8 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
366372
}
367373
// If we hit EOF after only seeing comments inside this table, attach them to the table itself
368374
// so they don't leak to whatever comes next.
369-
if !sawKeyValue && len(dec.pendingComments) > 0 {
370-
comments := strings.Join(dec.pendingComments, "\n")
371-
if tableNodeValue.HeadComment == "" {
372-
tableNodeValue.HeadComment = comments
373-
} else {
374-
tableNodeValue.HeadComment = tableNodeValue.HeadComment + "\n" + comments
375-
}
376-
dec.pendingComments = make([]string, 0)
375+
if !sawKeyValue {
376+
dec.attachOrphanedCommentsToNode(tableNodeValue)
377377
}
378378

379379
err = dec.d.DeeplyAssign(c, fullPath, tableNodeValue)
@@ -454,14 +454,8 @@ func (dec *tomlDecoder) processArrayTable(currentNode *toml.Node) (bool, error)
454454
// so lets leave that expression for the next round of parsing
455455
if exp.Kind == toml.ArrayTable || exp.Kind == toml.Table {
456456
// If this array-table entry had only comments, attach them to the entry so they don't leak.
457-
if !sawKeyValue && len(dec.pendingComments) > 0 {
458-
comments := strings.Join(dec.pendingComments, "\n")
459-
if tableNodeValue.HeadComment == "" {
460-
tableNodeValue.HeadComment = comments
461-
} else {
462-
tableNodeValue.HeadComment = tableNodeValue.HeadComment + "\n" + comments
463-
}
464-
dec.pendingComments = make([]string, 0)
457+
if !sawKeyValue {
458+
dec.attachOrphanedCommentsToNode(tableNodeValue)
465459
}
466460
runAgainstCurrentExp = true
467461
break

0 commit comments

Comments
 (0)