Copyright | (c) David Johnson 2015 |
---|---|
Maintainer | djohnson.m@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
{-# LANGUAGE DeriveGeneric #-} module Main ( main ) where import System.Envy import GHC.Generics data PGConfig = PGConfig { pgHost :: String -- "PG_HOST" , pgPort :: Int -- "PG_PORT" } deriving (Generic, Show) -- Default instance used if environment variable doesn't exist instance DefConfig PGConfig where defConfig = PGConfig "localhost" 5432 instance FromEnv PGConfig -- Generically produces the following body (no implementation needed if using Generics): -- fromEnv = PGConfig <$> envMaybe "PG_HOST" .!= "localhost" -- <*> envMaybe "PG_PORT" .!= 5432 main :: IO () main = print =<< do decodeEnv :: IO (Either String PGConfig) -- PGConfig { pgHost = "custom-pg-url", pgPort = 5432 }
Synopsis
- class FromEnv a where
- class GFromEnv f where
- class ToEnv a where
- class Typeable a => Var a where
- data EnvList a = EnvList [EnvVar]
- data EnvVar = EnvVar {}
- newtype Parser a = Parser {}
- decodeEnv :: FromEnv a => IO (Either String a)
- decodeWithDefaults :: FromEnv a => a -> IO a
- decode :: FromEnv a => IO (Maybe a)
- showEnv :: IO ()
- setEnvironment :: EnvList a -> IO (Either String ())
- setEnvironment' :: ToEnv a => a -> IO (Either String ())
- unsetEnvironment :: EnvList a -> IO (Either String ())
- unsetEnvironment' :: ToEnv a => a -> IO (Either String ())
- makeEnv :: [EnvVar] -> EnvList a
- env :: Var a => String -> Parser a
- envMaybe :: Var a => String -> Parser (Maybe a)
- (.=) :: Var a => String -> a -> EnvVar
- (.!=) :: Parser (Maybe a) -> a -> Parser a
- newtype ReadShowVar a = ReadShowVar {
- unReadShowVar :: a
- class DefConfig a where
- defConfig :: a
- data Option = Option {}
- defOption :: Option
- runEnv :: Parser a -> IO (Either String a)
- gFromEnvCustom :: forall a. (Generic a, GFromEnv (Rep a)) => Option -> Maybe a -> Parser a
Classes
class GFromEnv f where Source #
Generic
FromEnv
Instances
(GFromEnv a, GFromEnv b) => GFromEnv (a :*: b) Source # | Products |
GFromEnv a => GFromEnv (C1 i a) Source # | Don't absorb meta data |
GFromEnv a => GFromEnv (D1 i a) Source # | Don't absorb meta data |
(Selector s, Var a) => GFromEnv (S1 s (K1 i a :: Type -> Type)) Source # | Construct a |
Type class for objects which can be converted to a set of environment variable settings.
class Typeable a => Var a where Source #
Class for converting to / from an environment variable
Convert a value into an environment variable.
fromVar :: String -> Maybe a Source #
Parse an environment variable.
Instances
Var Int16 Source # | |
Var Int32 Source # | |
Var Int64 Source # | |
Var Int8 Source # | |
Var Word16 Source # | |
Var Word32 Source # | |
Var Word64 Source # | |
Var Word8 Source # | |
Var ByteString Source # | |
Defined in System.Envy | |
Var ByteString Source # | |
Defined in System.Envy | |
Var Text Source # | |
Var Text Source # | |
Var Day Source # | |
Var UTCTime Source # | |
Var String Source # | |
Var Integer Source # | |
Var () Source # | |
Var Bool Source # | |
Var Double Source # | |
Var Int Source # | |
(Var a, Typeable a) => Var (Identity a) Source # | |
(Var a, Typeable a) => Var (First a) Source # | |
(Var a, Typeable a) => Var (Last a) Source # | |
(Typeable a, Show a, Read a) => Var (ReadShowVar a) Source # | |
Defined in System.Envy | |
Var a => Var (Maybe a) Source # | |
List of environment variables. Captures a "phantom type" which allows the type checker to detect the proper implementation of toEnv to use.
Variable type, smart constructor for handling environment variables.
EnvVar | |
|
Parser Monad for environment variable retrieval
Instances
MonadFail Parser Source # | |
Defined in System.Envy | |
MonadIO Parser Source # | |
Defined in System.Envy | |
Alternative Parser Source # | |
Applicative Parser Source # | |
Functor Parser Source # | |
Monad Parser Source # | |
MonadPlus Parser Source # | |
MonadError String Parser Source # | |
Defined in System.Envy throwError :: String -> Parser a # catchError :: Parser a -> (String -> Parser a) -> Parser a # |
Functions
decodeWithDefaults :: FromEnv a => a -> IO a Source #
Environment retrieval with default values provided
setEnvironment :: EnvList a -> IO (Either String ()) Source #
Set environment via a ToEnv constrained type
setEnvironment' :: ToEnv a => a -> IO (Either String ()) Source #
Set environment directly using a value of class ToEnv
unsetEnvironment :: EnvList a -> IO (Either String ()) Source #
Unset Environment from a ToEnv
constrained type
unsetEnvironment' :: ToEnv a => a -> IO (Either String ()) Source #
Unset Environment using a value of class ToEnv
Environment variable getter. Fails if the variable is not set or fails to parse.
Environment variable getter returning Maybe
:: Var a | |
=> String | The variable name to set. |
-> a | Object to set in the environment. |
-> EnvVar | Mapping of Variable to Value. |
Infix environment variable setter
Smart constructor for producing types of EnvVar
:: Parser (Maybe a) | Parser that might fail. |
-> a | Value to return if the parser fails. |
-> Parser a | Parser that returns the default on failure. |
For use with envMaybe
for providing default arguments.
Utility Types
newtype ReadShowVar a Source #
ReadShowVar | |
|
Instances
(Typeable a, Show a, Read a) => Var (ReadShowVar a) Source # | |
Defined in System.Envy |
Generics
For customizing environment variable generation
Option | |
|