Copyright | (c) 2019 Daniel YU |
---|---|
License | BSD3 |
Maintainer | leptonyu@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Configuration Loader for Production in Haskell.
Synopsis
- loadAndRunSalak :: Monad m => SourcePackT m () -> ReaderT SourcePack m a -> m a
- runSalak :: MonadIO m => PropConfig -> ReaderT SourcePack m a -> m a
- data PropConfig = PropConfig {
- configName :: Maybe String
- configDirKey :: Text
- searchCurrent :: Bool
- searchHome :: Bool
- commandLine :: ParseCommandLine
- loadExt :: FilePath -> SourcePackT IO ()
- class Monad m => HasSourcePack m where
- askSourcePack :: m SourcePack
- fetch :: (HasSourcePack m, FromProp a) => Text -> m (Either String a)
- require :: (HasSourcePack m, FromProp a) => Text -> m a
- data ReloadableSourcePack
- type ReloadableSourcePackT = StateT ReloadableSourcePack
- data ReloadResult = ReloadResult {}
- reloadable :: (MonadIO m, HasSourcePack m) => ReloadableSourcePackT m a -> m a
- reloadAction :: Monad m => ReloadableSourcePackT m (IO ReloadResult)
- fetchD :: (MonadIO m, FromProp a) => Text -> ReloadableSourcePackT m (Either String (IO a))
- requireD :: (MonadIO m, FromProp a) => Text -> ReloadableSourcePackT m (IO a)
- type Prop = PropT PResult
- class FromProp a where
- class FromEnumProp a where
- fromEnumProp :: Text -> Either String a
- (.?=) :: Alternative f => f a -> a -> f a
- (.?:) :: (Alternative f, Default b) => f a -> (b -> a) -> f a
- data SourcePack
- type SourcePackT = StateT SourcePack
- loadCommandLine :: MonadIO m => ParseCommandLine -> SourcePackT m ()
- loadEnv :: MonadIO m => SourcePackT m ()
- loadMock :: Monad m => [(Text, Text)] -> SourcePackT m ()
- type ExtLoad = (String, FilePath -> SourcePackT IO ())
- loadByExt :: (HasLoad a, MonadIO m) => a -> FilePath -> SourcePackT m ()
- class HasLoad a where
- data a :|: b = a :|: b
- type ParseCommandLine = [String] -> IO [(Text, Priority -> Value)]
- defaultParseCommandLine :: ParseCommandLine
- type Priority = Int
- data Value
- defaultLoadSalak :: MonadIO m => PropConfig -> ReaderT SourcePack m a -> m a
How to use this library
| This library define a universal procedure to load configurations and parse properties, also supports reload configuration files.
We can load configurations from command line, environment, configuration files such as yaml or toml etc, and we may want to have our own strategies to load configurations from multi 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 priority, priorities cannot be changed.
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 FromProp Config where fromProp = Config <$> "user" ? pattern "[a-z]{5,16}" <*> "pwd" <*> "ext" .?= 1 main = runSalak def { configName = Just "salak", loadExt = loadByExt $ YAML :|: TOML } $ do c :: Config <- require "test.config" lift $ print c
GHCi play
λ> import Salak λ> import Salak.Load.YAML λ> import Salak.Load.TOML λ> :set -XTypeApplications λ> instance FromProp Config where fromProp = Config <$> "user" <*> "dir" <*> "ext" .?= 1 λ> f = runSalak def { configName = Just "salak", loadExt = loadByExt $ YAML :|: TOML } λ> f (require "") >>= print @Config Config {name = "daniel", dir = Just "ls", ext = 2}
Salak
:: Monad m | |
=> SourcePackT m () | Load properties monad. |
-> ReaderT SourcePack m a | Fetch properties monad. |
-> m a |
Load and run salak SourcePack
and fetch properties.
runSalak :: MonadIO m => PropConfig -> ReaderT SourcePack m a -> m a Source #
Default load salak. All these configuration sources has orders, from highest priority to lowest priority:
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.
data PropConfig Source #
Prop load configuration
PropConfig | |
|
Instances
Default PropConfig Source # | |
Defined in Salak def :: PropConfig # |
Static Get Properties
class Monad m => HasSourcePack m where Source #
askSourcePack :: m SourcePack Source #
Instances
Monad m => HasSourcePack (ReaderT SourcePack m) Source # | |
Defined in Salak | |
Monad m => HasSourcePack (StateT SourcePack m) Source # | |
Defined in Salak |
:: (HasSourcePack m, FromProp a) | |
=> Text | Properties key |
-> m (Either String a) |
Try fetch properties from SourcePack
:: (HasSourcePack m, FromProp a) | |
=> Text | Properties key |
-> m a |
Fetch properties from SourcePack
, or throw fail
Dynamic Get Properties
data ReloadableSourcePack Source #
Reloadable SourcePack
data ReloadResult Source #
Instances
Eq ReloadResult Source # | |
Defined in Salak.Load.Dynamic (==) :: ReloadResult -> ReloadResult -> Bool # (/=) :: ReloadResult -> ReloadResult -> Bool # | |
Show ReloadResult Source # | |
Defined in Salak.Load.Dynamic showsPrec :: Int -> ReloadResult -> ShowS # show :: ReloadResult -> String # showList :: [ReloadResult] -> ShowS # |
reloadable :: (MonadIO m, HasSourcePack m) => ReloadableSourcePackT m a -> m a Source #
Lift to reloadable environment for dynamic properties.
reloadAction :: Monad m => ReloadableSourcePackT m (IO ReloadResult) Source #
Try fetch dynamic properties from SourcePack
:: (MonadIO m, FromProp a) | |
=> Text | Properties key |
-> ReloadableSourcePackT m (IO a) |
Fetch dynamic properties from SourcePack
, or throw fail
Prop Parser
class FromProp a where Source #
Nothing
Instances
FromProp Bool Source # | |
FromProp Double Source # | |
FromProp Float Source # | |
FromProp Int Source # | |
FromProp Int8 Source # | |
FromProp Int16 Source # | |
FromProp Int32 Source # | |
FromProp Int64 Source # | |
FromProp Word Source # | |
FromProp Word8 Source # | |
FromProp Word16 Source # | |
FromProp Word32 Source # | |
FromProp Word64 Source # | |
FromEnumProp a => FromProp a Source # | |
Defined in Salak.Prop | |
FromProp Text Source # | |
FromProp Text Source # | |
FromProp String Source # | |
FromProp Scientific Source # | |
Defined in Salak.Prop | |
FromProp a => FromProp [a] Source # | |
Defined in Salak.Prop | |
FromProp a => FromProp (Maybe a) Source # | |
class FromEnumProp a where Source #
(.?=) :: Alternative f => f a -> a -> f a infixl 5 Source #
Optional value.
(.?:) :: (Alternative f, Default b) => f a -> (b -> a) -> f a infixl 5 Source #
Default value.
SourcePack
data SourcePack Source #
Instances
Show SourcePack Source # | |
Defined in Salak.Types showsPrec :: Int -> SourcePack -> ShowS # show :: SourcePack -> String # showList :: [SourcePack] -> ShowS # | |
MonadReader SourcePack Prop Source # | |
Defined in Salak.Prop ask :: Prop SourcePack # local :: (SourcePack -> SourcePack) -> Prop a -> Prop a # reader :: (SourcePack -> a) -> Prop a # | |
Monad m => HasSourcePack (ReaderT SourcePack m) Source # | |
Defined in Salak | |
Monad m => HasSourcePack (StateT SourcePack m) Source # | |
Defined in Salak |
type SourcePackT = StateT SourcePack Source #
Load configurations
loadCommandLine :: MonadIO m => ParseCommandLine -> SourcePackT m () Source #
loadEnv :: MonadIO m => SourcePackT m () Source #
Load By Extension
Other
VStr !Priority !Text | |
VNum !Priority !Scientific | |
VBool !Priority !Bool | |
VZTime !Priority !TimeZone !LocalTime | |
VLTime !Priority !LocalTime | |
VDay !Priority !Day | |
VHour !Priority !TimeOfDay |
defaultLoadSalak :: MonadIO m => PropConfig -> ReaderT SourcePack m a -> m a Source #
Deprecated: use runSalak instead