Skip to main content

Psi: caveats

What is easy to get wrong, each with the page that has the rule.

Whitespace does not separate​

Fields on one line, and elements in […], are separated by , (syntax):

server { host: localhost port: 8080 }
hosts: [a b]

reports

2 errors in 1 source
app.psi
1:26: expected ',' or a line break, found 'port:'
hint: separate items with ',' or a line break; whitespace never separates
2:11: expected ',' or a line break, found 'b'
hint: separate items with ',' or a line break; whitespace never separates

Nothing is typed​

null, unquoted, is the one reserved word. true, 007 and 1e5 are strings, kept as written, and so is "null" quoted (syntax). Reading them is the program's business.

# is not a comment​

A comment is // or /* … */. # starts a directive:

port: 8080 # the default

reports

2 errors in 1 source
app.psi
1:12: '#' with no directive name after it
hint: a comment is written '//' or '/* … */'
1:12: expected ',' or a line break, found '#'
hint: separate items with ',' or a line break; whitespace never separates

An unquoted $ is a substitution​

Outside quotes $ always opens a substitution, and a path takes all it can: $name.txt is the path name.txt, not name and the text .txt. A path that has to end there is written in quotes, where the braces end it, "{$name}.txt", and where a $ alone is text:

name: report
file: "{$name}.txt"
price: "$5"

loads as

name: report
file: report.txt
price: "$5"

${…} in quotes is text​

In quotes only {$ substitutes: "${HOME}/bin", as a shell, Spring or HOCON writes it, is the text ${HOME}/bin, with no error. A value written for HOCON loads with the text where a value was meant. Outside quotes ${…} is an error, which says to drop the braces.

home: /home/app
shell: "${home}/bin"
psi: "{$home}/bin"

loads as

home: /home/app
shell: "${home}/bin"
psi: /home/app/bin

\{# in quotes is a call​

In "…" and """…""", {$ opens a substitution and {# a call, so a Mustache, Handlebars or Jinja template there, with its {{#if}} and {# … #}, is read as calls. Write a template in '''…''', which substitutes nothing, or each {# as \{#:

raw: '''
{{#items}}{{name}}{{/items}}
'''
escaped: "{\{#items}}{{name}}{{/items}}"

loads as

raw: "{\{#items}}{{name}}{{/items}}"
escaped: "{\{#items}}{{name}}{{/items}}"

A substitution makes a call late​

A call with a substitution among its arguments is made once everything has merged, as a late call: what it gives is placed then, a lookup through it waits for it, and its errors come after every error of syntax. It cannot stand among the fields of the root, where it would build the tree it refers to; call it in a block. Its handler is given what a substitution finds, a map or a sequence as well as a scalar, and one that takes text reports the rest:

hosts: [a, b]
home: #env($hosts)

reports

1 error in 1 source
app.psi
2:7: '#env': argument 'name' takes a scalar, not a sequence

An optional substitution in a string can vanish​

$?path with nothing at path is left out: a field disappears, and an element is dropped. Inside a string it is empty, so that "jdbc:{$?db.host}/orders" silently loses its host, and a value in quotes is never left out: a: "{$?b}" is "" when b is missing, where a: $?b leaves a out. {$?path; join(",")} is no different: with nothing at path no formatter is called, and the string is empty.

A replacing value brings its own tag only​

Merged maps keep a tag, but a value that replaces another does not inherit one (merging):

timeout: @duration 5s
timeout: 10s

loads as

timeout: 10s

A value has one tag​

A value carries one tag at most, so tags never add up: @a @b x is an error, and maps merged keep the later's tag, not both. Two sources that tag one map differently leave it with the tag of the source loaded last (merging):

pool: @pooled { max: 10 }
pool: @readonly { min: 1 }

loads as

pool: @readonly
max: 10
min: 1

A key written twice merges​

: merges a map into the one before it, so keys an earlier definition gave stay. := replaces, and is the one way to drop them (merging).

A path with no dot starts at the root​

$path is looked for from the root of the whole tree, never from the block it stands in: a block refers to a key beside it with a dot, $.old, or by the full path, $config.old, not $old (paths). Nothing is searched for up the tree.

config {
old { user: app }
new: $config.old
beside: $.old
wrong: $old
}

reports

1 error in 1 source
app.psi
5:10: nothing at 'old' to substitute
hint: write '$?old' if it may be missing

The same holds in a source read by #include: a path in it starts at the root of the tree it is included into, not at the block the #include stands in, where HOCON would look first. A source that refers to its own keys writes them with a dot, $.x, which starts where it is included.

A relative path counts the elements written​

Below an element of a sequence, $.x finds its own element wherever it ends up, and $..[1] the second element written, whatever an injection before them brings in. The path it is told by, in an error or as the position of the node a formatter is given, is the one written, s[1].x below, not where the element stands once loaded, s[2]: knowing that would wait for the injections before it.

prefix: [a, b]
s: [$prefix*, { y: $.x }]

reports

1 error in 1 source
app.psi
2:20: nothing at '.x' (s[1].x) to substitute
hint: write '$?.x' if it may be missing

Substitutions see the end​

A substitution is resolved once every source is merged, so it refers to a value as the last source left it, not as it stood where the substitution is written (substitutions):

host: localhost
url: "http://{$host}/"
host: example.com

loads as

host: example.com
url: http://example.com/

An index from the end needs the whole sequence​

$s[-1] is the last element of s once every source is merged, and a key written twice replaces a sequence, never appends to it: of two sources each setting hosts, it is the last one's last host. A sequence gathered from several is written with injections, [$a.hosts*, $b.hosts*]. Counting from the end needs every element, so an element that counts from the end of its own sequence is an error (substitutions):

hosts: [db1, db2]
hosts: [db3]
last: $hosts[-1]

loads as

hosts:
- db3
last: db3
base: [db1, db2]
site: [db3]
hosts: [$base*, $site*]
last: $hosts[-1]

loads as

base:
- db1
- db2
site:
- db3
hosts:
- db1
- db2
- db3
last: db3
all: [x, $all[-1]*]

reports

1 error in 1 source
app.psi
1:10: '$all[-1]' counts from the end of a sequence that needs it to know where its end is
hint: an index from 0 needs only the elements before it; or refer to the sequence from outside it

A second injection below a key makes a map​

Below a key, one injection alone is the key's value, a:⏎ $s* meaning a: $s*, and takes a map or a sequence. A second line makes the block a map, a line with no - being a field's place, so each injection then has to find a map (injections). A line added below one that worked can break it; a sequence of several is written with dashes, - $s* on each line:

s: [a, b]
m { x: 1 }
one:
$s*
two:
$s*
$m*

reports

1 error in 1 source
app.psi
6:3: '$s' injected among fields is not a map; it is defined at app.psi:1:4

A field added below a substitution drops its tag​

A substitution alone below a key is the key's value, and brings the tag of what it finds, as key: $b does. With a field below it, it has to be an injection, $b*, which brings the fields and no tag. The fields come out the same either way; the tag stays only when the field is merged in from elsewhere:

b: @t { y: 1 }
value:
$b
merged:
$b
merged.x: 1
injected:
$b*
x: 1

loads as

b: @t
y: 1
value: @t
y: 1
merged: @t
y: 1
x: 1
injected:
y: 1
x: 1

To keep the tag, write it again, injected: @t, or merge the field in rather than write it below.

A * after a substitution marks it​

$prefix*, a whole value, is an injection, not the text of prefix followed by *, and in a longer value, $dir*.log, it is an error. A glob or any other text with a * right after a substitution is written in quotes, where the braces end the path before it:

dir: /var/log
logs: $dir*.log

reports

1 error in 1 source
app.psi
2:7: a '*' marks an injection, and a string cannot hold one
hint: write '$a*' alone outside quotes to inject, or '"{$a}*"' for the text '*' after a substitution
dir: /var/log
logs: "{$dir}/*.log"
prefix: app
pattern: "{$prefix}*"

loads as

dir: /var/log
logs: /var/log/*.log
prefix: app
pattern: app*

A formatter needs quotes​

{$path; name(…)} formats only in quotes (formatters). Outside them, $hosts is a substitution and ;join(-) text after it, a concatenation, and the error says that a sequence is no part of a string, with no word of a formatter:

hosts: [a, b]
bare: $hosts;join(-)

reports

1 error in 1 source
app.psi
2:7: '$hosts' in a string is not a scalar; it is defined at app.psi:1:8

In quotes, $hosts with no braces is text, and the value is taken as written:

hosts: [a, b]
quoted: "$hosts; join(-)"

loads as

hosts:
- a
- b
quoted: "$hosts; join(-)"

A formatter gives a string, and no tag​

A formatted substitution is part of a string, never the fields or elements of what it finds: marked with a *, {$hosts*; join(",")}, it is an error. Nor does the string keep the node's tag, as $path does: it has only the one written before it:

hosts: @hosts [a, b]
copy: $hosts
joined: "{$hosts; join(",")}"
tagged: @csv "{$hosts; join(",")}"

loads as

hosts: @hosts
- a
- b
copy: @hosts
- a
- b
joined: "a,b"
tagged: @csv "a,b"

An empty block is written​

key: at the end of a line needs something deeper below it; an empty block is {} or [] (indentation).

#hocon reads one file​

With no extension, #hocon(file: app) reads the first of app.conf, app.json and app.properties it finds, and only that one (importing). Typesafe Config's own include "app" merges every one of them, .conf over .json over .properties. To have that, write one #hocon for each, with its extension, in that order from .properties to .conf, so that the later wins, and optional: true on those that may be missing.

Two references setting one key​

standard() and withReferences merge every reference on the class path, those of one name in the order of the class path (loading). Nothing fixes that order, so two libraries that set one key are settled by where their jars stand, silently, as in Typesafe Config. The program's own configuration, merged over them, settles it for good.

A reference resolves with the application​

A reference is resolved with everything else, not on its own as Typesafe Config requires: it sees what the application overrides, and it may refer to a key only the application sets. Loaded without such an application, it fails at the reference, in the library's jar.