yq/pkg/yqlib/update_navigation_strategy.go

44 lines
1.4 KiB
Go
Raw Normal View History

2019-12-25 01:11:04 +00:00
package yqlib
2020-01-05 04:14:14 +00:00
func UpdateNavigationStrategy(updateCommand UpdateCommand, autoCreate bool) NavigationStrategy {
2019-12-28 07:19:37 +00:00
return &NavigationStrategyImpl{
visitedNodes: []*NodeContext{},
pathParser: NewPathParser(),
2019-12-28 07:19:37 +00:00
followAlias: func(nodeContext NodeContext) bool {
2019-12-25 01:11:04 +00:00
return false
},
2019-12-28 07:19:37 +00:00
autoCreateMap: func(nodeContext NodeContext) bool {
2020-01-05 04:14:14 +00:00
return autoCreate
2019-12-25 01:11:04 +00:00
},
2019-12-28 07:19:37 +00:00
visit: func(nodeContext NodeContext) error {
node := nodeContext.Node
2020-01-05 04:14:14 +00:00
changesToApply := updateCommand.Value
2020-01-09 10:27:52 +00:00
if updateCommand.Overwrite || node.Value == "" {
2020-01-05 04:14:14 +00:00
log.Debug("going to update")
DebugNode(node)
log.Debug("with")
DebugNode(changesToApply)
if !updateCommand.DontUpdateNodeValue {
node.Value = changesToApply.Value
}
2020-01-05 04:14:14 +00:00
node.Tag = changesToApply.Tag
node.Kind = changesToApply.Kind
node.Style = changesToApply.Style
if !updateCommand.DontUpdateNodeContent {
node.Content = changesToApply.Content
}
node.Anchor = changesToApply.Anchor
node.Alias = changesToApply.Alias
2020-09-13 00:59:40 +00:00
if updateCommand.CommentsMergeStrategy != IgnoreCommentsMergeStrategy {
node.HeadComment = changesToApply.HeadComment
node.LineComment = changesToApply.LineComment
node.FootComment = changesToApply.FootComment
}
2020-01-05 04:14:14 +00:00
} else {
log.Debug("skipping update as node already has value %v and overwriteFlag is ", node.Value, updateCommand.Overwrite)
}
2019-12-25 01:11:04 +00:00
return nil
},
}
}