Skip to main content

Psi: substitutions

$path is replaced by the node at path, written as keys joined by dots from the root of the tree, with indexes in brackets: $servers[0].host, or from where it stands after a dot, $.host (paths). Substitutions are resolved once every source is merged, so a value refers to its final state, whichever source set it.

A substitution is written two ways, by where it stands: $path outside quotes, and {$path} inside "…" and """…""", where it is always part of the string.

  • $path alone after a key, after a - or as an element of […] puts the node there whatever it is: a block, a sequence, a value. A tag written before it, @t $path, replaces the node's own, which it keeps otherwise; so does a tag before a directive's result. Marked with a *, $path*, it is an injection instead, and alone among fields it has to be one.
  • Inside a value, jdbc:$host/db or "jdbc:{$host}/db", it builds a string, and the node has to be a value. In quotes it does so even alone: "{$port}" is a string, never a graft.
  • $?path is optional: with nothing at path, the field is left out, an element of a sequence is dropped, and a part of a string, {$?path}, is empty. Written over an earlier value of its key, it leaves that value: port: 80 then port: $?env.port is 80 when nothing is at env.port.
  • Outside quotes a $ always opens a substitution, and one with no path after it is an error: a value holding a literal $ is quoted. Braces are not written there: ${path} is an error, and so are {$ and {# inside a word, x{$y}.
  • Inside "…" and """…""" only {$ opens a substitution, and {# a call; everything else is text there, $ and ${ included, so the shell's and Spring's "${HOME}" stand as written. \{ is a literal {, which \{$ and \{# need. A raw string, '…' or '''…''', has no substitutions at all.
db {
host: localhost
url: "jdbc:postgresql://{$db.host}:5432/orders"
}
replica: $db
backup: $db.host
user: $?env.user
file: "{$db.host}.log"
shell: "${HOME}/bin"
escaped: "\\{$not.a.path}"
raw: '{$not.a.path}'

reports

1 error in 1 source
app.psi
10:13: nothing at 'not.a.path' to substitute
hint: write '$?not.a.path' if it may be missing

Paths​

A path names a node by the keys and the indexes that lead to it from the root of the whole tree, or from where it stands when it starts with a dot.

  • Keys are joined by dots, $db.host. A key that is not bare, anything but a letter or _ followed by letters, digits, _ and -, is written in backticks, as it is after a key: $`akka.actor`.timeout, $`my key`. A backtick or a backslash in one is escaped with a backslash.
  • Indexes stand in brackets after a key or another index, $servers[0].host, $grid[1][0]. One after a - counts from the end, $servers[-1] being the last element. An index past either end finds nothing, as a missing key does: $s[5] and $s[-5] on three elements are errors, and $?s[-5] is left out.
  • A path starts at the root unless it starts with a dot, in a source read by #include too: $old is the root's old wherever it stands, never one beside it (caveats). Nothing is searched for, up the tree or anywhere else.
  • A dot starts it where it stands, at the block or the sequence around it, and each further dot goes a level up: $.old is the key old beside it, $..old one beside the block it stands in, and in a sequence $.[0] an element beside it. A dot too many, up past the root, is an error, and so are dots with no key or index after them. Many dots are a sign that the config is built wrong.
  • A relative path starts where it is written, and leads to one path from the root, as if written so there: in a source read by #include it starts where the #include stands, among a late call's arguments where the call stands, and in an element of a sequence at that element, wherever what an injection before it puts moves it. A copy, b: $a, takes what its references found where a stands, not beside b.
  • A path goes through what is resolved on the way: a key that a substitution, an injection or a late call sets is followed into what it gives, $replica.host with replica: $db. One that leads back to itself is a cycle.
  • Counting from the end needs the whole sequence, so a reference inside a sequence, or one that an element of it needs, cannot count from that sequence's end: a: [x, $a[-1]] is an error, while a: [x, $a[0]] is [x, x].
  • Outside quotes a path takes all it can, so $host/db is host and the text /db, but $host-name and $name.txt are paths whole. A path that has to end where it would go on is written in quotes, where the braces end it: "{$name}.txt".
servers: [{ host: a, port: 1 }, { host: b, port: 2 }]
`akka.actor` { timeout: 5s }
db: $servers[0]
first: $servers[0].host
last: $servers[-1].host
port: $db.port
timeout: $`akka.actor`.timeout
log: "{$db.host}.log"

loads as

servers:
- host: a
port: 1
- host: b
port: 2
`akka.actor`:
timeout: 5s
db:
host: a
port: 1
first: a
last: b
port: 1
timeout: 5s
log: a.log
service {
name: orders
db {
host: db.local
url: "jdbc:postgresql://{$.host}/{$..name}"
}
hosts: [$..db.host, { first: $..[0] }]
}
copy: $service.db

loads as

service:
name: orders
db:
host: db.local
url: jdbc:postgresql://db.local/orders
hosts:
- db.local
- first: db.local
copy:
host: db.local
url: jdbc:postgresql://db.local/orders

Injections​

An injection puts what a node holds in its place: the fields of a map among fields, the elements of a sequence among elements. It is marked with a * right after the substitution, $path* or $?path*, so that what a block or a sequence holds can be read off the text: [$a, $b] has two elements whatever a and b are, and [$a*, $b] the elements of a and then b.

WrittenWhat it is
key: $athe node itself, a graft
key: $a*a node of what a holds: a map of its fields, or a sequence of its elements
$a* alone among fieldsthe fields of a, a map
- $a*, […, $a*, …]the elements of a, a sequence
- $a, […, $a, …]one element
$a alone among fieldsan error
$a or $a* on a line among - elements with no - an error
  • Among the fields of a block, the node must be a map, and its fields stand there as if written there: what follows overrides them, what comes before is overridden, and blocks merge deep.
  • Among elements, after a - or in […], the node must be a sequence, and its elements stand there. Every line of a sequence written with - starts with one.
  • After a key, the node must be a map or a sequence, and the key takes a new one holding what it holds, without its tags: key: @t $a* tags the new node. A graft, key: $a, takes the node whole, tags included, and anything else.
  • Two or more alone in a block below a key are fields' places, each the fields of a map, merged in turn. A sequence of several is written with dashes: - $a* on each line.
  • An injection takes no tag among fields or elements: the fields and elements it puts keep their own.
  • $?path* with nothing at path puts nothing; after a key, the key is left out.
  • A directive injects with the same *, what it gives in place of the node: #include(file: "x")* among fields, - #d* among elements, key: #d* after a key. Unmarked it is one node, and alone among fields an error.
  • The * marks a whole value, element or field line: in a string, x$a*, $dir*.log or "{$a*}", it is an error. For the text * after a substitution, write it in quotes: "{$dir}*.log".
  • The node is taken as it ends up, every source merged. An injection at the top of a source is an error, unless the source is included in a block.
defaults { host: localhost, port: 5432, pool { size: 10, timeout: 30 } }
db {
$defaults*
port: 6432
pool { size: 20 }
}
monitoring { interval: 10s }
audited:
$db*
$monitoring*
hosts: [a, b]
all:
- primary
- $hosts*
- backup
flat: [$hosts*, $db.host]
groups: [$hosts, [backup]]
copy: $hosts*

loads as

defaults:
host: localhost
port: 5432
pool:
size: 10
timeout: 30
db:
host: localhost
port: 6432
pool:
size: 20
timeout: 30
monitoring:
interval: 10s
audited:
host: localhost
port: 6432
pool:
size: 20
timeout: 30
interval: 10s
hosts:
- a
- b
all:
- primary
- a
- b
- backup
flat:
- a
- b
- localhost
groups:
- - a
- b
- - backup
copy:
- a
- b

Formatters​

{$path; name(arguments)}, in quotes, turns the node at path into text with a formatter: "{$hosts; join(",")}" puts the hosts one after another, a , between them. A loader may be built with formatters of its own (Extending); join is the one every loader has.

  • join(separator) puts the elements of a sequence one after another with the separator between them, nothing for an empty one. Anything but a sequence, and an element that is no value, null included, are errors.
  • What a formatter gives is part of the string it stands in, which takes only the tag written before it. It is never an injection: {$hosts*; join(",")} is an error.
  • A call in a string takes a formatter as a path does, {#name(…); join(",")}, and has whatever it gives formatted.
  • {$?path; …} with nothing at path calls no formatter, and reads as the empty string. A field left out when the substitution finds nothing has no spelling with a formatter: "{$?hosts; join(",")}" is "".
  • A formatter is written in quotes only: after $path outside quotes a ; is a separator or text. A substitution or a call takes one formatter, and no whitespace before the ; or the }.
  • The arguments are literals, strings, integers and bare names, read by their own rules, so a " or a } inside one closes nothing: "hosts: {$hosts; join(", ")}" is one string. A substitution or a call in one is an error, unlike in a directive's.
hosts: [db1, db2, db3]
url: "jdbc:postgresql://{$hosts; join(",")}/orders"
list: @csv "{$hosts; join(", ")}"
banner: """
Serving {$hosts; join(" and ")}
"""

loads as

hosts:
- db1
- db2
- db3
url: "jdbc:postgresql://db1,db2,db3/orders"
list: @csv "db1, db2, db3"
banner: "Serving db1 and db2 and db3"