salak: Configuration (re)Loader and Parser.

[ configuration, library, mit ] [ Propose Tags ]
This version is deprecated.

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10, 0.1.11, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.6, 0.2.7, 0.2.8, 0.2.9, 0.2.9.1, 0.2.9.2, 0.2.9.3, 0.2.10, 0.3, 0.3.1, 0.3.2, 0.3.3, 0.3.3.1, 0.3.3.2, 0.3.4, 0.3.4.1, 0.3.5, 0.3.5.1, 0.3.5.2, 0.3.5.3, 0.3.6 (info)
Dependencies attoparsec (>=0.13.2.2 && <0.14), base (>=4.10 && <5), bytestring (>=0.10.8.2 && <0.11), data-default (>=0.7.1.1 && <0.8), directory (>=1.3.3.0 && <1.4), exceptions (>=0.10.2 && <0.11), filepath (>=1.4.2.1 && <1.5), hashable (>=1.2.7.0 && <1.3), heaps (>=0.3.6.1 && <0.4), menshen (>=0.0.3 && <0.1), mtl (>=2.2.2 && <2.3), scientific (>=0.3.6.2 && <0.4), text (>=1.2.3.1 && <1.3), time (>=1.8.0.2 && <1.9), unliftio-core (>=0.1.2.0 && <0.2), unordered-containers (>=0.2.10.0 && <0.3) [details]
License BSD-3-Clause
Copyright (c) 2018 Daniel YU
Author Daniel YU
Maintainer Daniel YU <leptonyu@gmail.com>
Category Library, Configuration
Home page https://github.com/leptonyu/salak#readme
Uploaded by leptonyu at 2019-07-27T12:08:29Z
Distributions
Reverse Dependencies 10 direct, 0 indirect [details]
Downloads 14519 total (105 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for salak-0.3.1

[back to package description]

salak

Hackage stackage LTS package stackage Nightly package Build Status

Configuration (re)loader in Haskell.

salak-yaml

salak-yaml

salak-toml

salak-toml

Introduction

This library defines a universal procedure to load configurations and parse properties, also supports reload configuration files.

Parse Functions

HasSalak monad provide a unified function require to parse properties. Here are some examples.

a :: Bool              <- require "bool.key"
b :: Maybe Int         <- require "int.optional.key"
c :: Either String Int <- require "int.error.key"
d :: IO Int            <- require "int.reloadable.key" -- This property can be changed by reloading configurations.

Load Strategy

We can load configurations from command lines, environment, configuration files such as yaml or toml etc., and we may want to have our own strategies to load configurations from multiply sources and overwrite properties by orders of these sources.

PropConfig defines a common loading strategy:

  1. loadCommandLine
  2. loadEnvironment
  3. loadConfFiles
  4. load file from folder salak.conf.dir if defined
  5. load file from current folder if enabled
  6. load file from home folder if enabled
  7. file extension matching, support yaml or toml or any other loader.

Load earlier has higher priorities. Priorities cannot be changed.

For command lines and environment,

CommandLine:  --package.a.enabled=true
Environment: PACKAGE_A_ENABLED: false

Usage

Environment:

export TEST_CONFIG_NAME=daniel

Current Directory: salak.yaml

test.config:
  name: noop
  dir: ls

Current Directory: salak.toml

[test.config]
ext=2
data Config = Config
  { name :: Text
  , dir  :: Maybe Text
  , ext  :: Int
  } deriving (Eq, Show)

instance Monad m => FromProp m Config where
  fromProp = Config
    <$> "user" ? pattern "[a-z]{5,16}"
    <*> "pwd"
    <*> "ext" .?= 1

main = runSalakWith "salak" (YAML :|: TOML) $ do
  c :: Config <- require "test.config"
  lift $ print c

GHCi play

λ> :set -XFlexibleInstances -XMultiParamTypeClasses -XOverloadedStrings
λ> import Salak
λ> import Data.Default
λ> import Data.Text(Text)
λ> data Config = Config { name :: Text, dir  :: Maybe Text, ext  :: Int} deriving (Eq, Show)
λ> instance Monad m => FromProp m Config where fromProp = Config <$> "user" <*> "dir" <*> "ext" .?= 1
λ> runSalak def (require "") :: IO Config
Config {name = "daniel", dir = Nothing, ext = 1}

TODO:

  • Recover placeholder
  • Add git pull support.
  • Add automatic reloading.