diff --git a/pkg/yqlib/doc/Recursive Descent.md b/pkg/yqlib/doc/Recursive Descent.md index 865306ec..ee0a61f8 100644 --- a/pkg/yqlib/doc/Recursive Descent.md +++ b/pkg/yqlib/doc/Recursive Descent.md @@ -32,6 +32,48 @@ a: frog frog ``` +## Recursively find nodes with keys +Given a sample.yml file of: +```yaml +a: + name: frog + b: + name: blog + age: 12 +``` +then +```bash +yq eval '.. | select(has("name"))' sample.yml +``` +will output +```yaml +name: frog +b: + name: blog + age: 12 +name: blog +age: 12 +``` + +## Recursively find nodes with values +Given a sample.yml file of: +```yaml +a: + nameA: frog + b: + nameB: frog + age: 12 +``` +then +```bash +yq eval '.. | select(. == "frog")' sample.yml +``` +will output +```yaml +frog +frog +``` + ## Recurse map (values and keys) Note that the map key appears in the results diff --git a/pkg/yqlib/operator_recursive_descent_test.go b/pkg/yqlib/operator_recursive_descent_test.go index 6b335878..0d25d16d 100644 --- a/pkg/yqlib/operator_recursive_descent_test.go +++ b/pkg/yqlib/operator_recursive_descent_test.go @@ -62,6 +62,24 @@ var recursiveDescentOperatorScenarios = []expressionScenario{ "D0, P[a], (!!str)::frog\n", }, }, + { + description: "Recursively find nodes with keys", + document: `{a: {name: frog, b: {name: blog, age: 12}}}`, + expression: `.. | select(has("name"))`, + expected: []string{ + "D0, P[a], (!!map)::{name: frog, b: {name: blog, age: 12}}\n", + "D0, P[a b], (!!map)::{name: blog, age: 12}\n", + }, + }, + { + description: "Recursively find nodes with values", + document: `{a: {nameA: frog, b: {nameB: frog, age: 12}}}`, + expression: `.. | select(. == "frog")`, + expected: []string{ + "D0, P[a nameA], (!!str)::frog\n", + "D0, P[a b nameB], (!!str)::frog\n", + }, + }, { description: "Recurse map (values and keys)", subdescription: "Note that the map key appears in the results",