Skip to content

Commit

Permalink
omit operator (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenson committed Mar 23, 2024
1 parent 3b85cef commit f5bfe5a
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/yqlib/doc/operators/omit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

## Omit keys from map
Note that the order of the keys matches the omit order and non existent keys are skipped.

Given a sample.yml file of:
```yaml
myMap:
cat: meow
dog: bark
thing: hamster
hamster: squeak
```
then
```bash
yq '.myMap |= omit(["hamster", "cat", "goat"])' sample.yml
```
will output
```yaml
myMap:
dog: bark
thing: hamster
```

## Omit indices from array
Note that the order of the indices matches the omit order and non existent indices are skipped.

Given a sample.yml file of:
```yaml
- cat
- leopard
- lion
```
then
```bash
yq 'omit([2, 0, 734, -5])' sample.yml
```
will output
```yaml
- leopard
```

1 change: 1 addition & 0 deletions pkg/yqlib/lexer_participle.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var participleYqRules = []*participleYqRule{
simpleOp("map", mapOpType),
simpleOp("filter", filterOpType),
simpleOp("pick", pickOpType),
simpleOp("omit", omitOpType),

{"FlattenWithDepth", `flatten\([0-9]+\)`, flattenWithDepth(), 0},
{"Flatten", `flatten`, opTokenWithPrefs(flattenOpType, nil, flattenPreferences{depth: -1}), 0},
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var mapOpType = &operationType{Type: "MAP", NumArgs: 1, Precedence: 50, Handler:
var filterOpType = &operationType{Type: "FILTER", NumArgs: 1, Precedence: 50, Handler: filterOperator}
var errorOpType = &operationType{Type: "ERROR", NumArgs: 1, Precedence: 50, Handler: errorOperator}
var pickOpType = &operationType{Type: "PICK", NumArgs: 1, Precedence: 50, Handler: pickOperator}
var omitOpType = &operationType{Type: "OMIT", NumArgs: 1, Precedence: 50, Handler: omitOperator}
var evalOpType = &operationType{Type: "EVAL", NumArgs: 1, Precedence: 50, Handler: evalOperator}
var mapValuesOpType = &operationType{Type: "MAP_VALUES", NumArgs: 1, Precedence: 50, Handler: mapValuesOperator}

Expand Down
73 changes: 73 additions & 0 deletions pkg/yqlib/operator_omit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package yqlib

import (
"container/list"
"strconv"
)

func omitMap(original *CandidateNode, indices *CandidateNode) *CandidateNode {
filteredContent := make([]*CandidateNode, 0, max(0, len(original.Content)-len(indices.Content)*2))

for index := 0; index < len(original.Content); index += 2 {
pos := findInArray(indices, original.Content[index])
if pos < 0 {
clonedKey := original.Content[index].Copy()
clonedValue := original.Content[index+1].Copy()
filteredContent = append(filteredContent, clonedKey, clonedValue)
}
}
result := original.CopyWithoutContent()
result.AddChildren(filteredContent)
return result
}

func omitSequence(original *CandidateNode, indices *CandidateNode) *CandidateNode {
filteredContent := make([]*CandidateNode, 0, max(0, len(original.Content)-len(indices.Content)))

for index := 0; index < len(original.Content); index++ {
pos := findInArray(indices, createScalarNode(index, strconv.Itoa(index)))
if pos < 0 {
filteredContent = append(filteredContent, original.Content[index].Copy())
}
}
result := original.CopyWithoutContent()
result.AddChildren(filteredContent)
return result
}

func omitOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("Omit")

contextIndicesToOmit, err := d.GetMatchingNodes(context, expressionNode.RHS)

if err != nil {
return Context{}, err
}
indicesToOmit := &CandidateNode{}
if contextIndicesToOmit.MatchingNodes.Len() > 0 {
indicesToOmit = contextIndicesToOmit.MatchingNodes.Front().Value.(*CandidateNode)
}
if len(indicesToOmit.Content) == 0 {
log.Debugf("No omit indices specified")
return context, nil
}
var results = list.New()

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)

var replacement *CandidateNode

if node.Kind == MappingNode {
replacement = omitMap(node, indicesToOmit)
} else if node.Kind == SequenceNode {
replacement = omitSequence(node, indicesToOmit)
} else {
log.Debugf("Omit from type %v (%v) is noop", node.Tag, node.GetNicePath())
return context, nil
}
replacement.LeadingContent = node.LeadingContent
results.PushBack(replacement)
}
return context.ChildContext(results), nil
}
60 changes: 60 additions & 0 deletions pkg/yqlib/operator_omit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package yqlib

import (
"testing"
)

var omitOperatorScenarios = []expressionScenario{
{
description: "Omit keys from map",
subdescription: "Note that the order of the keys matches the omit order and non existent keys are skipped.",
document: "myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeak}\n",
expression: `.myMap |= omit(["hamster", "cat", "goat"])`,
expected: []string{
"D0, P[], (!!map)::myMap: {dog: bark, thing: hamster}\n",
},
},
{
description: "Omit keys from map",
skipDoc: true,
document: "!things myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeak}\n",
expression: `.myMap |= omit(["hamster", "cat", "goat"])`,
expected: []string{
"D0, P[], (!!map)::!things myMap: {dog: bark, thing: hamster}\n",
},
},
{
description: "Omit keys from map with comments",
skipDoc: true,
document: "# abc\nmyMap: {cat: meow, dog: bark, thing: hamster, hamster: squeak}\n# xyz\n",
expression: `.myMap |= omit(["hamster", "cat", "goat"])`,
expected: []string{
"D0, P[], (!!map)::# abc\nmyMap: {dog: bark, thing: hamster}\n# xyz\n",
},
},
{
description: "Omit indices from array",
subdescription: "Note that the order of the indices matches the omit order and non existent indices are skipped.",
document: `[cat, leopard, lion]`,
expression: `omit([2, 0, 734, -5])`,
expected: []string{
"D0, P[], (!!seq)::[leopard]\n",
},
},
{
description: "Omit indices from array with comments",
skipDoc: true,
document: "# abc\n[cat, leopard, lion]\n# xyz",
expression: `omit([2, 0, 734, -5])`,
expected: []string{
"D0, P[], (!!seq)::# abc\n[leopard]\n# xyz\n",
},
},
}

func TestOmitOperatorScenarios(t *testing.T) {
for _, tt := range omitOperatorScenarios {
testScenario(t, &tt)
}
documentOperatorScenarios(t, "omit", omitOperatorScenarios)
}

0 comments on commit f5bfe5a

Please sign in to comment.