{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Configuration.Dotenv.Scheme.Types where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<*>), pure)
import Data.Functor ((<$>))
#endif
import Data.Maybe (isJust)
import Data.Map.Lazy (Map)
import Data.Yaml
import qualified Data.Map.Lazy as ML
import Data.Text (Text)
import qualified Data.Text as T
import Text.Read (readMaybe)
newtype EnvType = EnvType Text
deriving (Show, Eq, Ord)
instance FromJSON EnvType where
parseJSON (String value) = pure (EnvType value)
parseJSON anyOther = fail ("Not an object: " ++ show anyOther)
data Env =
Env
{ envName :: String
, envType :: EnvType
, required :: Bool
} deriving (Show, Eq, Ord)
instance FromJSON Env where
parseJSON (Object m) =
Env
<$> m .: "name"
<*> m .: "type"
<*> m .:? "required" .!= False
parseJSON x = fail ("Not an object: " ++ show x)
type ValidatorMap = Map Text (Text -> Bool)
defaultValidatorMap :: ValidatorMap
defaultValidatorMap =
let booleanValidator :: Text -> Bool
booleanValidator text = isJust (readMaybe (T.unpack text) :: Maybe Bool)
integerValidator :: Text -> Bool
integerValidator text = isJust (readMaybe (T.unpack text) :: Maybe Integer)
textValidator :: Text -> Bool
textValidator = const True
in ML.fromList
[ ("bool", booleanValidator)
, ("integer", integerValidator)
, ("text", textValidator)
]