envy-2.1.3.0: An environmentally friendly way to deal with environment variables
Copyright(c) David Johnson 2015
Maintainerdjohnson.m@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.Envy

Description

{-# 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

Classes

class FromEnv a where Source #

FromEnv Typeclass w/ Generic default implementation

Minimal complete definition

Nothing

Methods

fromEnv :: Maybe a -> Parser a Source #

default fromEnv :: (Generic a, GFromEnv (Rep a)) => Maybe a -> Parser a Source #

class GFromEnv f where Source #

Generic FromEnv

Methods

gFromEnv :: Option -> Maybe (f a) -> Parser (f a) Source #

Instances

Instances details
(GFromEnv a, GFromEnv b) => GFromEnv (a :*: b) Source #

Products

Instance details

Defined in System.Envy

Methods

gFromEnv :: Option -> Maybe ((a :*: b) a0) -> Parser ((a :*: b) a0) Source #

GFromEnv a => GFromEnv (C1 i a) Source #

Don't absorb meta data

Instance details

Defined in System.Envy

Methods

gFromEnv :: Option -> Maybe (C1 i a a0) -> Parser (C1 i a a0) Source #

GFromEnv a => GFromEnv (D1 i a) Source #

Don't absorb meta data

Instance details

Defined in System.Envy

Methods

gFromEnv :: Option -> Maybe (D1 i a a0) -> Parser (D1 i a a0) Source #

(Selector s, Var a) => GFromEnv (S1 s (K1 i a :: Type -> Type)) Source #

Construct a Parser from a selName and DefConfig record field

Instance details

Defined in System.Envy

Methods

gFromEnv :: Option -> Maybe (S1 s (K1 i a) a0) -> Parser (S1 s (K1 i a) a0) Source #

class ToEnv a where Source #

Type class for objects which can be converted to a set of environment variable settings.

Methods

toEnv :: a -> EnvList a Source #

Convert an object into a list of environment variable settings.

class Typeable a => Var a where Source #

Class for converting to / from an environment variable

Methods

toVar :: a -> String Source #

Convert a value into an environment variable.

fromVar :: String -> Maybe a Source #

Parse an environment variable.

Instances

Instances details
Var Int16 Source # 
Instance details

Defined in System.Envy

Var Int32 Source # 
Instance details

Defined in System.Envy

Var Int64 Source # 
Instance details

Defined in System.Envy

Var Int8 Source # 
Instance details

Defined in System.Envy

Var Word16 Source # 
Instance details

Defined in System.Envy

Var Word32 Source # 
Instance details

Defined in System.Envy

Var Word64 Source # 
Instance details

Defined in System.Envy

Var Word8 Source # 
Instance details

Defined in System.Envy

Var ByteString Source # 
Instance details

Defined in System.Envy

Var ByteString Source # 
Instance details

Defined in System.Envy

Var Text Source # 
Instance details

Defined in System.Envy

Var Text Source # 
Instance details

Defined in System.Envy

Var Day Source # 
Instance details

Defined in System.Envy

Var UTCTime Source # 
Instance details

Defined in System.Envy

Var String Source # 
Instance details

Defined in System.Envy

Var Integer Source # 
Instance details

Defined in System.Envy

Var () Source # 
Instance details

Defined in System.Envy

Methods

toVar :: () -> String Source #

fromVar :: String -> Maybe () Source #

Var Bool Source # 
Instance details

Defined in System.Envy

Var Double Source # 
Instance details

Defined in System.Envy

Var Int Source # 
Instance details

Defined in System.Envy

(Var a, Typeable a) => Var (Identity a) Source # 
Instance details

Defined in System.Envy

(Var a, Typeable a) => Var (First a) Source # 
Instance details

Defined in System.Envy

(Var a, Typeable a) => Var (Last a) Source # 
Instance details

Defined in System.Envy

Methods

toVar :: Last a -> String Source #

fromVar :: String -> Maybe (Last a) Source #

(Typeable a, Show a, Read a) => Var (ReadShowVar a) Source # 
Instance details

Defined in System.Envy

Var a => Var (Maybe a) Source # 
Instance details

Defined in System.Envy

data EnvList a Source #

List of environment variables. Captures a "phantom type" which allows the type checker to detect the proper implementation of toEnv to use.

Constructors

EnvList [EnvVar] 

Instances

Instances details
Show (EnvList a) Source # 
Instance details

Defined in System.Envy

Methods

showsPrec :: Int -> EnvList a -> ShowS #

show :: EnvList a -> String #

showList :: [EnvList a] -> ShowS #

data EnvVar Source #

Variable type, smart constructor for handling environment variables.

Constructors

EnvVar 

Fields

Instances

Instances details
Show EnvVar Source # 
Instance details

Defined in System.Envy

Eq EnvVar Source # 
Instance details

Defined in System.Envy

Methods

(==) :: EnvVar -> EnvVar -> Bool #

(/=) :: EnvVar -> EnvVar -> Bool #

newtype Parser a Source #

Parser Monad for environment variable retrieval

Constructors

Parser 

Fields

Instances

Instances details
MonadFail Parser Source # 
Instance details

Defined in System.Envy

Methods

fail :: String -> Parser a #

MonadIO Parser Source # 
Instance details

Defined in System.Envy

Methods

liftIO :: IO a -> Parser a #

Alternative Parser Source # 
Instance details

Defined in System.Envy

Methods

empty :: Parser a #

(<|>) :: Parser a -> Parser a -> Parser a #

some :: Parser a -> Parser [a] #

many :: Parser a -> Parser [a] #

Applicative Parser Source # 
Instance details

Defined in System.Envy

Methods

pure :: a -> Parser a #

(<*>) :: Parser (a -> b) -> Parser a -> Parser b #

liftA2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c #

(*>) :: Parser a -> Parser b -> Parser b #

(<*) :: Parser a -> Parser b -> Parser a #

Functor Parser Source # 
Instance details

Defined in System.Envy

Methods

fmap :: (a -> b) -> Parser a -> Parser b #

(<$) :: a -> Parser b -> Parser a #

Monad Parser Source # 
Instance details

Defined in System.Envy

Methods

(>>=) :: Parser a -> (a -> Parser b) -> Parser b #

(>>) :: Parser a -> Parser b -> Parser b #

return :: a -> Parser a #

MonadPlus Parser Source # 
Instance details

Defined in System.Envy

Methods

mzero :: Parser a #

mplus :: Parser a -> Parser a -> Parser a #

MonadError String Parser Source # 
Instance details

Defined in System.Envy

Methods

throwError :: String -> Parser a #

catchError :: Parser a -> (String -> Parser a) -> Parser a #

Functions

decodeEnv :: FromEnv a => IO (Either String a) Source #

Environment retrieval with failure info

decodeWithDefaults :: FromEnv a => a -> IO a Source #

Environment retrieval with default values provided

decode :: FromEnv a => IO (Maybe a) Source #

Environment retrieval (with no failure info)

showEnv :: IO () Source #

Display all environment variables, for convenience

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

makeEnv :: [EnvVar] -> EnvList a Source #

Smart constructor, environment creation helper.

env Source #

Arguments

:: Var a 
=> String

Key to look up.

-> Parser a

Return a value of this type or throw an error.

Environment variable getter. Fails if the variable is not set or fails to parse.

envMaybe Source #

Arguments

:: Var a 
=> String

Key to look up.

-> Parser (Maybe a)

Return Nothing if variable isn't set.

Environment variable getter returning Maybe

(.=) Source #

Arguments

:: 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

(.!=) Source #

Arguments

:: 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 #

A utility type to use any instance of Read and Show as an instance of Var.

Constructors

ReadShowVar 

Fields

Instances

Instances details
(Typeable a, Show a, Read a) => Var (ReadShowVar a) Source # 
Instance details

Defined in System.Envy

Generics

class DefConfig a where Source #

Type class for objects which have a default configuration.

Methods

defConfig :: a Source #

data Option Source #

For customizing environment variable generation

Constructors

Option 

Fields

Instances

Instances details
Show Option Source # 
Instance details

Defined in System.Envy

defOption :: Option Source #

Default Option for field modification

runEnv :: Parser a -> IO (Either String a) Source #

For use with Generics, no FromEnv typeclass necessary

getPgConfig :: IO (Either String ConnectInfo)
getPgConfig = runEnv $ gFromEnvCustom defOption

gFromEnvCustom :: forall a. (Generic a, GFromEnv (Rep a)) => Option -> Maybe a -> Parser a Source #

Meant for specifying a custom Option for environment retrieval

instance FromEnv PGConfig where
  fromEnv = gFromEnvCustom Option { dropPrefixCount = 8, customPrefix = "PG" }