yq/pkg/yqlib/operator_comments.go

112 lines
3.2 KiB
Go
Raw Normal View History

2020-11-06 00:23:26 +00:00
package yqlib
2020-11-06 00:45:18 +00:00
import (
2021-11-12 04:02:28 +00:00
"bufio"
"bytes"
2020-11-06 00:45:18 +00:00
"container/list"
2021-11-12 04:02:28 +00:00
"regexp"
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
)
type commentOpPreferences struct {
2020-11-06 00:23:26 +00:00
LineComment bool
HeadComment bool
FootComment bool
}
func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-11-06 00:23:26 +00:00
log.Debugf("AssignComments operator!")
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
2020-11-06 00:23:26 +00:00
if err != nil {
return Context{}, err
2020-11-06 00:23:26 +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(context.ReadOnlyClone(), expressionNode.RHS)
2021-01-06 09:22:50 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:22:50 +00:00
}
if rhs.MatchingNodes.Front() != nil {
comment = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
2021-01-06 09:22:50 +00:00
}
}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-11-06 00:23:26 +00:00
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(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
2021-01-06 09:22:50 +00:00
if err != nil {
return Context{}, err
2021-01-06 09:22:50 +00:00
}
if rhs.MatchingNodes.Front() != nil {
comment = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
2021-01-06 09:22:50 +00:00
}
}
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
2021-11-12 04:02:28 +00:00
candidate.LeadingContent = "" // clobber the leading content, if there was any.
2020-11-06 00:23:26 +00:00
}
if preferences.FootComment {
candidate.Node.FootComment = comment
}
}
return context, nil
2020-11-06 00:23:26 +00:00
}
2020-11-06 00:45:18 +00:00
func getCommentsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
preferences := expressionNode.Operation.Preferences.(commentOpPreferences)
2021-11-12 04:02:28 +00:00
var startCommentCharaterRegExp = regexp.MustCompile(`^# `)
var subsequentCommentCharaterRegExp = regexp.MustCompile(`\n# `)
2020-11-06 00:45:18 +00:00
log.Debugf("GetComments operator!")
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-11-06 00:45:18 +00:00
candidate := el.Value.(*CandidateNode)
comment := ""
if preferences.LineComment {
comment = candidate.Node.LineComment
2021-11-12 04:02:28 +00:00
} else if preferences.HeadComment && candidate.LeadingContent != "" {
var chompRegexp = regexp.MustCompile(`\n$`)
var output bytes.Buffer
var writer = bufio.NewWriter(&output)
var encoder = NewYamlEncoder(2, false, false, false)
if err := encoder.PrintLeadingContent(writer, candidate.LeadingContent); err != nil {
2021-11-12 04:02:28 +00:00
return Context{}, err
}
if err := writer.Flush(); err != nil {
return Context{}, err
}
comment = output.String()
comment = chompRegexp.ReplaceAllString(comment, "")
2020-11-06 00:45:18 +00:00
} else if preferences.HeadComment {
comment = candidate.Node.HeadComment
} else if preferences.FootComment {
comment = candidate.Node.FootComment
}
2021-11-12 04:02:28 +00:00
comment = startCommentCharaterRegExp.ReplaceAllString(comment, "")
comment = subsequentCommentCharaterRegExp.ReplaceAllString(comment, "\n")
2020-11-06 00:45:18 +00:00
node := &yaml.Node{Kind: yaml.ScalarNode, Value: comment, Tag: "!!str"}
2021-11-23 22:57:35 +00:00
result := candidate.CreateReplacement(node)
results.PushBack(result)
2020-11-06 00:45:18 +00:00
}
return context.ChildContext(results), nil
2020-11-06 00:45:18 +00:00
}