fiction

Package Version Hex Docs

A library to help combine app configuration from multiple sources into a single record. This core is pure gleam, though most useful providers will have target-specific code.

Heavily inspired by Figment

gleam add fiction@1
import gleam/dynamic/decode
import fiction

type Config {
  Config(
    port: Int,
    host: String
  )
}

// The decoder can provide defaults if you want
fn config_decoder() {
  use port <- decode.optional_field("port", 5432, decode.int)
  use host <- decode.field("host", decode.string)

  decode.success(Config(port:, host:))
}

pub fn main() -> Nil {
  let assert Ok(config) =
    fiction.new()
    |> fiction.join(get_config_from_file)
    |> fiction.merge(get_config_from_env)
    |> fiction.extract(with: config_decoder())
}

// providers are just plain functions that return a Result(Dict(String, Value), String)
fn get_config_from_file() {
  // this can do basically anything
}

fn get_config_from_env() {
  // try a different source
}

Further documentation can be found at https://hexdocs.pm/fiction.

Development

gleam test  # Run the tests
Search Document