Easy configuration with erlang
19 February 2009
Erlang isn’t the easiest of languages to pass around a set of configuration. You can always write a gen_server to handle holding configuration for you, but why? That’s just another running process and what happens if it dies? You have to account for that, etc.
I’ve been working on a framework stack in Erlang for a current project of mine and I’m delighted to share it for posterity.
Introducing: Configerl.
Configerl is my library I use for projects to pull out configuration. I’ll show you what I mean:
Say I have a list of tuples intended for configuration purposes, like so:
Config = [{port, 12345}, {name, "converse"}].
With configerl you can pull out and parse the different configurations easily:
config:parse(port, Config) => 12345.
What about getting with a default?
config:parse_or_default(port, Config, "ted").
Yes yes, nothing so grand about that, but what about if you want to merge configurations?
config:append([{port, 12345}, {name, "converse"}], [{friends, "whisper"}]).
What about updating configurations:
config:update({port, 54321}, Config).
How about removing a configuration:
config:delete(port, Config).
And now we get to the really fun stuff:
Fetching multiple keys
[Port, Name] = config:fetch([port, name], Config).
Fetching multiple keys with defaults
[Port, Name] = config:fetch_with_default([port, name], Config, [12345, "converse"]).
Fetching multiple keys with a default configuration
DefaultConfig = [{port, 12345}, {name, "whisper"}].
[Port, Name] = config:fetch_with_default_config([port, name], Config, DefaultConfig).
The project is still growing quite a bit and is fully tested up to this point. Please feel free to add to the library or do with what you please.
Awesome!
Comments
blog comments powered by Disqus