module Cfg (
getConfigRaw,
getConfig,
) where
import Data.Text (Text)
import Data.Tree (Tree (..))
import Cfg.Source (RootConfig(..), FetchSource, NestedConfig)
import Cfg.Parser (RootParser(..), ConfigParseError, NestedParser, ValueParser)
import Cfg.Deriving (ConfigValue(..), ConfigRoot(..))
import GHC.Generics
import Data.ByteString (ByteString)
import Cfg.Deriving.SubConfig (SubConfig(..))
import Cfg.Deriving.LabelModifier (ToUpper)
import Cfg.Deriving.ConfigRoot (ConfigRootOpts(..))
import Cfg.Deriving.SubConfig (SubConfigOpts(..))
getConfigRaw ::
Monad m =>
Tree Text ->
(Tree Text -> m (Tree Text)) ->
(Tree Text -> Either e a) ->
m (Either e a)
getConfigRaw :: forall (m :: * -> *) e a.
Monad m =>
Tree Text
-> (Tree Text -> m (Tree Text))
-> (Tree Text -> Either e a)
-> m (Either e a)
getConfigRaw Tree Text
keyTree Tree Text -> m (Tree Text)
source Tree Text -> Either e a
parser = Tree Text -> Either e a
parser (Tree Text -> Either e a) -> m (Tree Text) -> m (Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Tree Text -> m (Tree Text)
source Tree Text
keyTree
getConfig :: forall a m . (Monad m, RootConfig a, RootParser a) => FetchSource m -> m (Either ConfigParseError a)
getConfig :: forall a (m :: * -> *).
(Monad m, RootConfig a, RootParser a) =>
FetchSource m -> m (Either ConfigParseError a)
getConfig FetchSource m
fetch = forall a. RootParser a => Tree Text -> Either ConfigParseError a
parseRootConfig @a (Tree Text -> Either ConfigParseError a)
-> m (Tree Text) -> m (Either ConfigParseError a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FetchSource m
fetch (forall a. RootConfig a => Tree Text
toRootConfig @a)
data Environment = Development | Production
deriving stock ((forall x. Environment -> Rep Environment x)
-> (forall x. Rep Environment x -> Environment)
-> Generic Environment
forall x. Rep Environment x -> Environment
forall x. Environment -> Rep Environment x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Environment -> Rep Environment x
from :: forall x. Environment -> Rep Environment x
$cto :: forall x. Rep Environment x -> Environment
to :: forall x. Rep Environment x -> Environment
Generic, Int -> Environment -> ShowS
[Environment] -> ShowS
Environment -> String
(Int -> Environment -> ShowS)
-> (Environment -> String)
-> ([Environment] -> ShowS)
-> Show Environment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Environment -> ShowS
showsPrec :: Int -> Environment -> ShowS
$cshow :: Environment -> String
show :: Environment -> String
$cshowList :: [Environment] -> ShowS
showList :: [Environment] -> ShowS
Show)
deriving ([Tree Text]
[Tree Text] -> NestedConfig Environment
forall a. [Tree Text] -> NestedConfig a
$ctoNestedConfig :: [Tree Text]
toNestedConfig :: [Tree Text]
NestedConfig) via ConfigValue Environment
deriving (Parser Environment
Parser Environment -> ValueParser Environment
forall a. Parser a -> ValueParser a
$cparser :: Parser Environment
parser :: Parser Environment
ValueParser) via ConfigValue Environment
deriving Tree Text -> Either ConfigParseError Environment
(Tree Text -> Either ConfigParseError Environment)
-> NestedParser Environment
forall a.
(Tree Text -> Either ConfigParseError a) -> NestedParser a
$cparseNestedConfig :: Tree Text -> Either ConfigParseError Environment
parseNestedConfig :: Tree Text -> Either ConfigParseError Environment
NestedParser
data WarpConfig = WarpConfig
{ WarpConfig -> Int
warpConfigPort :: Int
, WarpConfig -> Int
warpConfigTimeout :: Int
, WarpConfig -> Bool
warpConfigHTTP2Enabled :: Bool
, WarpConfig -> ByteString
warpConfigServerName :: ByteString
}
deriving ((forall x. WarpConfig -> Rep WarpConfig x)
-> (forall x. Rep WarpConfig x -> WarpConfig) -> Generic WarpConfig
forall x. Rep WarpConfig x -> WarpConfig
forall x. WarpConfig -> Rep WarpConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. WarpConfig -> Rep WarpConfig x
from :: forall x. WarpConfig -> Rep WarpConfig x
$cto :: forall x. Rep WarpConfig x -> WarpConfig
to :: forall x. Rep WarpConfig x -> WarpConfig
Generic, Int -> WarpConfig -> ShowS
[WarpConfig] -> ShowS
WarpConfig -> String
(Int -> WarpConfig -> ShowS)
-> (WarpConfig -> String)
-> ([WarpConfig] -> ShowS)
-> Show WarpConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WarpConfig -> ShowS
showsPrec :: Int -> WarpConfig -> ShowS
$cshow :: WarpConfig -> String
show :: WarpConfig -> String
$cshowList :: [WarpConfig] -> ShowS
showList :: [WarpConfig] -> ShowS
Show)
deriving ([Tree Text]
[Tree Text] -> NestedConfig WarpConfig
forall a. [Tree Text] -> NestedConfig a
$ctoNestedConfig :: [Tree Text]
toNestedConfig :: [Tree Text]
NestedConfig) via (SubConfig WarpConfig)
deriving (Tree Text -> Either ConfigParseError WarpConfig
(Tree Text -> Either ConfigParseError WarpConfig)
-> NestedParser WarpConfig
forall a.
(Tree Text -> Either ConfigParseError a) -> NestedParser a
$cparseNestedConfig :: Tree Text -> Either ConfigParseError WarpConfig
parseNestedConfig :: Tree Text -> Either ConfigParseError WarpConfig
NestedParser) via (SubConfig WarpConfig)
data RedisConfig = RedisConfig
{ RedisConfig -> Text
redisConfigHost :: Text
, RedisConfig -> Int
redisConfigPort :: Int
, RedisConfig -> Maybe ByteString
redisConfigConnectAuth :: Maybe ByteString
}
deriving ((forall x. RedisConfig -> Rep RedisConfig x)
-> (forall x. Rep RedisConfig x -> RedisConfig)
-> Generic RedisConfig
forall x. Rep RedisConfig x -> RedisConfig
forall x. RedisConfig -> Rep RedisConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. RedisConfig -> Rep RedisConfig x
from :: forall x. RedisConfig -> Rep RedisConfig x
$cto :: forall x. Rep RedisConfig x -> RedisConfig
to :: forall x. Rep RedisConfig x -> RedisConfig
Generic, Int -> RedisConfig -> ShowS
[RedisConfig] -> ShowS
RedisConfig -> String
(Int -> RedisConfig -> ShowS)
-> (RedisConfig -> String)
-> ([RedisConfig] -> ShowS)
-> Show RedisConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RedisConfig -> ShowS
showsPrec :: Int -> RedisConfig -> ShowS
$cshow :: RedisConfig -> String
show :: RedisConfig -> String
$cshowList :: [RedisConfig] -> ShowS
showList :: [RedisConfig] -> ShowS
Show)
deriving ([Tree Text]
[Tree Text] -> NestedConfig RedisConfig
forall a. [Tree Text] -> NestedConfig a
$ctoNestedConfig :: [Tree Text]
toNestedConfig :: [Tree Text]
NestedConfig) via (SubConfig RedisConfig)
deriving (Tree Text -> Either ConfigParseError RedisConfig
(Tree Text -> Either ConfigParseError RedisConfig)
-> NestedParser RedisConfig
forall a.
(Tree Text -> Either ConfigParseError a) -> NestedParser a
$cparseNestedConfig :: Tree Text -> Either ConfigParseError RedisConfig
parseNestedConfig :: Tree Text -> Either ConfigParseError RedisConfig
NestedParser) via (SubConfig RedisConfig)
data AppConfig = AppConfig
{ AppConfig -> WarpConfig
appConfigWarpSettings :: WarpConfig
, AppConfig -> RedisConfig
appConfigRedisSettings :: RedisConfig
, AppConfig -> Environment
appConfigEnvironment :: Environment
}
deriving stock ((forall x. AppConfig -> Rep AppConfig x)
-> (forall x. Rep AppConfig x -> AppConfig) -> Generic AppConfig
forall x. Rep AppConfig x -> AppConfig
forall x. AppConfig -> Rep AppConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AppConfig -> Rep AppConfig x
from :: forall x. AppConfig -> Rep AppConfig x
$cto :: forall x. Rep AppConfig x -> AppConfig
to :: forall x. Rep AppConfig x -> AppConfig
Generic, Int -> AppConfig -> ShowS
[AppConfig] -> ShowS
AppConfig -> String
(Int -> AppConfig -> ShowS)
-> (AppConfig -> String)
-> ([AppConfig] -> ShowS)
-> Show AppConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AppConfig -> ShowS
showsPrec :: Int -> AppConfig -> ShowS
$cshow :: AppConfig -> String
show :: AppConfig -> String
$cshowList :: [AppConfig] -> ShowS
showList :: [AppConfig] -> ShowS
Show)
deriving (Tree Text
Tree Text -> RootConfig AppConfig
forall a. Tree Text -> RootConfig a
$ctoRootConfig :: Tree Text
toRootConfig :: Tree Text
RootConfig) via (ConfigRoot AppConfig)
deriving (Tree Text -> Either ConfigParseError AppConfig
(Tree Text -> Either ConfigParseError AppConfig)
-> RootParser AppConfig
forall a. (Tree Text -> Either ConfigParseError a) -> RootParser a
$cparseRootConfig :: Tree Text -> Either ConfigParseError AppConfig
parseRootConfig :: Tree Text -> Either ConfigParseError AppConfig
RootParser) via (ConfigRoot AppConfig)
sample :: Tree Text
sample :: Tree Text
sample = Node
{ rootLabel :: Text
rootLabel = Text
"AppConfig"
, subForest :: [Tree Text]
subForest =
[ Node
{ rootLabel :: Text
rootLabel = Text
"appConfigWarpSettings"
, subForest :: [Tree Text]
subForest =
[ Node
{ rootLabel :: Text
rootLabel = Text
"warpConfigPort"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"8080" [] ]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"warpConfigTimeout"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"30" [] ]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"warpConfigHTTP2Enabled"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"True" [] ]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"warpConfigServerName"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"MyServer" [] ]
}
]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"appConfigRedisSettings"
, subForest :: [Tree Text]
subForest =
[ Node
{ rootLabel :: Text
rootLabel = Text
"redisConfigHost"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"https://localhost" [] ]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"redisConfigPort"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"6379" [] ]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"redisConfigConnectAuth"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"Just password" [] ]
}
]
}
, Node
{ rootLabel :: Text
rootLabel = Text
"appConfigEnvironment"
, subForest :: [Tree Text]
subForest = [ Text -> [Tree Text] -> Tree Text
forall a. a -> [Tree a] -> Tree a
Node Text
"Development" [] ]
}
]
}
data EnvWarpConfig = EnvWarpConfig
{ EnvWarpConfig -> Int
envWarpConfigPort :: Int
, EnvWarpConfig -> Int
envWarpConfigTimeout :: Int
, EnvWarpConfig -> Bool
envWarpConfigHTTP2Enabled :: Bool
, EnvWarpConfig -> ByteString
envWarpConfigServerName :: ByteString
}
deriving ((forall x. EnvWarpConfig -> Rep EnvWarpConfig x)
-> (forall x. Rep EnvWarpConfig x -> EnvWarpConfig)
-> Generic EnvWarpConfig
forall x. Rep EnvWarpConfig x -> EnvWarpConfig
forall x. EnvWarpConfig -> Rep EnvWarpConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EnvWarpConfig -> Rep EnvWarpConfig x
from :: forall x. EnvWarpConfig -> Rep EnvWarpConfig x
$cto :: forall x. Rep EnvWarpConfig x -> EnvWarpConfig
to :: forall x. Rep EnvWarpConfig x -> EnvWarpConfig
Generic, Int -> EnvWarpConfig -> ShowS
[EnvWarpConfig] -> ShowS
EnvWarpConfig -> String
(Int -> EnvWarpConfig -> ShowS)
-> (EnvWarpConfig -> String)
-> ([EnvWarpConfig] -> ShowS)
-> Show EnvWarpConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EnvWarpConfig -> ShowS
showsPrec :: Int -> EnvWarpConfig -> ShowS
$cshow :: EnvWarpConfig -> String
show :: EnvWarpConfig -> String
$cshowList :: [EnvWarpConfig] -> ShowS
showList :: [EnvWarpConfig] -> ShowS
Show)
deriving ([Tree Text]
[Tree Text] -> NestedConfig EnvWarpConfig
forall a. [Tree Text] -> NestedConfig a
$ctoNestedConfig :: [Tree Text]
toNestedConfig :: [Tree Text]
NestedConfig) via (SubConfigOpts ToUpper EnvWarpConfig)
deriving (Tree Text -> Either ConfigParseError EnvWarpConfig
(Tree Text -> Either ConfigParseError EnvWarpConfig)
-> NestedParser EnvWarpConfig
forall a.
(Tree Text -> Either ConfigParseError a) -> NestedParser a
$cparseNestedConfig :: Tree Text -> Either ConfigParseError EnvWarpConfig
parseNestedConfig :: Tree Text -> Either ConfigParseError EnvWarpConfig
NestedParser) via (SubConfigOpts ToUpper EnvWarpConfig)
data EnvRedisConfig = EnvRedisConfig
{ EnvRedisConfig -> Text
envRedisConfigHost :: Text
, EnvRedisConfig -> Int
envRedisConfigPort :: Int
, EnvRedisConfig -> Maybe ByteString
envRedisConfigConnectAuth :: Maybe ByteString
}
deriving ((forall x. EnvRedisConfig -> Rep EnvRedisConfig x)
-> (forall x. Rep EnvRedisConfig x -> EnvRedisConfig)
-> Generic EnvRedisConfig
forall x. Rep EnvRedisConfig x -> EnvRedisConfig
forall x. EnvRedisConfig -> Rep EnvRedisConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EnvRedisConfig -> Rep EnvRedisConfig x
from :: forall x. EnvRedisConfig -> Rep EnvRedisConfig x
$cto :: forall x. Rep EnvRedisConfig x -> EnvRedisConfig
to :: forall x. Rep EnvRedisConfig x -> EnvRedisConfig
Generic, Int -> EnvRedisConfig -> ShowS
[EnvRedisConfig] -> ShowS
EnvRedisConfig -> String
(Int -> EnvRedisConfig -> ShowS)
-> (EnvRedisConfig -> String)
-> ([EnvRedisConfig] -> ShowS)
-> Show EnvRedisConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EnvRedisConfig -> ShowS
showsPrec :: Int -> EnvRedisConfig -> ShowS
$cshow :: EnvRedisConfig -> String
show :: EnvRedisConfig -> String
$cshowList :: [EnvRedisConfig] -> ShowS
showList :: [EnvRedisConfig] -> ShowS
Show)
deriving ([Tree Text]
[Tree Text] -> NestedConfig EnvRedisConfig
forall a. [Tree Text] -> NestedConfig a
$ctoNestedConfig :: [Tree Text]
toNestedConfig :: [Tree Text]
NestedConfig) via (SubConfigOpts ToUpper EnvRedisConfig)
deriving (Tree Text -> Either ConfigParseError EnvRedisConfig
(Tree Text -> Either ConfigParseError EnvRedisConfig)
-> NestedParser EnvRedisConfig
forall a.
(Tree Text -> Either ConfigParseError a) -> NestedParser a
$cparseNestedConfig :: Tree Text -> Either ConfigParseError EnvRedisConfig
parseNestedConfig :: Tree Text -> Either ConfigParseError EnvRedisConfig
NestedParser) via (SubConfigOpts ToUpper EnvRedisConfig)
data EnvAppConfig = EnvAppConfig
{ EnvAppConfig -> EnvWarpConfig
envAppConfigWarpSettings :: EnvWarpConfig
, EnvAppConfig -> EnvRedisConfig
envAppConfigRedisSettings :: EnvRedisConfig
, EnvAppConfig -> Environment
envAppConfigEnvironment :: Environment
}
deriving stock ((forall x. EnvAppConfig -> Rep EnvAppConfig x)
-> (forall x. Rep EnvAppConfig x -> EnvAppConfig)
-> Generic EnvAppConfig
forall x. Rep EnvAppConfig x -> EnvAppConfig
forall x. EnvAppConfig -> Rep EnvAppConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EnvAppConfig -> Rep EnvAppConfig x
from :: forall x. EnvAppConfig -> Rep EnvAppConfig x
$cto :: forall x. Rep EnvAppConfig x -> EnvAppConfig
to :: forall x. Rep EnvAppConfig x -> EnvAppConfig
Generic, Int -> EnvAppConfig -> ShowS
[EnvAppConfig] -> ShowS
EnvAppConfig -> String
(Int -> EnvAppConfig -> ShowS)
-> (EnvAppConfig -> String)
-> ([EnvAppConfig] -> ShowS)
-> Show EnvAppConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EnvAppConfig -> ShowS
showsPrec :: Int -> EnvAppConfig -> ShowS
$cshow :: EnvAppConfig -> String
show :: EnvAppConfig -> String
$cshowList :: [EnvAppConfig] -> ShowS
showList :: [EnvAppConfig] -> ShowS
Show)
deriving (Tree Text
Tree Text -> RootConfig EnvAppConfig
forall a. Tree Text -> RootConfig a
$ctoRootConfig :: Tree Text
toRootConfig :: Tree Text
RootConfig) via (ConfigRootOpts ToUpper ToUpper EnvAppConfig)
deriving (Tree Text -> Either ConfigParseError EnvAppConfig
(Tree Text -> Either ConfigParseError EnvAppConfig)
-> RootParser EnvAppConfig
forall a. (Tree Text -> Either ConfigParseError a) -> RootParser a
$cparseRootConfig :: Tree Text -> Either ConfigParseError EnvAppConfig
parseRootConfig :: Tree Text -> Either ConfigParseError EnvAppConfig
RootParser) via (ConfigRootOpts ToUpper ToUpper EnvAppConfig)