mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Compare commits
3 Commits
op-precend
...
v4.5.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebeb1146ba | ||
|
|
0bb53ec770 | ||
|
|
a50e154652 |
@@ -11,7 +11,7 @@ var (
|
|||||||
GitDescribe string
|
GitDescribe string
|
||||||
|
|
||||||
// Version is main version number that is being run at the moment.
|
// Version is main version number that is being run at the moment.
|
||||||
Version = "4.4.1"
|
Version = "4.5.0"
|
||||||
|
|
||||||
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
|
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
|
||||||
// then it means that it is a final release. Otherwise, this is a pre-release
|
// then it means that it is a final release. Otherwise, this is a pre-release
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM mikefarah/yq:4.4.1
|
FROM mikefarah/yq:4.5.0
|
||||||
|
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
|
|
||||||
|
|||||||
58
pkg/yqlib/doc/Variable Operators.md
Normal file
58
pkg/yqlib/doc/Variable Operators.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
For more complex scenarios, variables can be used to hold values of expression to be used in other expressions.
|
||||||
|
|
||||||
|
## Single value variable
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
a: cat
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq eval '.a as $foo | $foo' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
cat
|
||||||
|
```
|
||||||
|
|
||||||
|
## Multi value variable
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
- cat
|
||||||
|
- dog
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq eval '.[] as $foo | $foo' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
cat
|
||||||
|
dog
|
||||||
|
```
|
||||||
|
|
||||||
|
## Using variables as a lookup
|
||||||
|
Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...)
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
"posts":
|
||||||
|
- "title": Frist psot
|
||||||
|
"author": anon
|
||||||
|
- "title": A well-written article
|
||||||
|
"author": person1
|
||||||
|
"realnames":
|
||||||
|
"anon": Anonymous Coward
|
||||||
|
"person1": Person McPherson
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq eval '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
title: Frist psot
|
||||||
|
author: Anonymous Coward
|
||||||
|
title: A well-written article
|
||||||
|
author: Person McPherson
|
||||||
|
```
|
||||||
|
|
||||||
1
pkg/yqlib/doc/headers/Variable Operators.md
Normal file
1
pkg/yqlib/doc/headers/Variable Operators.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
For more complex scenarios, variables can be used to hold values of expression to be used in other expressions.
|
||||||
@@ -13,8 +13,8 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Context{}, err
|
return Context{}, err
|
||||||
}
|
}
|
||||||
|
//need to iterate backwards to ensure correct indices when deleting multiple
|
||||||
for el := nodesToDelete.MatchingNodes.Front(); el != nil; el = el.Next() {
|
for el := nodesToDelete.MatchingNodes.Back(); el != nil; el = el.Prev() {
|
||||||
candidate := el.Value.(*CandidateNode)
|
candidate := el.Value.(*CandidateNode)
|
||||||
|
|
||||||
deleteImmediateChildOp := &Operation{
|
deleteImmediateChildOp := &Operation{
|
||||||
|
|||||||
@@ -37,6 +37,22 @@ var deleteOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (doc)::[1, 3]\n",
|
"D0, P[], (doc)::[1, 3]\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `a: [1,2,3]`,
|
||||||
|
expression: `del(.a[])`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (doc)::a: []\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `a: [10,x,10, 10, x, 10]`,
|
||||||
|
expression: `del(.a[] | select(. == 10))`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (doc)::a: [x, x]\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Delete nested entry in array",
|
description: "Delete nested entry in array",
|
||||||
document: `[{a: cat, b: dog}]`,
|
document: `[{a: cat, b: dog}]`,
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ var variableOperatorScenarios = []expressionScenario{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Using variables as a lookup",
|
description: "Using variables as a lookup",
|
||||||
|
subdescription: "Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...)",
|
||||||
document: `{"posts": [{"title": "Frist psot", "author": "anon"},
|
document: `{"posts": [{"title": "Frist psot", "author": "anon"},
|
||||||
{"title": "A well-written article", "author": "person1"}],
|
{"title": "A well-written article", "author": "person1"}],
|
||||||
"realnames": {"anon": "Anonymous Coward",
|
"realnames": {"anon": "Anonymous Coward",
|
||||||
@@ -40,4 +41,5 @@ func TestVariableOperatorScenarios(t *testing.T) {
|
|||||||
for _, tt := range variableOperatorScenarios {
|
for _, tt := range variableOperatorScenarios {
|
||||||
testScenario(t, &tt)
|
testScenario(t, &tt)
|
||||||
}
|
}
|
||||||
|
documentScenarios(t, "Variable Operators", variableOperatorScenarios)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
name: yq
|
name: yq
|
||||||
version: '4.4.1'
|
version: '4.5.0'
|
||||||
summary: A lightweight and portable command-line YAML processor
|
summary: A lightweight and portable command-line YAML processor
|
||||||
description: |
|
description: |
|
||||||
The aim of the project is to be the jq or sed of yaml files.
|
The aim of the project is to be the jq or sed of yaml files.
|
||||||
|
|||||||
Reference in New Issue
Block a user