Skip to main content

Psi: directives

A directive is #name(arguments), or #name with none: a call the loader makes where it stands, which gives a value, or marked with a *, #name(arguments)*, the fields of a map among fields or the elements of a sequence among elements; in quotes, {#name(arguments)} puts what it gives in the string (in a string). Psi has three, #prop, #env and #include, and a program can add its own or turn one off. An argument is any value a field can have, a substitution included (arguments). Where a directive stands decides what it may give, as for a substitution: injections say how.

Arguments​

Positional arguments come first, then named ones, written as fields are: name: value, or name { … } for a block. An argument is any value a field can have: a scalar, null, a string in quotes, """…""" included, a block, a sequence, a tag, another call, a substitution.

  • A scalar reaches the handler as a string, as everywhere in Psi: 2 and "2" arrive the same. Unquoted, it ends at a ) as at a ,.
  • A * at the top of an argument is an error, #d($a*): the arguments have no fields or elements of their own to inject into. Inside a block or a sequence an argument holds, a * is what it is anywhere, #d([$a*, b]).
  • A call in an argument is made first, and the outer call is given what it gave.
  • A handler reads what it is given, and reports what it cannot take: #env(name: [a]) is an error of #env's.

A call with a substitution among its arguments is late: directives are made while the source is read, before anything merges, and what a substitution refers to exists only once everything has merged. Such a call is made then, as a substitution standing where it stands would be resolved:

  • Its arguments are resolved first, and each is given as found, a map or a sequence as well as a scalar. An optional substitution that finds nothing gives no argument, so that the parameter is as if left out.
  • What it gives is placed as an early call's is, at a key, injected with a *, or in a string; and everything said of a substitution there holds of it: a merge with it, and a lookup through it, wait for it, and a cycle through its arguments is a cycle of substitutions.
  • It is made once where it stands, and a substitution of it copies what it gave.
  • Its errors, and those of a source it reads, come with the substitutions', after every error of syntax.
  • Among the fields of the root it is an error: it would be an injection that refers to the tree it builds.
  • A call in its arguments is late too when what that call gave holds a substitution, or is late itself.

Every directive may be late, #include included: #include(file: "{$env}.psi")* reads the file the config names.

db {
name: orders
user: #env("{$db.prefix}_USER", default: $db.name)
password: #env("{$db.prefix}_PASSWORD", optional: true)
prefix: ORDERS
}

loads as

db:
name: orders
user: app
prefix: ORDERS

Values set at launch​

Nothing set at launch reaches a source on its own: no system property overrides a value, and the environment is not read. A source takes such a value where it says so:

CallGives
#prop("db.host")the system property db.host; a name with a dot is quoted
#prop(java)with no property java, a block of every property under java.: version, vm { name, … }
#env(HOME)the environment variable HOME
…, default: localhostthe default when it is not set
…, optional: truenothing when it is not set, so that the field is left out

A value not set is an error otherwise. Left out, a field leaves an earlier definition of its key standing, which makes a default of its own. With -Ddb.host=db.prod, and no DB_PORT in the environment:

db {
host: localhost
host: #prop("db.host", optional: true)
port: #env(DB_PORT, default: 5432)
url: "{$db.host}:{$db.port}"
}

loads as

db:
host: db.prod
port: 5432
url: db.prod:5432

In a string​

Inside "…" and """…""", {#name(arguments)} puts what the call gives in the string, as {$path} puts what a substitution finds: "{#env(HOME)}/logs" needs no key to hold the variable.

  • What it gives must be a value: a block, a sequence or null is an error at the call, unless it is formatted. A call that gives nothing, optional: true with nothing set, reads as the empty string.
  • A formatter after it, {#name(arguments); f(…)}, turns whatever it gives into text, as after a path: "{#yaml(file: hosts); join(",")}" joins the hosts a YAML file lists. A call that gives nothing calls no formatter.
  • Its arguments are read as outside quotes, so a " or a } inside a string argument closes neither the string nor the call: "{#env(name: "HOME")}/logs" is one string. A call left open at the end of the string is an error.
  • It is part of a string, never an injection: {#d*} is an error, with a formatter or without. Outside quotes a call is never part of a word: x{#d} is an error, and x#d is text.
  • \{# is the text {#, which a Mustache or a Jinja template in "…" needs, {{#if}} written {\{#if}}; a raw string, '…' or '''…''', takes it as written.
logs: "{#env(LOG_DIR, default: "/var/log")}/orders"
home: "{#prop("user.home", optional: true)}/.orders"
hosts: "{#prop("db.hosts", default: [db1, db2]); join(",")}"
template: '{{#if user}}hi{{/if}}'

loads as

logs: /srv/log/orders
home: /.orders
hosts: "db1,db2"
template: "{\{#if user}}hi{{/if}}"

Including other sources​

#include reads another Psi source where it stands. Among fields, marked, #include(file: "x")*, it merges the source's fields into the block, so the fields after it override them; as a value it makes the source a block.

ArgumentReads
file: "conf.d/db"a file, relative to the source that includes it,
resource: "defaults"a resource on the program's class path
url: "https://…"a URL
optional: truenothing when the target is not there, rather than an error

A file or resource with no extension is looked for as .Ψ, .ψ and .psi, in that order. A URL that takes more than 10 seconds to connect, or 30 to send its next bytes, is an error. Every include is read again, so the limits on reading below count it each time.

With defaults.psi:

server { host: localhost, port: 8080 }
log.level: INFO

and no local.psi next to it,

#include(file: "defaults")*
server.port: 9090
#include(file: "local", optional: true)*

loads as

server:
host: localhost
port: 9090
log:
level: INFO

Importing YAML and HOCON​

Two modules of their own give a loader directives that read a YAML or a HOCON file where they stand, as #include reads a Psi one: cfg-psi-yaml and cfg-psi-hocon. Each is registered under the name the files call it by:

libraryDependencies += "io.h8" %% "cfg-psi-yaml" % "0.6.0"

val loader = Psi.builder.withDirective("yaml", h8io.cfg.impl.psi.yaml.Yaml).build
  • The target is written as #include's: file, resource or url, and optional. A file or a resource with no extension is looked for as .yaml and then .yml by #yaml, as .conf, .json and .properties by #hocon, the first found read.
  • What it gives is the file's whole tree, every value a string. A YAML file may hold any node at its root, a sequence or a single value as well as a mapping, and keeps the tags written in it.
  • A HOCON file is resolved on its own: its substitutions inside it, with no system property, no environment variable and no reference.conf reaching it. A value set at launch comes in by #prop and #env only, as everywhere in Psi.
  • Psi's substitutions refer to what an import gives as to anything else, once it is merged; they do not reach into the file while it is read.
  • A file that cannot be read or parsed is the directive's error, with what the reader said of it.
  • A YAML file is read under the limits on reading below. A HOCON file, and what it includes, Typesafe Config reads itself, past them, and with no limit on how deep it goes: import HOCON you trust.

With db.yaml holding host: localhost and port: !int 5432, hosts.yaml the list - a.example and - b.example, and cache.conf holding size: 100 and ttl: ${size}s:

db: #yaml(file: db)
hosts: #yaml(file: hosts)
cache: #hocon(file: cache)
db.host: db.prod

loads as

db:
host: db.prod
port: @int 5432
hosts:
- a.example
- b.example
cache:
size: 100
ttl: 100s