Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 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,4 @@ | ||
# Shuffle | ||
|
||
Shuffles an array. Note that this command does _not_ use a cryptographically secure random number generator to randomise the array order. | ||
|
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,51 @@ | ||
# Shuffle | ||
|
||
Shuffles an array. Note that this command does _not_ use a cryptographically secure random number generator to randomise the array order. | ||
|
||
|
||
## Shuffle array | ||
Given a sample.yml file of: | ||
```yaml | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
- 5 | ||
``` | ||
then | ||
```bash | ||
yq 'shuffle' sample.yml | ||
``` | ||
will output | ||
```yaml | ||
- 5 | ||
- 2 | ||
- 4 | ||
- 1 | ||
- 3 | ||
``` | ||
|
||
## Shuffle array in place | ||
Given a sample.yml file of: | ||
```yaml | ||
cool: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
- 5 | ||
``` | ||
then | ||
```bash | ||
yq '.cool |= shuffle' sample.yml | ||
``` | ||
will output | ||
```yaml | ||
cool: | ||
- 5 | ||
- 2 | ||
- 4 | ||
- 1 | ||
- 3 | ||
``` | ||
|
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,37 @@ | ||
package yqlib | ||
|
||
import ( | ||
"container/list" | ||
"fmt" | ||
"math/rand" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
func shuffleOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { | ||
|
||
// ignore CWE-338 gosec issue of not using crypto/rand | ||
// this is just to shuffle an array rather generating a | ||
// secret or something that needs proper rand. | ||
myRand := rand.New(rand.NewSource(Now().UnixNano())) // #nosec | ||
|
||
results := list.New() | ||
|
||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { | ||
candidate := el.Value.(*CandidateNode) | ||
|
||
candidateNode := unwrapDoc(candidate.Node) | ||
|
||
if candidateNode.Kind != yaml.SequenceNode { | ||
return context, fmt.Errorf("node at path [%v] is not an array (it's a %v)", candidate.GetNicePath(), candidate.GetNiceTag()) | ||
} | ||
|
||
result := deepClone(candidateNode) | ||
|
||
a := result.Content | ||
|
||
myRand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] }) | ||
results.PushBack(candidate.CreateReplacement(result)) | ||
} | ||
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,30 @@ | ||
package yqlib | ||
|
||
import "testing" | ||
|
||
var shuffleOperatorScenarios = []expressionScenario{ | ||
{ | ||
description: "Shuffle array", | ||
document: "[1, 2, 3, 4, 5]", | ||
expression: `shuffle`, | ||
expected: []string{ | ||
"D0, P[], (!!seq)::[5, 2, 4, 1, 3]\n", | ||
}, | ||
}, | ||
|
||
{ | ||
description: "Shuffle array in place", | ||
document: "cool: [1, 2, 3, 4, 5]", | ||
expression: `.cool |= shuffle`, | ||
expected: []string{ | ||
"D0, P[], (doc)::cool: [5, 2, 4, 1, 3]\n", | ||
}, | ||
}, | ||
} | ||
|
||
func TestShuffleByOperatorScenarios(t *testing.T) { | ||
for _, tt := range shuffleOperatorScenarios { | ||
testScenario(t, &tt) | ||
} | ||
documentOperatorScenarios(t, "shuffle", shuffleOperatorScenarios) | ||
} |