mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-26 16:35:38 +00:00
Improved handling of trailing document comments
This commit is contained in:
parent
dee592eb3b
commit
6bce23d9c2
@ -12,6 +12,62 @@ testBasicEvalRoundTrip() {
|
|||||||
assertEquals 123 "$X"
|
assertEquals 123 "$X"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testBasicTrailingContent() {
|
||||||
|
cat >test-trailing.yml <<EOL
|
||||||
|
test:
|
||||||
|
# this comment will be removed
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
test:
|
||||||
|
# this comment will be removed
|
||||||
|
EOM
|
||||||
|
X=$(./yq test-trailing.yml -P)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testBasicTrailingContent() {
|
||||||
|
cat >test-trailing.yml <<EOL
|
||||||
|
test:
|
||||||
|
# this comment will be removed
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
test:
|
||||||
|
# hi
|
||||||
|
EOM
|
||||||
|
X=$(./yq '. footComment = "hi"' test-trailing.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testBasicTrailingContentEvalAll() {
|
||||||
|
cat >test-trailing.yml <<EOL
|
||||||
|
test:
|
||||||
|
# this comment will be removed
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
test:
|
||||||
|
# this comment will be removed
|
||||||
|
EOM
|
||||||
|
X=$(./yq ea test-trailing.yml -P)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
|
testBasicTrailingContentEvalAll() {
|
||||||
|
cat >test-trailing.yml <<EOL
|
||||||
|
test:
|
||||||
|
# this comment will be removed
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
test:
|
||||||
|
# hi
|
||||||
|
EOM
|
||||||
|
X=$(./yq ea '. footComment = "hi"' test-trailing.yml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
testBasicPipeWithDot() {
|
testBasicPipeWithDot() {
|
||||||
./yq -n ".a = 123" > test.yml
|
./yq -n ".a = 123" > test.yml
|
||||||
X=$(cat test.yml | ./yq '.')
|
X=$(cat test.yml | ./yq '.')
|
||||||
|
@ -15,6 +15,7 @@ type CandidateNode struct {
|
|||||||
Key *yaml.Node // node key, if this is a value from a map (or index in an array)
|
Key *yaml.Node // node key, if this is a value from a map (or index in an array)
|
||||||
|
|
||||||
LeadingContent string
|
LeadingContent string
|
||||||
|
TrailingContent string
|
||||||
|
|
||||||
Path []interface{} /// the path we took to get to this node
|
Path []interface{} /// the path we took to get to this node
|
||||||
Document uint // the document index of this node
|
Document uint // the document index of this node
|
||||||
@ -170,6 +171,9 @@ func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode, prefs assignP
|
|||||||
if other.Node.FootComment != "" {
|
if other.Node.FootComment != "" {
|
||||||
n.Node.FootComment = other.Node.FootComment
|
n.Node.FootComment = other.Node.FootComment
|
||||||
}
|
}
|
||||||
|
if other.TrailingContent != "" {
|
||||||
|
n.TrailingContent = other.TrailingContent
|
||||||
|
}
|
||||||
if other.Node.HeadComment != "" {
|
if other.Node.HeadComment != "" {
|
||||||
n.Node.HeadComment = other.Node.HeadComment
|
n.Node.HeadComment = other.Node.HeadComment
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,6 @@ yq '. foot_comment=.a' sample.yml
|
|||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
a: cat
|
a: cat
|
||||||
|
|
||||||
# cat
|
# cat
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -117,6 +116,7 @@ b:
|
|||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
a: cat # meow
|
a: cat # meow
|
||||||
|
# have a great day
|
||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
|
@ -61,7 +61,9 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod
|
|||||||
candidate.Node.HeadComment = comment
|
candidate.Node.HeadComment = comment
|
||||||
candidate.LeadingContent = "" // clobber the leading content, if there was any.
|
candidate.LeadingContent = "" // clobber the leading content, if there was any.
|
||||||
}
|
}
|
||||||
if preferences.FootComment {
|
if preferences.FootComment && candidate.Node.Kind == yaml.DocumentNode && comment != "" {
|
||||||
|
candidate.TrailingContent = "# " + comment
|
||||||
|
} else if preferences.FootComment && candidate.Node.Kind != yaml.DocumentNode {
|
||||||
candidate.Node.FootComment = comment
|
candidate.Node.FootComment = comment
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,6 +99,8 @@ func getCommentsOperator(d *dataTreeNavigator, context Context, expressionNode *
|
|||||||
comment = chompRegexp.ReplaceAllString(comment, "")
|
comment = chompRegexp.ReplaceAllString(comment, "")
|
||||||
} else if preferences.HeadComment {
|
} else if preferences.HeadComment {
|
||||||
comment = candidate.Node.HeadComment
|
comment = candidate.Node.HeadComment
|
||||||
|
} else if preferences.FootComment && candidate.Node.Kind == yaml.DocumentNode && candidate.TrailingContent != "" {
|
||||||
|
comment = candidate.TrailingContent
|
||||||
} else if preferences.FootComment {
|
} else if preferences.FootComment {
|
||||||
comment = candidate.Node.FootComment
|
comment = candidate.Node.FootComment
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ var commentOperatorScenarios = []expressionScenario{
|
|||||||
document: `a: cat`,
|
document: `a: cat`,
|
||||||
expression: `. foot_comment=.a`,
|
expression: `. foot_comment=.a`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (doc)::a: cat\n\n# cat\n",
|
"D0, P[], (doc)::a: cat\n# cat\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -144,7 +144,7 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
document2: docNoComments,
|
document2: docNoComments,
|
||||||
expression: `select(fi == 0) * select(fi == 1)`,
|
expression: `select(fi == 0) * select(fi == 1)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::a: apple\nb: banana\n\n# footer\n",
|
"D0, P[], (!!map)::a: apple\nb: banana\n# footer\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -157,12 +157,13 @@ var multiplyOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "doc 2 has footer",
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: docNoComments,
|
document: docNoComments,
|
||||||
document2: docWithFooter,
|
document2: docWithFooter,
|
||||||
expression: `select(fi == 0) * select(fi == 1)`,
|
expression: `select(fi == 0) * select(fi == 1)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[], (!!map)::b: banana\na: apple\n\n# footer\n",
|
"D0, P[], (!!map)::b: banana\na: apple\n# footer\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -134,6 +134,10 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := p.encoder.PrintLeadingContent(writer, mappedDoc.TrailingContent); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
p.previousDocIndex = mappedDoc.Document
|
p.previousDocIndex = mappedDoc.Document
|
||||||
if err := writer.Flush(); err != nil {
|
if err := writer.Flush(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -110,6 +110,11 @@ func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *Expr
|
|||||||
Node: &dataBucket,
|
Node: &dataBucket,
|
||||||
FileIndex: s.fileIndex,
|
FileIndex: s.fileIndex,
|
||||||
}
|
}
|
||||||
|
//move document comments into candidate node
|
||||||
|
// otherwise unwrap drops them.
|
||||||
|
candidateNode.TrailingContent = dataBucket.FootComment
|
||||||
|
dataBucket.FootComment = ""
|
||||||
|
|
||||||
if currentIndex == 0 {
|
if currentIndex == 0 {
|
||||||
candidateNode.LeadingContent = leadingContent
|
candidateNode.LeadingContent = leadingContent
|
||||||
}
|
}
|
||||||
|
@ -96,6 +96,11 @@ func readDocuments(reader io.Reader, filename string, fileIndex int, decoder Dec
|
|||||||
EvaluateTogether: true,
|
EvaluateTogether: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//move document comments into candidate node
|
||||||
|
// otherwise unwrap drops them.
|
||||||
|
candidateNode.TrailingContent = dataBucket.FootComment
|
||||||
|
dataBucket.FootComment = ""
|
||||||
|
|
||||||
inputList.PushBack(candidateNode)
|
inputList.PushBack(candidateNode)
|
||||||
|
|
||||||
currentIndex = currentIndex + 1
|
currentIndex = currentIndex + 1
|
||||||
|
Loading…
Reference in New Issue
Block a user