try updating add

This commit is contained in:
Mike Farah 2021-10-30 14:23:02 +11:00
parent fbe266a166
commit ab732e0cc6

View File

@ -1,7 +1,6 @@
# Add # Add
Add behaves differently according to the type of the LHS: Add behaves differently according to the type of the LHS:
* arrays: concatenate * arrays: concatenate
* number scalars: arithmetic addition * number scalars: arithmetic addition
* string scalars: concatenate * string scalars: concatenate
@ -9,9 +8,7 @@ Add behaves differently according to the type of the LHS:
Use `+=` as append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`. Use `+=` as append assign for things like increment. Note that `.a += .x` is equivalent to running `.a = .a + .x`.
## Concatenate and assign arrays ## Concatenate and assign arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
val: thing val: thing
@ -19,15 +16,11 @@ a:
- cat - cat
- dog - dog
``` ```
then then
```bash ```bash
yq eval '.a.b += ["cow"]' sample.yml yq eval '.a.b += ["cow"]' sample.yml
``` ```
will output will output
```yaml ```yaml
a: a:
val: thing val: thing
@ -38,9 +31,7 @@ a:
``` ```
## Concatenate arrays ## Concatenate arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
- 1 - 1
@ -49,15 +40,11 @@ b:
- 3 - 3
- 4 - 4
``` ```
then then
```bash ```bash
yq eval '.a + .b' sample.yml yq eval '.a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
- 1 - 1
- 2 - 2
@ -66,68 +53,50 @@ will output
``` ```
## Concatenate null to array ## Concatenate null to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
- 1 - 1
- 2 - 2
``` ```
then then
```bash ```bash
yq eval '.a + null' sample.yml yq eval '.a + null' sample.yml
``` ```
will output will output
```yaml ```yaml
- 1 - 1
- 2 - 2
``` ```
## Add new object to array ## Add new object to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
- dog: woof - dog: woof
``` ```
then then
```bash ```bash
yq eval '.a + {"cat": "meow"}' sample.yml yq eval '.a + {"cat": "meow"}' sample.yml
``` ```
will output will output
```yaml ```yaml
- dog: woof - dog: woof
- cat: meow - cat: meow
``` ```
## Add string to array ## Add string to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
- 1 - 1
- 2 - 2
``` ```
then then
```bash ```bash
yq eval '.a + "hello"' sample.yml yq eval '.a + "hello"' sample.yml
``` ```
will output will output
```yaml ```yaml
- 1 - 1
- 2 - 2
@ -135,9 +104,7 @@ will output
``` ```
## Append to array ## Append to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
- 1 - 1
@ -146,15 +113,11 @@ b:
- 3 - 3
- 4 - 4
``` ```
then then
```bash ```bash
yq eval '.a = .a + .b' sample.yml yq eval '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: a:
- 1 - 1
@ -167,9 +130,7 @@ b:
``` ```
## Append another array using += ## Append another array using +=
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
- 1 - 1
@ -178,15 +139,11 @@ b:
- 3 - 3
- 4 - 4
``` ```
then then
```bash ```bash
yq eval '.a += .b' sample.yml yq eval '.a += .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: a:
- 1 - 1
@ -199,9 +156,7 @@ b:
``` ```
## Relative append ## Relative append
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: a:
a1: a1:
@ -212,15 +167,11 @@ a:
- dog - dog
a3: {} a3: {}
``` ```
then then
```bash ```bash
yq eval '.a[].b += ["mouse"]' sample.yml yq eval '.a[].b += ["mouse"]' sample.yml
``` ```
will output will output
```yaml ```yaml
a: a:
a1: a1:
@ -235,109 +186,82 @@ a:
``` ```
## String concatenation ## String concatenation
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
b: meow b: meow
``` ```
then then
```bash ```bash
yq eval '.a = .a + .b' sample.yml yq eval '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: catmeow a: catmeow
b: meow b: meow
``` ```
## Number addition - float ## Number addition - float
If the lhs or rhs are floats then the expression will be calculated with floats. If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: 3 a: 3
b: 4.9 b: 4.9
``` ```
then then
```bash ```bash
yq eval '.a = .a + .b' sample.yml yq eval '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: 7.9 a: 7.9
b: 4.9 b: 4.9
``` ```
## Number addition - int ## Number addition - int
If both the lhs and rhs are ints then the expression will be calculated with ints. If both the lhs and rhs are ints then the expression will be calculated with ints.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: 3 a: 3
b: 4 b: 4
``` ```
then then
```bash ```bash
yq eval '.a = .a + .b' sample.yml yq eval '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: 7 a: 7
b: 4 b: 4
``` ```
## Increment numbers ## Increment numbers
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: 3 a: 3
b: 5 b: 5
``` ```
then then
```bash ```bash
yq eval '.[] += 1' sample.yml yq eval '.[] += 1' sample.yml
``` ```
will output will output
```yaml ```yaml
a: 4 a: 4
b: 6 b: 6
``` ```
## Add to null ## Add to null
Adding to null simply returns the rhs Adding to null simply returns the rhs
Running Running
```bash ```bash
yq eval --null-input 'null + "cat"' yq eval --null-input 'null + "cat"'
``` ```
will output will output
```yaml ```yaml
cat cat
``` ```