The new Bee Config API provides an alternative to using java.util.Properties in Scala programs. The API provides a very simple Scala API for loading configuration files. Simplicity is the key!
Often, java.util.Properties is still used in Scala applications, but it is often unsatisfactory. This API is broadly similar in intent, but pimped to work well in Scala.
Features
Config files are like properties files, except they are always Unicode. UTF8, UTF16 and UTF32 are supported (either word order); boms differentiate them. No other encodings are supported.
Configs can be loaded from files, from the classpath, from URLs, or from any other source that provides a java.io.InputStream.
Configs can be cascaded to provide fallback values. The cascades form linked lists of any length.
String interpolation allows values to contain other values, or system properties or environment variables.
String interpolation also allows keys to contain other values, or system properties or environment variables. This can be used for conditional configurations!
Config values are evaluated once during construction and thereafter are immutable and thread-safe.
A configuration is map-like and the full capability of Scala’s collections can be applied to their data.
Values can be enclosed by double-quote marks if whitespace is to be preserved in them. Otherwise any surrounding whitespace is trimmed.
Values can span multiple lines by ending each line with a single back-slash, then continuing on the next line. The newline characters are retained to form part of the value.
Values can contain comma-separated lists. These can use double-quotes to surround list elements, which may then contain commas. Backslash is used to mark the following character as a literal.
Comments are introduced by the ‘#’ character and run to the end of the line. They can appear anywhere that is not a double-quoted string.
Configs can contain sub-sections, and these are themselves configs. This provides, in effect, arbitrary nesting of sections. It is all controlled by key prefixes of your choosing.
Configs can contain INI-file sections, which are a simple alternative syntax for sub-sections.
No other external dependencies are needed.
Configs have a low memory footprint.
Reloading Changed Files
If you want to read configuration from a file once at startup, fine that’s easy. If you want to read configuration from a file (or similar) that may change from time to time and you want to load the changes, there is support for this too.
Why Bee Config? Why Not xxx?
Several compelling attempts have been made recently to produce a new configuration API. No disrespect to these, so why not…
Typesafe Config - Nice one, but it is not simple. Also, being Java, it’s a little awkward when Scala needs configuring.
Fig - Json might be popular but all those quote marks are a pain.
SnakeYaml (and other Yaml parsers) - a good idea but frustratingly I can’t persuade my colleagues to adopt this stuff.