module Snap.Utilities.Configuration.Lookup (
cfgLookup,
cfgLookupWithDefault,
stringValue,
listValue
) where
import qualified Data.Configurator as C
import qualified Data.Configurator.Types as CT
import Data.List (groupBy, intercalate, find, sortBy)
import Data.HashMap.Strict (toList)
import Data.Text (Text, isPrefixOf, splitOn, pack, unpack)
import Snap.Utilities.Configuration.Types
cfgLookup :: Text -> (CT.Value -> a) -> [ConfigPair] -> Maybe a
cfgLookup key transformer = fmap (transformer . snd) . (found key)
cfgLookupWithDefault :: Text -> a -> (CT.Value -> a) -> [ConfigPair] -> a
cfgLookupWithDefault key def transformer = maybe def (transformer . snd) . (found key)
found :: Text -> [ConfigPair] -> Maybe ConfigPair
found key = find (\(k, _) -> k == key)
stringValue :: CT.Value -> String
stringValue (CT.String x) = unpack x
stringValue (CT.List x) = intercalate " " $ map show x
stringValue x = show x
listValue :: CT.Value -> [String]
listValue (CT.List x) = map stringValue x
listValue (CT.String x) = map unpack . splitOn "," $ x
listValue x = listValue . CT.String . pack . show $ x