Dynamic Configuration Database in Erlang

zxq9 zxq9@REDACTED
Wed Dec 4 16:19:05 CET 2019


On 2019/12/05 0:08, zxq9 wrote:
> On 2019/12/04 22:03, Stefano Bertuola wrote:
>> I am looking for understanding how to implement a 
>> configuration database in Erlang (which allows dynamic configuration).
>>
>> Does anyone have more details about this or any other implementation?

A quick note on this bit here:

> You can implement a relatively naked "global dictionary" if you haven't 
> figured out what sort of configuration data your program needs. This 
> would be a simple process that exposes two functions:
> 
> -spec config(Key :: atom()) -> Value :: term().
> -spec config(Key :: atom(), Value :: term()) -> ok.

Really the spec should be:

   -spec config(Key :: atom()) -> {ok, Value :: term()} | undefined.
   -spec config(Key :: atom(), Value :: term()) -> ok.

The alternative is to crash the system if someone requests an undefined 
value (or to return the atom 'undefined' and messily match on it all the 
time -- which is workable until the setting value you actually *intend* 
happens to itself be 'undefined'!). You could also use 'false' in place 
of 'undefined' if you happen to need some listy abstractions that 
operate over config data, of course, but anyway, I think you get the idea.

I'm a big fan of {ok, Value} | {error, Reason} type return values 
because they provide more options for the author of the calling code, to 
include direct assertion at the place they call it:

   % Only crash the calling process if Key doesn't exist
   {ok, Setting} = conf_man:config(Key),
   % ...

etc.

I just noticed this in retrospect. Small detail, but can have enough of 
an impact on calling code that it is worth mentioning.

-Craig



More information about the erlang-questions mailing list