Giter Club home page Giter Club logo

ecc's Introduction

ecc

An OTP application for compiling configuration values into well-structured and validated namespaces.

Configuration values can be loaded via an implementation of the ecc_gen_driver behavior module. By default, a driver is supplied for loading configuration values via environment variables and an optional dotenv file path. This driver can be found in the ecc_env_driver module.

Validation and namespacing of configuration values occurs through modules that implement the ecc_gen_config behavior module. Each configuration module exports a set of functions that:

  • declare the namespace its configuration will be stored under (this must be unique), via namespace/0
  • transform the configuration set (loaded by a ecc_gen_driver module) into a validated structure, via load/1
  • provide a means to access attributes of this validated structure, via get/2

An example implementation of ecc_gen_config can be found in the test suite, under ecc_test_config.erl.

Build

$ rebar3 compile

Test

$ rebar3 eunit

Documentation

$ rebar3 edoc

Formatting

$ rebar3 fmt

Usage

Example configuration module implementation:

-module(example_config_module).

-behavior(ecc_gen_config).

-export([namespace/0, load/1, get/2]).

-record(example_config_rec, {
    foo :: string(),
    bar :: integer(),
    baz :: boolean()
}).

%%%===================================================================
%%% ecc_gen_config callbacks
%%%===================================================================

namespace() ->
    ?MODULE.

load(Maps = #{"FOO" := Foo, "BAR" := Bar}) ->
    % or handle this as a result, for simplicity of this example we won't provide a helpful error message
    BarInt = parse_int(Bar),
    % or handle this as a result, for simplicity of this example we won't provide a helpful error message
    BazBool = parse_bool(maps:get("BAZ", Maps, "false")),
    {ok, #example_config_rec{foo = Foo, bar = BarInt, baz = BazBool}};
load(#{"FOO" := _Foo}) ->
    {error, "missing required FOO value"};
load(#{"BAR" := _Bar}) ->
    {error, "missing required BAR value"};
load(_) ->
    {error, "missing required FOO and BAR values"}.

get(foo, #example_config_rec{foo = Foo}) ->
    Foo;
get(bar, #example_config_rec{bar = Bar}) ->
    Bar;
get(baz, #example_config_rec{baz = Baz}) ->
    Baz;
get(_, _) ->
    undefined.

%%%===================================================================
%%% Internal functions
%%%===================================================================

parse_int(Val) when is_list(Val) ->
    erlang:list_to_integer(Val).

parse_bool("true") ->
    true;
parse_bool("false") ->
    false.

Then to register this module with the ecc application:

ok = ecc:register(example_config_module)

Subsequent usage, once the configuration module has been registered:

#example_config_rec{foo = Foo} = ecc:namespace(example_config_module),
Foo = ecc:key(example_config_module, foo)

ecc's People

Contributors

kevinwilson541 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.