diff --git a/how-it-works.md b/how-it-works.md index f77a4cd9..fbc647be 100644 --- a/how-it-works.md +++ b/how-it-works.md @@ -1,8 +1,63 @@ -# How it works +# Expression Syntax: A Visual Guide +In `yq`, expressions are made up of operators and pipes. A context of nodes is passed through the expression, and each operation takes the context as input and returns a new context as output. That output is piped in as input for the next operation in the expression. -In `yq` expressions are made up of operators and pipes. A context of nodes is passed through the expression and each operation takes the context as input and returns a new context as output. That output is piped in as input for the next operation in the expression. To begin with, the context is set to the first yaml document of the first yaml file (if processing in sequence using eval). +Let's break down the process step by step using a diagram. We'll start with a single YAML document, apply an expression, and observe how the context changes at each step. -Lets look at a couple of examples. +Given a document like: + +```yaml +root: + items: + - name: apple + type: fruit + - name: carrot + type: vegetable + - name: banana + type: fruit +``` + +You can use dot notation to access nested structures. For example, to access the `name` of the first item, you would use the expression `.root.items[0].name`, which would return `apple`. + +But lets see how we could find all the fruit under `items` + +## Step 1: Initial Context +The context starts at the root of the YAML document. In this case, the entire document is the initial context. + +``` +root +└── items + ├── name: apple + │ type: fruit + ├── name: carrot + │ type: vegetable + └── name: banana + type: fruit +``` + +## Step 2: Splatting the Array +Using the expression `.root.items[]`, we "splat" the items array. This means each element of the array becomes its own node in the context: + +``` +Node 1: { name: apple, type: fruit } +Node 2: { name: carrot, type: vegetable } +Node 3: { name: banana, type: fruit } +``` + +## Step 3: Filtering the Nodes +Next, we apply a filter to select only the nodes where type is fruit. The expression `.root.items[] | select(.type == "fruit")` filters the nodes: + +``` +Filtered Node 1: { name: apple, type: fruit } +Filtered Node 2: { name: banana, type: fruit } +``` + +## Step 4: Extracting a Field +Finally, we extract the name field from the filtered nodes using `.root.items[] | select(.type == "fruit") | .name` This results in: + +``` +apple +banana +``` ## Simple assignment example @@ -44,7 +99,6 @@ a: dog b: dog ``` - ## Complex assignment, operator precedence rules Just like math expressions - `yq` expressions have an order of precedence. The pipe `|` operator has a low order of precedence, so operators with higher precedence will get evaluated first. @@ -73,7 +127,7 @@ name: sally fruit: mango ``` -To properly update this yaml, you will need to use brackets (think BODMAS from maths) and wrap the entire LHS: +**Important**: To properly update this YAML, you must wrap the entire LHS in parentheses. Think of it like using brackets in math to ensure the correct order of operations. `(.[] | select(.name == "sally") | .fruit) = "mango"` @@ -126,4 +180,4 @@ The assignment operator then copies across the value from the RHS to the value o ```yaml a: 2 b: thing -``` +``` \ No newline at end of file