more tests

This commit is contained in:
Mike Farah
2020-11-06 11:45:18 +11:00
parent 5ab584afac
commit 05520c2168
6 changed files with 127 additions and 49 deletions
+32 -3
View File
@@ -1,8 +1,13 @@
package yqlib
import "container/list"
import (
"container/list"
"strings"
type AssignCommentPreferences struct {
"gopkg.in/yaml.v3"
)
type CommentOpPreferences struct {
LineComment bool
HeadComment bool
FootComment bool
@@ -27,7 +32,7 @@ func AssignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path
return nil, err
}
preferences := pathNode.Operation.Preferences.(*AssignCommentPreferences)
preferences := pathNode.Operation.Preferences.(*CommentOpPreferences)
for el := lhs.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
@@ -45,3 +50,27 @@ func AssignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, path
}
return matchingNodes, nil
}
func GetCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
preferences := pathNode.Operation.Preferences.(*CommentOpPreferences)
log.Debugf("GetComments operator!")
var results = list.New()
for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
comment := ""
if preferences.LineComment {
comment = candidate.Node.LineComment
} else if preferences.HeadComment {
comment = candidate.Node.HeadComment
} else if preferences.FootComment {
comment = candidate.Node.FootComment
}
comment = strings.Replace(comment, "# ", "", 1)
node := &yaml.Node{Kind: yaml.ScalarNode, Value: comment, Tag: "!!str"}
lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path}
results.PushBack(lengthCand)
}
return results, nil
}