yq/pkg/yqlib/update_navigation_strategy.go

46 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
},
2020-02-07 05:32:39 +00:00
shouldDeeplyTraverse: func(nodeContext NodeContext) bool {
return true
},
2020-06-10 23:40:59 +00:00
shouldOnlyDeeplyVisitLeaves: func(nodeContext NodeContext) bool {
return true
},
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
node.Content = changesToApply.Content
node.Anchor = changesToApply.Anchor
node.Alias = changesToApply.Alias
2020-01-05 04:14:14 +00:00
node.HeadComment = changesToApply.HeadComment
node.LineComment = changesToApply.LineComment
node.FootComment = changesToApply.FootComment
} 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
},
}
}