Skip to main content

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​

PsiYAMLHOCON
Comments//, /* … */ nesting##, //
A fieldkey: valuekey: valuekey: value, key = value, key { … }
Blocksbraces or indentation, mixedindentation, or {…} in flowbraces
Fields on one lineseparated by ,in flow, by ,by ,
A value with spacesquotedas writtenas written, the words joined
Typingnone: every value a stringnone in cfg-yaml, nulls asideTypesafe Config's: 1e5 is 100000.0
Nullnullnull, Null, NULL, ~null
a.b as a keynestsone keynests
A key with a dot in it`a.b`a.b"a.b"
Tags@t, any node!t, any nodenone
A key written twicemaps merge, the rest replacedthe last one, in cfg-yamlmaps 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—---—
Errorsall of them, each where it isthe first, thrownthe first, thrown
Input from anyonesafe, within limitssafe in cfg-yamltrusted only

Coming from YAML​

  • # starts a directive, not a comment: a comment is // or /* … */.
  • A value with whitespace is quoted: name: my service is an error, name: "my service" a value.
  • A tag is @t, not !t, and is the name alone in both: @duration 5s and YAML's !duration 5s give the tag duration. !!int, tag:yaml.org,2002:int in YAML, is written @`tag:yaml.org,2002:int` if it is needed at all.
  • - stands deeper than its key: YAML's hosts:⏎- a, the marker in the key's column, is an error. A map element is written as in YAML, its first field after the - .
  • null is 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 indicator width(n) (text over several lines).
  • Anchors and aliases are substitutions: &base needs nothing, *base is $base, and the merge key <<: *base is 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-yaml keeps 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 service into one value, Psi reports it.
  • Nothing is typed: 1e5 stays as written, where Typesafe Config gives 100000.0. Reading 30s as a duration or 512M as 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] and a += x are written with injections, [$a*, x], and ${base} { port: 2 } as $base* with port: 2 after 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 $HOME finds a key HOME in 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).
  • include is 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, .json and .properties of 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 in block finds block.x, and at the root only when nothing is there; in Psi $x finds x at the root, wherever the #include stands, and $.x the x beside it, in block (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