2020-11-06 00:23:26 +00:00
|
|
|
package yqlib
|
|
|
|
|
2020-11-06 00:45:18 +00:00
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"strings"
|
2020-11-06 00:23:26 +00:00
|
|
|
|
2021-01-06 09:22:50 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-11-06 00:45:18 +00:00
|
|
|
)
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
type commentOpPreferences struct {
|
2020-11-06 00:23:26 +00:00
|
|
|
LineComment bool
|
|
|
|
HeadComment bool
|
|
|
|
FootComment bool
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func assignCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
|
2020-11-06 00:23:26 +00:00
|
|
|
|
|
|
|
log.Debugf("AssignComments operator!")
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs)
|
2020-11-06 00:23:26 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
preferences := expressionNode.Operation.Preferences.(*commentOpPreferences)
|
2020-11-06 00:23:26 +00:00
|
|
|
|
2021-01-06 09:22:50 +00:00
|
|
|
comment := ""
|
2021-01-12 23:18:53 +00:00
|
|
|
if !expressionNode.Operation.UpdateAssign {
|
|
|
|
rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs)
|
2021-01-06 09:22:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rhs.Front() != nil {
|
|
|
|
comment = rhs.Front().Value.(*CandidateNode).Node.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:23:26 +00:00
|
|
|
for el := lhs.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2021-01-06 09:22:50 +00:00
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
if expressionNode.Operation.UpdateAssign {
|
|
|
|
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs)
|
2021-01-06 09:22:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rhs.Front() != nil {
|
|
|
|
comment = rhs.Front().Value.(*CandidateNode).Node.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:23:26 +00:00
|
|
|
log.Debugf("Setting comment of : %v", candidate.GetKey())
|
|
|
|
if preferences.LineComment {
|
|
|
|
candidate.Node.LineComment = comment
|
|
|
|
}
|
|
|
|
if preferences.HeadComment {
|
|
|
|
candidate.Node.HeadComment = comment
|
|
|
|
}
|
|
|
|
if preferences.FootComment {
|
|
|
|
candidate.Node.FootComment = comment
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return matchingNodes, nil
|
|
|
|
}
|
2020-11-06 00:45:18 +00:00
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func getCommentsOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
|
|
|
|
preferences := expressionNode.Operation.Preferences.(*commentOpPreferences)
|
2020-11-06 00:45:18 +00:00
|
|
|
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"}
|
2021-01-12 08:36:28 +00:00
|
|
|
result := candidate.CreateChild(nil, node)
|
|
|
|
results.PushBack(result)
|
2020-11-06 00:45:18 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|