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 :: MonadIO m => LoadSalakT m () -> RunSalakT m a -> m a
- runSalak :: MonadIO m => PropConfig -> RunSalakT m a -> m a
- runSalakWith :: (HasLoad file, MonadIO m) => String -> file -> RunSalakT m a -> m a
- data PropConfig = PropConfig {
- configName :: Maybe String
- configDirKey :: Text
- searchCurrent :: Bool
- searchHome :: Bool
- commandLine :: ParseCommandLine
- loadExt :: FilePath -> LoadSalakT IO ()
- data LoadSalakT m a
- loadSalak :: MonadIO m => PropConfig -> LoadSalakT m ()
- loadSalakFile :: (HasLoad file, MonadIO m) => String -> file -> LoadSalakT m ()
- loadCommandLine :: MonadIO m => ParseCommandLine -> LoadSalakT m ()
- type ParseCommandLine = [String] -> IO [(Text, Priority -> Value)]
- defaultParseCommandLine :: ParseCommandLine
- loadEnv :: MonadIO m => LoadSalakT m ()
- loadMock :: Monad m => [(Text, Text)] -> LoadSalakT m ()
- type ExtLoad = (String, FilePath -> LoadSalakT IO ())
- loadByExt :: (HasLoad a, MonadIO m) => a -> FilePath -> LoadSalakT m ()
- class HasLoad a where
- data a :|: b = a :|: b
- data RunSalakT m a
- liftNT :: Monad n => (forall x. m x -> n x) -> RunSalakT m a -> RunSalakT n a
- class Monad m => HasSourcePack m where
- askSourcePack :: m SourcePack
- logSP :: Text -> m ()
- readLogs :: m [Text]
- fetch :: (HasSourcePack m, FromProp a) => Text -> m (Either String a)
- require :: (HasSourcePack m, FromProp a) => Text -> m a
- data ReloadResult = ReloadResult {}
- exec :: MonadIO m => (IO ReloadResult -> IO a) -> RunSalakT m a
- requireD :: (MonadIO m, FromProp a) => Text -> RunSalakT m (IO a)
- data SourcePack
- type Priority = Int
- data Value
- type Prop = PropT PResult
- class FromProp a where
- class FromEnumProp a where
- fromEnumProp :: Text -> Either String a
- readPrimitive :: ([Selector] -> Value -> PResult a) -> Prop a
- data PResult a
- (.?=) :: Alternative f => f a -> a -> f a
- (.?:) :: (Alternative f, Default b) => f a -> (b -> a) -> f 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 = runSalakWith "salak" (YAML :|: TOML) $ do c :: Config <- require "test.config" lift $ print c
GHCi play
λ> import Salak λ> import Salak.YAML λ> import Salak.TOML λ> :set -XTypeApplications λ> instance FromProp Config where fromProp = Config <$> "user" <*> "dir" <*> "ext" .?= 1 λ> f = runSalakWith "salak" (YAML :|: TOML) λ> f (require "") >>= print @Config Config {name = "daniel", dir = Just "ls", ext = 2}
Salak Main Functions
:: MonadIO m | |
=> LoadSalakT m () | Load properties monad. |
-> RunSalakT m a | Fetch properties monad. |
-> m a |
Load and run salak SourcePack
and fetch properties.
runSalak :: MonadIO m => PropConfig -> RunSalakT m a -> m a Source #
Default run 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.
runSalakWith :: (HasLoad file, MonadIO m) => String -> file -> RunSalakT m a -> m a Source #
Simplified run salak, should specified code name and file format.
data PropConfig Source #
Prop load configuration
PropConfig | |
|
Instances
Default PropConfig Source # | |
Defined in Salak def :: PropConfig # |
Load Salak
data LoadSalakT m a Source #
Load Salak Monad Transfer
Instances
loadSalak :: MonadIO m => PropConfig -> LoadSalakT m () Source #
Default run 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.
loadSalakFile :: (HasLoad file, MonadIO m) => String -> file -> LoadSalakT m () Source #
Default run 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.
loadCommandLine :: MonadIO m => ParseCommandLine -> LoadSalakT m () Source #
Load command line arguments into SourcePack
type ParseCommandLine = [String] -> IO [(Text, Priority -> Value)] Source #
Convert arguments to properties
defaultParseCommandLine :: ParseCommandLine Source #
Default way to parse command line arguments
loadEnv :: MonadIO m => LoadSalakT m () Source #
Load environment variables into SourcePack
loadMock :: Monad m => [(Text, Text)] -> LoadSalakT m () Source #
Put key value pairs into SourcePack
Load Extensions
Run Salak
RunSalak Monad Transfer
Instances
MonadTrans RunSalakT Source # | |
Defined in Salak.Load.Dynamic | |
Monad m => Monad (RunSalakT m) Source # | |
Functor m => Functor (RunSalakT m) Source # | |
Monad m => Applicative (RunSalakT m) Source # | |
Defined in Salak.Load.Dynamic | |
MonadIO m => MonadIO (RunSalakT m) Source # | |
Defined in Salak.Load.Dynamic | |
MonadUnliftIO m => MonadUnliftIO (RunSalakT m) Source # | |
Defined in Salak.Load.Dynamic | |
MonadIO m => HasSourcePack (RunSalakT m) Source # | |
Get Static Properties
class Monad m => HasSourcePack m where Source #
Monad that can fetch properties.
Instances
Monad m => HasSourcePack (LoadSalakT m) Source # | |
Defined in Salak askSourcePack :: LoadSalakT m SourcePack Source # logSP :: Text -> LoadSalakT m () Source # readLogs :: LoadSalakT m [Text] Source # | |
MonadIO m => HasSourcePack (RunSalakT m) Source # | |
:: (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 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 # |
exec :: MonadIO m => (IO ReloadResult -> IO a) -> RunSalakT m a Source #
Run action in RunSalakT
, IO
ReloadResult
is reloadable action.
Fetch dynamic properties from SourcePack
, or throw fail
Parse properties
data SourcePack Source #
Source package, used to store all properties.
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 # |
VStr !Priority !Text | |
VRef !Priority ![RefText] | |
VNum !Priority !Scientific | |
VBool !Priority !Bool | |
VZTime !Priority !TimeZone !LocalTime | |
VLTime !Priority !LocalTime | |
VDay !Priority !Day | |
VHour !Priority !TimeOfDay |
class FromProp a where Source #
Nothing
Instances
class FromEnumProp a where Source #
Instances
Monad PResult Source # | |
Functor PResult Source # | |
Applicative PResult Source # | |
Alternative PResult Source # | |
HasValid Prop Source # | |
MonadReader SourcePack Prop Source # | |
Defined in Salak.Prop ask :: Prop SourcePack # local :: (SourcePack -> SourcePack) -> Prop a -> Prop a # reader :: (SourcePack -> a) -> Prop a # | |
Eq a => Eq (PResult a) Source # | |
Show a => Show (PResult a) Source # | |
FromProp a => IsString (Prop a) Source # | |
Defined in Salak.Prop fromString :: String -> Prop a # |
(.?=) :: 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.