Copyright | (c) 2018 Daniel YU |
---|---|
License | BSD3 |
Maintainer | Daniel YU <leptonyu@gmail.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Configuration Loader for Production in Haskell.
Synopsis
- defaultProperties :: IO Properties
- type ParseCommandLine = [String] -> IO [(String, Property)]
- defaultProperties' :: ParseCommandLine -> IO Properties
- defaultPropertiesWithFile :: FileName -> IO Properties
- empty :: Properties
- lookup :: FromProperties a => Text -> Properties -> Maybe a
- lookup' :: FromProperties a => Text -> Properties -> Return a
- toKeys :: Text -> [Key]
- data Property
- data Properties = Properties [Property] [HashMap Key Properties]
- type Key = Text
- class FromProperties a where
- data Return a
- insert :: [Key] -> Property -> Properties -> Properties
- makePropertiesFromEnvironment :: Properties -> IO Properties
- defaultParseCommandLine :: ParseCommandLine
- makePropertiesFromCommandLine :: ParseCommandLine -> Properties -> IO Properties
- makePropertiesFromJson :: Value -> Properties -> Properties
- makePropertiesFromYaml :: FilePath -> Properties -> IO Properties
- type FileName = Text
How to use this library
| This library default a standard configuration load process. It can load properties from CommandLine
, Environment
,
`JSON value` and Yaml
files. They all load to the same format Properties
. Earler property source has higher order
to load property. For example:
CommandLine: --package.a.enabled=true Environment: PACKAGE_A_ENABLED: false
lookup "package.a.enabled" properties => Just True
CommandLine
has higher order then Environment
, for the former load properties earler then later.
Usage:
data Config = Config { name :: Text , dir :: Maybe Text , ext :: Int } deriving (Eq, Show) instance FromJSON Config where parseJSON = withObject "Config" $ \v -> Config <$> v .: "name" <*> v .:? "dir" <*> (fromMaybe 1 <$> v .:? "ext")
main = do p <- defaultPropertiesWithFile "salak.yml" let Just config = lookup "salak.config" p :: Maybe Config print config
Properties Loader
defaultProperties :: IO Properties Source #
Initialize default properties from CommandLine
and Environment
.
CommandLine
use default parser.
type ParseCommandLine = [String] -> IO [(String, Property)] Source #
CommandLine parser. Parse command line into property key values.
defaultProperties' :: ParseCommandLine -> IO Properties Source #
Initialize default properties from CommandLine
and Environment
.
defaultPropertiesWithFile Source #
:: FileName | specify default config file name, can reset by config "salak.config.name" from |
-> IO Properties |
Initialize default properties from CommandLine
, Environment
and Yaml
files.
All these configuration sources has orders, from highest order to lowest order:
1. CommandLine 2. Environment 3. Specified Yaml file(file in "salak.config.dir") 4. Yaml file in current directory 5. Yaml file in home directory
empty :: Properties Source #
The empty Properties
Lookup Properties
lookup :: FromProperties a => Text -> Properties -> Maybe a Source #
Find Properties
by key and convert to specific Haskell value.
Return Nothing
means not found, and throw ErrorCall
means convert failed.
lookup' :: FromProperties a => Text -> Properties -> Return a Source #
Find Properties
by key and convert to specific Haskell value.
toKeys :: Text -> [Key] Source #
Split origin key by .
to sub keys:
"salak.config.name" -> ["salak","config","name"] "" -> [] "a..b" -> ["a","b"]
Types
A Property value represented as a Haskell value.
data Properties Source #
A Property Container to hold all properties
Instances
Eq Properties Source # | |
Defined in Data.Salak.Types (==) :: Properties -> Properties -> Bool # (/=) :: Properties -> Properties -> Bool # | |
Show Properties Source # | |
Defined in Data.Salak.Types showsPrec :: Int -> Properties -> ShowS # show :: Properties -> String # showList :: [Properties] -> ShowS # |
class FromProperties a where Source #
Convert Properties
to Haskell value.
fromProperties :: Properties -> Return a Source #
Instances
Return of FromProperties
Properties Loader Helper
insert :: [Key] -> Property -> Properties -> Properties Source #
Insert simple Property
into Properties
by Key
.
If the key already have values then the new property will discard.
makePropertiesFromEnvironment :: Properties -> IO Properties Source #
Load Properties
from Environment
defaultParseCommandLine :: ParseCommandLine Source #
Default command line parsers. Use format:
--KEY=VALUE
For example:
--salak.config.name=test.yml => ("salak.config.name", PStr "test.yml")
makePropertiesFromCommandLine :: ParseCommandLine -> Properties -> IO Properties Source #
Load Properties
from CommandLine
makePropertiesFromJson :: Value -> Properties -> Properties Source #
Load Properties
from JSON Value
makePropertiesFromYaml :: FilePath -> Properties -> IO Properties Source #
Load Properties
from Yaml
file.