Add with_entries example using filtering

This commit is contained in:
Curtis Vogt 2021-06-07 10:58:30 -05:00 committed by Mike Farah
parent fae2b2643c
commit ed377a032d
2 changed files with 24 additions and 0 deletions

View File

@ -98,3 +98,19 @@ KEY_a: 1
KEY_b: 2 KEY_b: 2
``` ```
Using with_entries is also useful whe you want to filter a map. Given a sample.yml file of:
```yaml
a:
b: bird
c:
d: dog
```
then
```bash
yq eval 'with_entries(select(.value | has("b")))' sample.yml
```
will output
```yaml
a:
b: bird
```

View File

@ -52,6 +52,14 @@ var entriesOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n", "D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
}, },
}, },
{
description: "Use with_entries to filter keys",
document: `{a: b: "bird", c: d: "dog"}`,
expression: `with_entries(.value | has("b"))`,
expected: []string{
"D0, P[], (!!map)::a:\n b: \"bird\"\n",
},
},
} }
func TestEntriesOperatorScenarios(t *testing.T) { func TestEntriesOperatorScenarios(t *testing.T) {