diff --git a/cmd/read_test.go b/cmd/read_test.go index f4990449..64f89a30 100644 --- a/cmd/read_test.go +++ b/cmd/read_test.go @@ -1207,6 +1207,26 @@ func TestReadBadDataCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Error.Error()) } +func TestReadDeepFromRootCmd(t *testing.T) { + content := `state: + country: + city: foo +` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read %s (**.city==foo)", filename)) + if result.Error != nil { + t.Error(result.Error) + } + + expectedOutput := `country: + city: foo +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadSplatPrefixCmd(t *testing.T) { content := `a: 2 b: diff --git a/pkg/yqlib/delete_navigation_strategy.go b/pkg/yqlib/delete_navigation_strategy.go index 3f397b94..a0792c38 100644 --- a/pkg/yqlib/delete_navigation_strategy.go +++ b/pkg/yqlib/delete_navigation_strategy.go @@ -12,12 +12,6 @@ func DeleteNavigationStrategy(pathElementToDelete interface{}) NavigationStrateg followAlias: func(nodeContext NodeContext) bool { return false }, - autoCreateMap: func(nodeContext NodeContext) bool { - return false - }, - shouldDeeplyTraverse: func(nodeContext NodeContext) bool { - return true - }, shouldOnlyDeeplyVisitLeaves: func(nodeContext NodeContext) bool { return false }, diff --git a/pkg/yqlib/filter_matching_node_navigation_strategy.go b/pkg/yqlib/filter_matching_node_navigation_strategy.go index ac49e1c4..4c15c4fe 100644 --- a/pkg/yqlib/filter_matching_node_navigation_strategy.go +++ b/pkg/yqlib/filter_matching_node_navigation_strategy.go @@ -4,12 +4,6 @@ func FilterMatchingNodesNavigationStrategy(value string) NavigationStrategy { return &NavigationStrategyImpl{ visitedNodes: []*NodeContext{}, pathParser: NewPathParser(), - followAlias: func(nodeContext NodeContext) bool { - return true - }, - autoCreateMap: func(nodeContext NodeContext) bool { - return false - }, visit: func(nodeContext NodeContext) error { return nil }, diff --git a/pkg/yqlib/navigation_strategy.go b/pkg/yqlib/navigation_strategy.go index 81a1ce7b..8b00a623 100644 --- a/pkg/yqlib/navigation_strategy.go +++ b/pkg/yqlib/navigation_strategy.go @@ -62,19 +62,32 @@ func (ns *NavigationStrategyImpl) GetVisitedNodes() []*NodeContext { } func (ns *NavigationStrategyImpl) FollowAlias(nodeContext NodeContext) bool { - return ns.followAlias(nodeContext) + if ns.followAlias != nil { + return ns.followAlias(nodeContext) + } + return true } func (ns *NavigationStrategyImpl) AutoCreateMap(nodeContext NodeContext) bool { - return ns.autoCreateMap(nodeContext) + if ns.autoCreateMap != nil { + return ns.autoCreateMap(nodeContext) + } + return false } func (ns *NavigationStrategyImpl) ShouldDeeplyTraverse(nodeContext NodeContext) bool { - return ns.shouldDeeplyTraverse(nodeContext) + if ns.shouldDeeplyTraverse != nil { + return ns.shouldDeeplyTraverse(nodeContext) + } + return true } func (ns *NavigationStrategyImpl) ShouldOnlyDeeplyVisitLeaves(nodeContext NodeContext) bool { - return ns.shouldOnlyDeeplyVisitLeaves(nodeContext) + if ns.shouldOnlyDeeplyVisitLeaves != nil { + return ns.shouldOnlyDeeplyVisitLeaves(nodeContext) + } + return true + } func (ns *NavigationStrategyImpl) ShouldTraverse(nodeContext NodeContext, nodeKey string) bool { diff --git a/pkg/yqlib/read_navigation_strategy.go b/pkg/yqlib/read_navigation_strategy.go index 98b2df39..b3ec4b0b 100644 --- a/pkg/yqlib/read_navigation_strategy.go +++ b/pkg/yqlib/read_navigation_strategy.go @@ -4,12 +4,6 @@ func ReadNavigationStrategy(deeplyTraverseArrays bool) NavigationStrategy { return &NavigationStrategyImpl{ visitedNodes: []*NodeContext{}, pathParser: NewPathParser(), - followAlias: func(nodeContext NodeContext) bool { - return true - }, - autoCreateMap: func(nodeContext NodeContext) bool { - return false - }, visit: func(nodeContext NodeContext) error { return nil }, @@ -26,8 +20,5 @@ func ReadNavigationStrategy(deeplyTraverseArrays bool) NavigationStrategy { } return deeplyTraverseArrays || !isInArray }, - shouldOnlyDeeplyVisitLeaves: func(nodeContext NodeContext) bool { - return true - }, } } diff --git a/pkg/yqlib/update_navigation_strategy.go b/pkg/yqlib/update_navigation_strategy.go index 99455925..bfb376e6 100644 --- a/pkg/yqlib/update_navigation_strategy.go +++ b/pkg/yqlib/update_navigation_strategy.go @@ -10,12 +10,6 @@ func UpdateNavigationStrategy(updateCommand UpdateCommand, autoCreate bool) Navi autoCreateMap: func(nodeContext NodeContext) bool { return autoCreate }, - shouldDeeplyTraverse: func(nodeContext NodeContext) bool { - return true - }, - shouldOnlyDeeplyVisitLeaves: func(nodeContext NodeContext) bool { - return true - }, visit: func(nodeContext NodeContext) error { node := nodeContext.Node changesToApply := updateCommand.Value