generic-env: Generic Environment Generator

[ development, library, mit ] [ Propose Tags ]


Maintainer's Corner

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],
Change log
Dependencies base (>=4.7 && <5), containers, text [details]
License MIT
Copyright 2019 Yigit Ozkavci
Author Yigit Ozkavci
Category Development
Home page
Bug tracker
Source repo head: git clone
Uploaded by yigitozkavci at 2019-04-30T20:24:16Z
Distributions NixOS:
Downloads 816 total (10 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2019-04-30 [all 1 reports]

Readme for generic-env-

[back to package description]


generic-env lets you produce your generic type from a given subset of environment variables.


{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}

module Example1 where

import GenericEnv
import GHC.Generics
import System.Environment

data AppEnv = AppEnv { name :: String, ver :: String } deriving (Generic, Show)

main :: IO ()
main = do
  setEnv "APP_NAME" "wow"
  setEnv "APP_VER" "1.0"
  print =<< fromEnv @AppEnv (withPrefix "APP_")
> main
Right (AppEnv {name = "wow", ver = "1.0"})

Let's change our version from String to Float:

data AppEnv = AppEnv { name :: String, ver :: Float } deriving (Generic, Show)

and run the same code:

> main
Right (AppEnv {name = "wow", ver = 1.0})

Next, try making the version an integer:

data AppEnv = AppEnv { name :: String, ver :: Int } deriving (Generic, Show)
> main
Left "Could not parse value of type 'Int' for the field named 'ver' from environment variable 'APP_VER=1.0'"


MIT License