From fdad47868488e611cec2cf84524d12a4b3695554 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Nov 2022 10:22:08 +1100 Subject: [PATCH] Improved set path example --- pkg/yqlib/doc/operators/path.md | 5 +++-- pkg/yqlib/operator_path_test.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/yqlib/doc/operators/path.md b/pkg/yqlib/doc/operators/path.md index 2ac89c16..34c7c667 100644 --- a/pkg/yqlib/doc/operators/path.md +++ b/pkg/yqlib/doc/operators/path.md @@ -123,7 +123,7 @@ a: ``` ## Set path to extract prune deep paths -Like pick but recursive. +Like pick but recursive. This uses `ireduce` to deeply set the selected paths into an empty object, Given a sample.yml file of: ```yaml @@ -137,7 +137,8 @@ parentC: ``` then ```bash -yq '(.parentB.child2, .parentC.child1) as $i ireduce({}; setpath($i | path; $i))' sample.yml +yq '(.parentB.child2, .parentC.child1) as $i + ireduce({}; setpath($i | path; $i))' sample.yml ``` will output ```yaml diff --git a/pkg/yqlib/operator_path_test.go b/pkg/yqlib/operator_path_test.go index dc1472b1..4b7e66ec 100644 --- a/pkg/yqlib/operator_path_test.go +++ b/pkg/yqlib/operator_path_test.go @@ -90,9 +90,9 @@ var pathOperatorScenarios = []expressionScenario{ }, { description: "Set path to extract prune deep paths", - subdescription: "Like pick but recursive.", + subdescription: "Like pick but recursive. This uses `ireduce` to deeply set the selected paths into an empty object,", document: documentToPrune, - expression: `(.parentB.child2, .parentC.child1) as $i ireduce({}; setpath($i | path; $i))`, + expression: "(.parentB.child2, .parentC.child1) as $i\n ireduce({}; setpath($i | path; $i))", expected: []string{ "D0, P[], (!!map)::parentB:\n child2: i am child2\nparentC:\n child1: me child1\n", },