Add {..path} references: render a node from elsewhere in the merged data root

This commit is contained in:
lilleman
2026-06-09 00:11:17 +02:00
parent 8db65ac598
commit 18de1b7004
4 changed files with 246 additions and 4 deletions
+17 -1
View File
@@ -221,6 +221,20 @@ form prefixes the century outside the checksummed core:
A function must be deterministic in the seeded rng (no wall-clock), so a seeded
faker stays reproducible.
**References.** A `{..path}` token renders a node from the **data root** instead
of a sibling field — the dot path is the one `Fake` takes, resolved across every
loaded directory. One category can borrow another, even across folders or layered
data dirs:
```json
{ "format": "Hej, {..en_US.person}!" }
```
renders e.g. `Hej, Pat Smith!`. References are bound when you create the faker, so
a path that is unknown, names a folder, or steps through a multi-variant choice
fails at `New`. A reference must not lead back to its own value (directly or
through a chain), or rendering won't terminate.
**Format string.** Every character is literal except:
| Token | Expands to |
@@ -232,8 +246,10 @@ faker stays reproducible.
| `#` | escape — the next char is literal (`#0``0`, `##``#`) |
| `{name}` | render the sibling field `name` |
| `{name()}` | call a built-in function (see **Functions**) |
| `{..path}` | render the node at a dot path from the data root (see **References**) |
`{a|b}` renders one of the sibling fields `a` or `b`, chosen at random.
`{a|b}` renders one of the sibling fields `a` or `b`, chosen at random; an arm
may be a `{..path}` reference too (`{name|..en_US.person}`).
**Putting it together** (`person.json`):