From 91ac643fbf806eaf10e1730b8aa98ebd2aa63931 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 1 Apr 2022 13:41:39 +1100 Subject: [PATCH] Added if statement tip and trick --- usage/tips-and-tricks.md | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/usage/tips-and-tricks.md b/usage/tips-and-tricks.md index 9ec94082..d6a3e5b8 100644 --- a/usage/tips-and-tricks.md +++ b/usage/tips-and-tricks.md @@ -141,6 +141,52 @@ yq 'with(.a.deeply ; .nested = "newValue" | .other= "newThing")' sample.yml The first argument expression sets the root context, and the second expression runs against that root context. + +## Logic without if/elif/else +`yq` has not yet added `if` expressions - however you should be able to use `with` and `select` to achieve the same outcome. Lets use an example: + +```yaml +- animal: cat +- animal: dog +- animal: frog +``` + +Now, if you were using good ol' jq - you may have a script with `if`s like so: + +```bash +jq ' .[] |= +if (.animal == "cat") then + .noise = "meow" | + .whiskers = true +elif (.animal == "dog") then + .noise = "woof" | + .happy = true +else + .noise = "??" +end +' < file.yaml +``` + +Using `yq` - you can get the same result by: + +```bash +yq '.[] |= ( + with(select(.animal == "cat"); + .noise = "meow" | + .whiskers = true + ) | + with(select(.animal == "dog"); + .noise = "woof" | + .happy = true + ) | + with(select(.noise == null); + .noise = "???" + ) +)' < file.yml +``` + +Note that the logic isn't quite the same, as there is no concept of 'else'. So you may need to put additional logic in the expressions, as this has for the 'else' logic. + ## yq adds a !!merge tag automatically The merge functionality from yaml v1.1 (e.g. `<<:`has actually been removed in the 1.2 spec. Thankfully, `yq` underlying yaml parser still supports that tag - and it's extra nice in that it explicitly puts the `!!merge` tag on key of the map entry. This tag tells other yaml parsers that this entry is a merge entry, as opposed to a regular string key that happens to have a value of `<<:`. This is backwards compatible with the 1.1 spec of yaml, it's simply an explicit way of specifying the type (for instance, you can use a `!!str` tag to enforce a particular value to be a string.