Psi, YAML and HOCON
Psi takes its ideas from YAML and HOCON — indentation and tags from one, merging, substitutions and includes from the other — and compatibility from neither (Why cfg). A YAML or HOCON file is not a Psi source, and most of what one writes differently fails loudly in Psi rather than meaning something else. A file kept in either format is imported as it is.
At a glance
| Psi | YAML | HOCON | |
|---|---|---|---|
| Comments | //, /* … */ nesting | # | #, // |
| A field | key: value | key: value | key: value, key = value, key { … } |
| Blocks | braces or indentation, mixed | indentation, or {…} in flow | braces |
| Fields on one line | separated by , | in flow, by , | by , |
| A value with spaces | quoted | as written | as written, the words joined |
| Typing | none: every value a string | none in cfg-yaml, nulls aside | Typesafe Config's: 1e5 is 100000.0 |
| Null | null | null, Null, NULL, ~ | null |
a.b as a key | nests | one key | nests |
| A key with a dot in it | `a.b` | a.b | "a.b" |
| Tags | @t, any node | !t, any node | none |
| A key written twice | maps merge, the rest replaced | the last one, in cfg-yaml | maps merge, the rest replaced |
| Dropping keys set before | := | — | a value that is no map first |
| Appending to a sequence | [$a*, x] | — | +=, ${a} [x] |
| Reuse | $path, $path* | anchors and aliases, << | ${path}, concatenation |
A substitution in "…" | {$path} | — | none: ${…} there is text, as in Psi |
| System properties, the environment | #prop, #env, where written | — | override values, and fill substitutions the tree lacks |
| Includes | #include(…), anywhere | — | include "…" |
| Text over several lines | """…""", indent stripped, escapes; '''…''' raw | |, > | """…""", raw, as written |
| Several documents in a file | — | --- | — |
| Errors | all of them, each where it is | the first, thrown | the first, thrown |
| Input from anyone | safe, within limits | safe in cfg-yaml | trusted only |
Coming from YAML
#starts a directive, not a comment: a comment is//or/* … */.- A value with whitespace is quoted:
name: my serviceis an error,name: "my service"a value. - A tag is
@t, not!t, and is the name alone in both:@duration 5sand YAML's!duration 5sgive the tagduration.!!int,tag:yaml.org,2002:intin YAML, is written@`tag:yaml.org,2002:int`if it is needed at all. -stands deeper than its key: YAML'shosts:⏎- a, the marker in the key's column, is an error. A map element is written as in YAML, its first field after the-.nullis the one null:~is the string~.'…'escapes with a backslash:'it\'s', where YAML writes'it''s'. A\in YAML's single quotes is text, in Psi's an escape only before'or\.- Block scalars are triple quotes: YAML's
|is"""…"""or'''…''', its>the marker+, and its indentation indicatorwidth(n)(text over several lines). - Anchors and aliases are substitutions:
&baseneeds nothing,*baseis$base, and the merge key<<: *baseis the injection$base*, its fields overridden by those written after it. Unlike an alias, a substitution sees the tree as every source left it (caveats). - A key written twice merges when both are maps, where YAML calls it an error and
cfg-yamlkeeps the last one. - What YAML lacks: substitutions in text,
jdbc:$host/db; includes; values set at launch;:=.
server:
name: my service # the name
timeout: !duration 5s
hosts:
- a
- b
reports
5 errors in 1 source
app.psi
2:12: a second word, 'service', after a value
hint: quote a value with whitespace in it, as in "foo bar"
3:22: a second word, '5s', after a value
hint: quote a value with whitespace in it, as in "foo bar"
4:7: nothing indented deeper below to be the value
hint: indent the value on the lines below, or write it on this line; an empty block is '{}' or '[]'
5:1: expected a key, found '- '
6:1: expected a key, found '- '
is written in Psi
server:
name: "my service" // the name
timeout: @duration 5s
hosts:
- a
- b
loads as
server:
name: "my service"
timeout: @duration 5s
hosts:
- a
- b
Coming from HOCON
- A field is
key: value:=is an error.key { … }stays. - Keys with dots in them go in backticks:
`akka.actor`: x, where HOCON quotes,"akka.actor" = x. Double quotes hold values only. - A value with whitespace is quoted: HOCON joins
my serviceinto one value, Psi reports it. - Nothing is typed:
1e5stays as written, where Typesafe Config gives100000.0. Reading30sas a duration or512Mas a size is the program's business, as reading a number is. - A substitution is
$host, and goes inside the string:"jdbc:{$host}/db"substitutes, HOCON's"jdbc:"${host}"/db"is an error. Outside quotes$always opens one, with no braces, so a literal$is quoted and${host}is an error; in quotes${host}is text, as in HOCON. - No concatenation of blocks or sequences: HOCON's
${a} [x]anda += xare written with injections,[$a*, x], and${base} { port: 2 }as$base*withport: 2after it. - No self-reference:
path: "{$path}:/bin"is a cycle. A value builds on another key, not on its own earlier value. - Nothing comes from the launch on its own: no system property overrides a value, and
$HOMEfinds a keyHOMEin the tree or nothing. A source says what it takes,home: #env(HOME),host: #prop("db.host")or, in a string,"{#env(HOME)}/bin"(values set at launch). includeis a directive:include "defaults"is#include(file: "defaults")*, and it may stand in any block. With no extension it reads the first Psi source it finds, one of.Ψ,.ψand.psi, where HOCON merges the.conf,.jsonand.propertiesof that name.- A path starts at the root or, after a dot, where it stands, in an included source too: HOCON looks for a path
of an included file first below the place of the include, so that
${x}in a file included inblockfindsblock.x, and at the root only when nothing is there; in Psi$xfindsxat the root, wherever the#includestands, and$.xthexbeside it, inblock(caveats). - A reference is resolved with the application, not on its own (caveats).
- Triple quotes strip the indent and take escapes and substitutions: HOCON's
"""…"""is raw and kept as written; Psi's'''…'''is raw, its indent stripped all the same. #starts a directive: a comment is//, as in HOCON, or/* … */.
db {
host = localhost
url: "jdbc:postgresql://"${db.host}"/orders"
"pool.size": 16 # per node
}
name: my service
reports
7 errors in 1 source
app.psi
2:8: expected ':', ':=' or '{' after a key, found '='
hint: write a field as 'key: value' or 'key { … }'
3:28: '${' outside quotes, where a substitution is written with no braces
hint: write '$path', or quote the value when the braces end the path, as in "{$name}.txt"
3:28: expected ',' or a line break, found '${db.host}"/orders"'
hint: separate items with ',' or a line break; whitespace never separates
3:38: a quote inside an unquoted value
hint: quote the whole value in "…"
3:46: a quote inside an unquoted value
hint: quote the whole value in "…"
4:3: a key in quotes
hint: write a key bare, or in backticks when it has to be quoted: `pool.size`
6:10: a second word, 'service', after a value
hint: quote a value with whitespace in it, as in "foo bar"
is written in Psi, path = ${HOME}/bin added, with HOME set to /home/app
db {
host: localhost
url: "jdbc:postgresql://{$db.host}/orders"
`pool.size`: 16 // per node
}
name: "my service"
path: "{#env(HOME)}/bin"
loads as
db:
host: localhost
url: jdbc:postgresql://localhost/orders
`pool.size`: 16
name: "my service"
path: /home/app/bin