2020-06-17 23:20:26 +00:00
|
|
|
package yqlib
|
|
|
|
|
2020-07-17 03:07:32 +00:00
|
|
|
import "gopkg.in/yaml.v3"
|
|
|
|
|
2020-07-17 03:26:20 +00:00
|
|
|
func ReadForMergeNavigationStrategy(arrayMergeStrategy ArrayMergeStrategy) NavigationStrategy {
|
2020-06-17 23:20:26 +00:00
|
|
|
return &NavigationStrategyImpl{
|
|
|
|
visitedNodes: []*NodeContext{},
|
|
|
|
pathParser: NewPathParser(),
|
|
|
|
followAlias: func(nodeContext NodeContext) bool {
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
shouldOnlyDeeplyVisitLeaves: func(nodeContext NodeContext) bool {
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
visit: func(nodeContext NodeContext) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
shouldDeeplyTraverse: func(nodeContext NodeContext) bool {
|
2020-07-17 03:26:20 +00:00
|
|
|
if nodeContext.Node.Kind == yaml.SequenceNode && arrayMergeStrategy == OverwriteArrayMergeStrategy {
|
2020-07-17 03:07:32 +00:00
|
|
|
nodeContext.IsMiddleNode = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-17 23:20:26 +00:00
|
|
|
var isInArray = false
|
|
|
|
if len(nodeContext.PathStack) > 0 {
|
|
|
|
var lastElement = nodeContext.PathStack[len(nodeContext.PathStack)-1]
|
|
|
|
switch lastElement.(type) {
|
|
|
|
case int:
|
|
|
|
isInArray = true
|
|
|
|
default:
|
|
|
|
isInArray = false
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 03:26:20 +00:00
|
|
|
return arrayMergeStrategy == UpdateArrayMergeStrategy || !isInArray
|
2020-06-17 23:20:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|