Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add filter operation * add tests * add tests * revert debug * simplify filter * fix tests * remove logs
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
## Filter array | ||
Given a sample.yml file of: | ||
```yaml | ||
- 1 | ||
- 2 | ||
- 3 | ||
``` | ||
then | ||
```bash | ||
yq 'filter(. < 3)' sample.yml | ||
``` | ||
will output | ||
```yaml | ||
- 1 | ||
- 2 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package yqlib | ||
|
||
import ( | ||
"container/list" | ||
) | ||
|
||
func filterOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { | ||
log.Debugf("-- filterOperation") | ||
var results = list.New() | ||
|
||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
candidate := el.Value.(*CandidateNode) | ||
children := context.SingleChildContext(candidate) | ||
splatted, err := splat(children, traversePreferences{}) | ||
if err != nil { | ||
return Context{}, err | ||
} | ||
filtered, err := selectOperator(d, splatted, expressionNode) | ||
if err != nil { | ||
return Context{}, err | ||
} | ||
|
||
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} | ||
collected, err := collectTogether(d, filtered, selfExpression) | ||
if err != nil { | ||
return Context{}, err | ||
} | ||
collected.Node.Style = unwrapDoc(candidate.Node).Style | ||
results.PushBack(collected) | ||
} | ||
return context.ChildContext(results), nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package yqlib | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var filterOperatorScenarios = []expressionScenario{ | ||
{ | ||
description: "Filter array", | ||
document: `[1,2,3]`, | ||
expression: `filter(. < 3)`, | ||
expected: []string{ | ||
"D0, P[], (!!seq)::[1, 2]\n", | ||
}, | ||
}, | ||
{ | ||
skipDoc: true, | ||
document: `[1,2,3]`, | ||
expression: `filter(. > 1)`, | ||
expected: []string{ | ||
"D0, P[], (!!seq)::[2, 3]\n", | ||
}, | ||
}, | ||
{ | ||
skipDoc: true, | ||
description: "Filter array to empty", | ||
document: `[1,2,3]`, | ||
expression: `filter(. > 4)`, | ||
expected: []string{ | ||
"D0, P[], (!!seq)::[]\n", | ||
}, | ||
}, | ||
{ | ||
skipDoc: true, | ||
description: "Filter empty array", | ||
document: `[]`, | ||
expression: `filter(. > 1)`, | ||
expected: []string{ | ||
"D0, P[], (!!seq)::[]\n", | ||
}, | ||
}, | ||
} | ||
|
||
func TestFilterOperatorScenarios(t *testing.T) { | ||
for _, tt := range filterOperatorScenarios { | ||
testScenario(t, &tt) | ||
} | ||
documentOperatorScenarios(t, "filter", filterOperatorScenarios) | ||
} |