Copyright | © Jonathan Lorimer 2023 |
---|---|
License | MIT |
Maintainer | jonathanlorimer@pm.me |
Stability | stable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides helper functions for manipulating the keys on our
internal tree representation of a configuration; KeyTree
.
These helper functions are generally oriented towards using environment variables as your configuration source.
Since: 0.0.2.0
Documentation
This function takes a separator and a list of keys and joins them from the end of the list to the beginning, interspersed with the provided separator.
>>>
getEnvKey "_" ["A", "B", "C"]
"A_B_C"
Since: 0.0.1.0
getKeys :: KeyTree Text Text -> [[Text]] Source #
Folds a KeyTree
from leaf to root, into distinct key paths.
This is necessary for the way that hierarchical structures are represented
in environment variables (i.e. "KEYA_SUBKEYA", "KEYA_SUBKEYB").
Here is a visual representation of how the keys would get folded
A / \ B C [ [ A, B ] , [ A, C ] ]
>>>
import KeyTree
>>>
import Data.Map qualified as M
>>>
getKeys $ Free $ M.singleton "A" $ Free (M.fromList [("B", Free M.empty), ("C", Free M.empty)])
[["A","B"],["A","C"]]
Since: 0.0.1.0
Gets all the keys from a configuration tree, and flattens the hierarchy so that each key is prefixed with its path through the tree.
Accepts separator to individuate the key prefixes.
>>>
import KeyTree
>>>
import Data.Map qualified as M
>>>
showEnvKeys' "-" $ Free $ M.singleton "A" $ Free (M.fromList [("B", Free M.empty), ("C", Free M.empty)])
["A-B","A-C"]
Since: 0.0.1.0
showEnvKeys :: forall {k} (a :: k). ConfigSource a => Text -> [Text] Source #
Same as showEnvKeys'
but the KeyTree
is generated via a configSource
>>>
import GHC.Generics (Generic (..))
>>>
import Cfg.Source (ConfigSource(..))
>>>
import Cfg.Parser (ConfigParser(..))
>>>
import Cfg.Deriving.Config (Config(..))
>>>
import Cfg.Source.Default (DefaultSource(..))
>>>
:{
data Sub = Sub { c :: Int, d :: Bool } deriving (Generic, Show, DefaultSource) deriving (ConfigSource, ConfigParser) via Config Sub data TypeCon = DataCon { a :: Sub } deriving (Generic, Show, DefaultSource) deriving (ConfigSource, ConfigParser) via Config TypeCon :}
>>>
showEnvKeys @TypeCon "_"
["a_c","a_d"]
Since: 0.0.1.0