mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-07 06:10:36 +00:00
Improving first op test
This commit is contained in:
parent
0ecdce24e8
commit
8b2ba41c6c
@ -1,3 +1,8 @@
|
|||||||
|
# First
|
||||||
|
|
||||||
|
Returns the first matching element in an array, or first matching value in a map.
|
||||||
|
|
||||||
|
Can be given an expression to match with, otherwise will just return the first.
|
||||||
|
|
||||||
## First matching element from array
|
## First matching element from array
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
@ -20,8 +25,10 @@ Given a sample.yml file of:
|
|||||||
```yaml
|
```yaml
|
||||||
- a: banana
|
- a: banana
|
||||||
- a: cat
|
- a: cat
|
||||||
|
b: firstCat
|
||||||
- a: apple
|
- a: apple
|
||||||
- a: cat
|
- a: cat
|
||||||
|
b: secondCat
|
||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
@ -30,6 +37,7 @@ yq 'first(.a == "cat")' sample.yml
|
|||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
a: cat
|
a: cat
|
||||||
|
b: firstCat
|
||||||
```
|
```
|
||||||
|
|
||||||
## First matching element from array with numeric condition
|
## First matching element from array with numeric condition
|
||||||
@ -38,6 +46,7 @@ Given a sample.yml file of:
|
|||||||
- a: 10
|
- a: 10
|
||||||
- a: 100
|
- a: 100
|
||||||
- a: 1
|
- a: 1
|
||||||
|
- a: 101
|
||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
@ -53,7 +62,10 @@ Given a sample.yml file of:
|
|||||||
```yaml
|
```yaml
|
||||||
- a: false
|
- a: false
|
||||||
- a: true
|
- a: true
|
||||||
|
b: firstTrue
|
||||||
- a: false
|
- a: false
|
||||||
|
- a: true
|
||||||
|
b: secondTrue
|
||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
@ -62,6 +74,7 @@ yq 'first(.a == true)' sample.yml
|
|||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
a: true
|
a: true
|
||||||
|
b: firstTrue
|
||||||
```
|
```
|
||||||
|
|
||||||
## First matching element from array with null values
|
## First matching element from array with null values
|
||||||
@ -84,19 +97,19 @@ a: cat
|
|||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
- a: dog
|
- a: dog
|
||||||
b: 5
|
b: 7
|
||||||
- a: cat
|
- a: cat
|
||||||
b: 3
|
b: 3
|
||||||
- a: apple
|
- a: apple
|
||||||
b: 7
|
b: 5
|
||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
yq 'first(.b > 4)' sample.yml
|
yq 'first(.b > 4 and .b < 6)' sample.yml
|
||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
a: dog
|
a: apple
|
||||||
b: 5
|
b: 5
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -127,7 +140,7 @@ x:
|
|||||||
y:
|
y:
|
||||||
a: 100
|
a: 100
|
||||||
z:
|
z:
|
||||||
a: 1
|
a: 101
|
||||||
```
|
```
|
||||||
then
|
then
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
5
pkg/yqlib/doc/operators/headers/first.md
Normal file
5
pkg/yqlib/doc/operators/headers/first.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# First
|
||||||
|
|
||||||
|
Returns the first matching element in an array, or first matching value in a map.
|
||||||
|
|
||||||
|
Can be given an expression to match with, otherwise will just return the first.
|
||||||
@ -13,15 +13,15 @@ var firstOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "First matching element from array with multiple matches",
|
description: "First matching element from array with multiple matches",
|
||||||
document: "[{a: banana},{a: cat},{a: apple},{a: cat}]",
|
document: "[{a: banana},{a: cat, b: firstCat},{a: apple},{a: cat, b: secondCat}]",
|
||||||
expression: `first(.a == "cat")`,
|
expression: `first(.a == "cat")`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[1], (!!map)::{a: cat}\n",
|
"D0, P[1], (!!map)::{a: cat, b: firstCat}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "First matching element from array with numeric condition",
|
description: "First matching element from array with numeric condition",
|
||||||
document: "[{a: 10},{a: 100},{a: 1}]",
|
document: "[{a: 10},{a: 100},{a: 1},{a: 101}]",
|
||||||
expression: `first(.a > 50)`,
|
expression: `first(.a > 50)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[1], (!!map)::{a: 100}\n",
|
"D0, P[1], (!!map)::{a: 100}\n",
|
||||||
@ -29,10 +29,10 @@ var firstOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "First matching element from array with boolean condition",
|
description: "First matching element from array with boolean condition",
|
||||||
document: "[{a: false},{a: true},{a: false}]",
|
document: "[{a: false},{a: true, b: firstTrue},{a: false}, {a: true, b: secondTrue}]",
|
||||||
expression: `first(.a == true)`,
|
expression: `first(.a == true)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[1], (!!map)::{a: true}\n",
|
"D0, P[1], (!!map)::{a: true, b: firstTrue}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -45,10 +45,10 @@ var firstOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "First matching element from array with complex condition",
|
description: "First matching element from array with complex condition",
|
||||||
document: "[{a: dog, b: 5},{a: cat, b: 3},{a: apple, b: 7}]",
|
document: "[{a: dog, b: 7},{a: cat, b: 3},{a: apple, b: 5}]",
|
||||||
expression: `first(.b > 4)`,
|
expression: `first(.b > 4 and .b < 6)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[0], (!!map)::{a: dog, b: 5}\n",
|
"D0, P[2], (!!map)::{a: apple, b: 5}\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -61,7 +61,7 @@ var firstOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "First matching element from map with numeric condition",
|
description: "First matching element from map with numeric condition",
|
||||||
document: "x: {a: 10}\ny: {a: 100}\nz: {a: 1}",
|
document: "x: {a: 10}\ny: {a: 100}\nz: {a: 101}",
|
||||||
expression: `first(.a > 50)`,
|
expression: `first(.a > 50)`,
|
||||||
expected: []string{
|
expected: []string{
|
||||||
"D0, P[y], (!!map)::{a: 100}\n",
|
"D0, P[y], (!!map)::{a: 100}\n",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user