Document 4 — Introducing Children Recursive Nesting

Recursive nesting: adding child nodes (customer.email, product.code) without adding any special-case algorithms.

So far, every node has been simple. Now we make the structure slightly more interesting.

We will do two things:

The traversal engine itself still does not change.

1. Add a Child to an Existing Node

Start with the existing customer node:

{
  "customer": {
    "name": "Keshav"
  },
  "order": {
    "id": "ORD-101"
  }
}

Now give customer another child: email.

{
  "customer": {
    "name": "Keshav",
    "email": "keshav@example.com"
  },
  "order": {
    "id": "ORD-101"
  }
}

2. Add a New Root with Its Own Child

Now add product at the root, and give it a child called code:

{
  "customer": {
    "name": "Keshav",
    "email": "keshav@example.com"
  },
  "order": {
    "id": "ORD-101"
  },
  "product": {
    "code": "P-100"
  }
}

3. Describe the New Structure with Instructions

{
  "customer": {
    "name": {
      "transform": "logPath"
    },
    "email": {
      "transform": "logPath"
    }
  },
  "order": {
    "id": {
      "transform": "logPath"
    }
  },
  "product": {
    "code": {
      "transform": "logPath"
    }
  }
}

Notice what happened. We did not add another traversal algorithm for children. The same traversal simply continues deeper into the source.

customer → email
product → code

4. The Function Still Does Not Change

function logPath(value, path) {
  console.log("I am here in the tree:", path, value);
}

The function knows nothing about customer, email, product, or code. Those values come from the source at runtime.

5. The Traversal Reaches the New Locations

I am here in the tree: customer.name Keshav
I am here in the tree: customer.email keshav@example.com
I am here in the tree: order.id ORD-101
I am here in the tree: product.code P-100

The Point

The structure has become deeper, but the traversal has not become more complicated.

The Mental Model: A child is not a special case that requires a new algorithm. It is simply another source node encountered during traversal.