1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Added tag operator

This commit is contained in:
Mike Farah
2020-11-19 16:45:05 +11:00
parent 9b48cf80e0
commit 36084a60a9
16 changed files with 241 additions and 120 deletions

View File

@@ -20,7 +20,8 @@ yq eval '{"wrap": .}' sample.yml
```
will output
```yaml
wrap: {name: Mike}
wrap:
name: Mike
```
### Using splat to create multiple objects
@@ -62,9 +63,7 @@ will output
```yaml
Mike: cat
Mike: dog
---
Rosey: monkey
---
Rosey: sheep
```

View File

@@ -101,7 +101,7 @@ yq eval '. | headComment' sample.yml
```
will output
```yaml
welcome!
```
### Get foot comment
@@ -115,6 +115,6 @@ yq eval '. | footComment' sample.yml
```
will output
```yaml
have a great day
```

View File

@@ -12,7 +12,7 @@ yq eval 'del(.b)' sample.yml
```
will output
```yaml
{a: cat}
a: cat
```
### Delete entry in array
@@ -28,7 +28,8 @@ yq eval 'del(.[1])' sample.yml
```
will output
```yaml
[1, 3]
- 1
- 3
```
### Delete no matches
@@ -43,6 +44,7 @@ yq eval 'del(.c)' sample.yml
```
will output
```yaml
{a: cat, b: dog}
a: cat
b: dog
```

View File

@@ -49,7 +49,6 @@ will output
```yaml
match: cat
doc: 0
---
match: frog
doc: 1
```

View File

@@ -13,7 +13,9 @@ yq eval 'explode(.f)' sample.yml
```
will output
```yaml
{f: {a: cat, b: cat}}
f:
a: cat
b: cat
```
### Explode with no aliases or anchors
@@ -43,7 +45,9 @@ yq eval 'explode(.f)' sample.yml
```
will output
```yaml
{f: {a: cat, cat: b}}
f:
a: cat
cat: b
```
### Explode with merge anchors

View File

@@ -1,23 +1,9 @@
This operator recursively matches all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches, for instance to set the `style` of all nodes in a yaml doc:
```bash
yq eval '.. style = "flow"' file.yaml
yq eval '.. style= "flow"' file.yaml
```
## Examples
### Matches single scalar value
Given a sample.yml file of:
```yaml
cat
```
then
```bash
yq eval '..' sample.yml
```
will output
```yaml
cat
```
### Map
Given a sample.yml file of:
```yaml

View File

@@ -146,39 +146,7 @@ c: 3.2
e: true
```
### Set style using a path
Given a sample.yml file of:
```yaml
a: cat
b: double
```
then
```bash
yq eval '.a style=.b' sample.yml
```
will output
```yaml
a: "cat"
b: double
```
### Example 8
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
yq eval '.. style=""' sample.yml
```
will output
```yaml
a: cat
b: dog
```
### Example 9
### Read style
Given a sample.yml file of:
```yaml
a: cat
@@ -193,20 +161,5 @@ will output
```
### Example 10
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '.. | style' sample.yml
```
will output
```yaml
```

View File

@@ -0,0 +1,45 @@
The tag operator can be used to get or set the tag of ndoes (e.g. `!!str`, `!!int`, `!!bool`).
## Examples
### Get tag
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
f: []
```
then
```bash
yq eval '.. | tag' sample.yml
```
will output
```yaml
!!map
!!str
!!int
!!float
!!bool
!!seq
```
### Convert numbers to strings
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '(.. | select(tag == "!!int")) tag = "!!str"' sample.yml
```
will output
```yaml
a: cat
b: "5"
c: 3.2
e: true
```

View File

@@ -29,21 +29,3 @@ fieldA
fieldC
```
### Combine selected paths
Given a sample.yml file of:
```yaml
a: fieldA
b: fieldB
c: fieldC
```
then
```bash
yq eval '(.a, .c) |= "potatoe"' sample.yml
```
will output
```yaml
a: potatoe
b: fieldB
c: potatoe
```

View File

@@ -0,0 +1 @@
The tag operator can be used to get or set the tag of ndoes (e.g. `!!str`, `!!int`, `!!bool`).