mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 19:35:38 +00:00
Fixed sort dropping head comment #1181
This commit is contained in:
parent
6f9f80cc48
commit
ea66a73f43
@ -85,14 +85,15 @@ func (n *CandidateNode) CreateChildInArray(index int, node *yaml.Node) *Candidat
|
|||||||
|
|
||||||
func (n *CandidateNode) CreateReplacement(node *yaml.Node) *CandidateNode {
|
func (n *CandidateNode) CreateReplacement(node *yaml.Node) *CandidateNode {
|
||||||
return &CandidateNode{
|
return &CandidateNode{
|
||||||
Node: node,
|
Node: node,
|
||||||
Path: n.createChildPath(nil),
|
Path: n.createChildPath(nil),
|
||||||
Parent: n.Parent,
|
LeadingContent: n.LeadingContent,
|
||||||
Key: n.Key,
|
Parent: n.Parent,
|
||||||
IsMapKey: n.IsMapKey,
|
Key: n.Key,
|
||||||
Document: n.Document,
|
IsMapKey: n.IsMapKey,
|
||||||
Filename: n.Filename,
|
Document: n.Document,
|
||||||
FileIndex: n.FileIndex,
|
Filename: n.Filename,
|
||||||
|
FileIndex: n.FileIndex,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -105,6 +105,7 @@ func getCommentsOperator(d *dataTreeNavigator, context Context, expressionNode *
|
|||||||
|
|
||||||
node := &yaml.Node{Kind: yaml.ScalarNode, Value: comment, Tag: "!!str"}
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: comment, Tag: "!!str"}
|
||||||
result := candidate.CreateReplacement(node)
|
result := candidate.CreateReplacement(node)
|
||||||
|
result.LeadingContent = "" // don't include the leading yaml content when retrieving a comment
|
||||||
results.PushBack(result)
|
results.PushBack(result)
|
||||||
}
|
}
|
||||||
return context.ChildContext(results), nil
|
return context.ChildContext(results), nil
|
||||||
|
|||||||
@ -79,6 +79,15 @@ var sortByOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!seq)::[{a: false, b: 1}, {a: false, b: 3}, {a: true, b: 2}]\n",
|
"D0, P[], (!!seq)::[{a: false, b: 1}, {a: false, b: 3}, {a: true, b: 2}]\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "head comment",
|
||||||
|
document: "# abc\n- def\n# ghi",
|
||||||
|
expression: `sort`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!seq)::# abc\n- def\n# ghi\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortByOperatorScenarios(t *testing.T) {
|
func TestSortByOperatorScenarios(t *testing.T) {
|
||||||
|
|||||||
@ -61,6 +61,7 @@ func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, p
|
|||||||
|
|
||||||
for index, filename := range filenames {
|
for index, filename := range filenames {
|
||||||
reader, leadingContent, err := readStream(filename, leadingContentPreProcessing)
|
reader, leadingContent, err := readStream(filename, leadingContentPreProcessing)
|
||||||
|
log.Debug("leadingContent: %v", leadingContent)
|
||||||
|
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
firstFileLeadingContent = leadingContent
|
firstFileLeadingContent = leadingContent
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user