Added File operators

This commit is contained in:
Mike Farah 2020-11-20 23:08:12 +11:00
parent d38caf6bc2
commit e451119014
4 changed files with 8 additions and 10 deletions

View File

@ -1,13 +1,13 @@
The file operator is used to filter based on filename. This is most often used with merge when needing to merge specific files together.
File operators are most often used with merge when needing to merge specific files together. Note that when doing this, you will need to use `eval-all` to ensure all yaml documents are loaded into memory before performing the merge (as opposed to `eval` which runs the expression once per document).
```bash
yq eval 'filename == "file1.yaml" * fileIndex == 0' file1.yaml file2.yaml
yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
```
## Examples
### Get filename
Given a sample.yml file of:
```yaml
'': null
a: cat
```
then
```bash
@ -21,7 +21,7 @@ sample.yaml
### Get file index
Given a sample.yml file of:
```yaml
'': null
a: cat
```
then
```bash

View File

@ -1,5 +1,5 @@
The file operator is used to filter based on filename. This is most often used with merge when needing to merge specific files together.
File operators are most often used with merge when needing to merge specific files together. Note that when doing this, you will need to use `eval-all` to ensure all yaml documents are loaded into memory before performing the merge (as opposed to `eval` which runs the expression once per document).
```bash
yq eval 'filename == "file1.yaml" * fileIndex == 0' file1.yaml file2.yaml
yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
```

View File

@ -18,8 +18,6 @@ type OperationType struct {
// operators TODO:
// - get path operator (like doc index)
// - get file index op (like doc index)
// - get file name op (like doc index)
// - write in place
// - mergeAppend (merges and appends arrays)
// - mergeEmpty (sets only if the document is empty, do I do that now?)

View File

@ -7,7 +7,7 @@ import (
var fileOperatorScenarios = []expressionScenario{
{
description: "Get filename",
document: `{}`,
document: `{a: cat}`,
expression: `filename`,
expected: []string{
"D0, P[], (!!str)::sample.yml\n",
@ -15,7 +15,7 @@ var fileOperatorScenarios = []expressionScenario{
},
{
description: "Get file index",
document: `{}`,
document: `{a: cat}`,
expression: `fileIndex`,
expected: []string{
"D0, P[], (!!int)::0\n",