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{},
|
2020-01-11 08:30:27 +00:00
|
|
|
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)
|
2020-04-17 07:09:33 +00:00
|
|
|
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
|
2020-06-17 23:20:26 +00:00
|
|
|
if !updateCommand.DontUpdateNodeContent {
|
|
|
|
node.Content = changesToApply.Content
|
|
|
|
}
|
2020-06-11 03:57:13 +00:00
|
|
|
node.Anchor = changesToApply.Anchor
|
|
|
|
node.Alias = changesToApply.Alias
|
2020-09-13 00:59:40 +00:00
|
|
|
if updateCommand.CommentsMergeStrategy != IgnoreCommentsMergeStrategy {
|
2020-09-07 23:46:04 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|