envy-extensible 0.1.0.0 → 0.2.0.0
raw patch · 3 files changed
+62/−10 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Extensible.Envy: instance Type.Membership.Forall (Type.Membership.KeyTargetAre GHC.TypeLits.KnownSymbol (Data.Extensible.Dictionary.Instance1 System.Envy.Var h)) xs => System.Envy.FromEnv (Data.Extensible.Field.RecordOf h xs)
+ Data.Extensible.Envy: recordFromEnvWithDefault :: forall (xs :: [Assoc Symbol Type]) h. Forall (KeyTargetAre KnownSymbol (Instance1 Var h)) xs => RecordOf h xs -> FieldLabelToEnvName -> Parser (RecordOf h xs)
+ Data.Extensible.Envy.Instances: instance Type.Membership.Forall (Type.Membership.KeyTargetAre GHC.TypeLits.KnownSymbol (Data.Extensible.Dictionary.Instance1 System.Envy.Var h)) xs => System.Envy.FromEnv (Data.Extensible.Field.RecordOf h xs)
Files
- envy-extensible.cabal +2/−1
- src/Data/Extensible/Envy.hs +24/−9
- src/Data/Extensible/Envy/Instances.hs +36/−0
envy-extensible.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: envy-extensible-version: 0.1.0.0+version: 0.2.0.0 synopsis: Provides FromEnv in envy instance for Record of extensible. -- description: homepage: https://github.com/igrep/envy-extensible#readme@@ -23,6 +23,7 @@ import: shared hs-source-dirs: src exposed-modules: Data.Extensible.Envy+ Data.Extensible.Envy.Instances test-suite envy-extensible-test import: shared
src/Data/Extensible/Envy.hs view
@@ -16,11 +16,13 @@ -- Maintainer: YAMAMOTO Yuji <yuji-yamamoto@iij.ad.jp> -- Stability : Experimental ----- Provides 'Env.FromEnv' instance for 'Ex.Record' and functions to--- create 'Ex.Record' from environment variable using 'Env.Parser'.+-- Provides functions to create 'Ex.Record' from environment variable+-- using 'Env.Parser'. --------------------------------------------------------------------------------+ module Data.Extensible.Envy ( recordFromEnvWith+ , recordFromEnvWithDefault , recordFromEnv , FieldLabelToEnvName , defaultFieldLabelToEnvName@@ -28,6 +30,7 @@ ) where +import Control.Applicative ((<|>)) import qualified Data.Char as C import Data.Extensible (Forall, Instance1) import qualified Data.Extensible as Ex@@ -97,16 +100,28 @@ {-# INLINE recordFromEnvWith #-} --- | Returns 'recordFromEnv' when the first argument of 'Env.fromEnv' is @Nothing@.-instance Forall (Ex.KeyTargetAre KnownSymbol (Instance1 Env.Var h)) xs- => Env.FromEnv (Ex.RecordOf (h :: Type -> Type) xs)+-- | 'Env.Parser' for 'Ex.Record'.+-- This is necessary to implement the instance of 'Env.FromEnv' for+-- 'Ex.Record' correctly.+recordFromEnvWithDefault+ :: forall (xs :: [Ex.Assoc Symbol Type]) h+ . Forall (Ex.KeyTargetAre KnownSymbol (Instance1 Env.Var h)) xs+ => Ex.RecordOf h xs+ -> FieldLabelToEnvName+ -> Env.Parser (Ex.RecordOf h xs)+recordFromEnvWithDefault def fl2en =+ Ex.hgenerateFor+ (Proxy :: Proxy (Ex.KeyTargetAre KnownSymbol (Instance1 Env.Var h))) f where- fromEnv (Just r) = pure r- fromEnv Nothing = recordFromEnv- {-# INLINE fromEnv #-}+ f membership =+ ( Ex.Field <$> Env.env (fl2en $ Ex.stringKeyOf membership)+ ) <|> pure (Ex.hlookup membership def)+{-# INLINE recordFromEnvWithDefault #-} --- | Necessary to make 'Ex.Record' an instance of 'Env.FromEnv'+-- | Necessary to make a 'Env.Parser' for 'Ex.Record'.+--+-- TODO: Remove this after <https://github.com/dmjio/envy/pull/31> is released. instance Env.Var a => Env.Var (Identity a) where toVar = Env.toVar . runIdentity {-# INLINE toVar #-}
+ src/Data/Extensible/Envy/Instances.hs view
@@ -0,0 +1,36 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}++--------------------------------------------------------------------------------+-- |+-- Module : Data.Extensible.Envy+-- Copyright : (c) 2020 IIJ Innovation Institute, Inc.+-- License : BSD3+-- Maintainer: YAMAMOTO Yuji <yuji-yamamoto@iij.ad.jp>+-- Stability : Experimental+--+-- Provides 'Env.FromEnv' instance for 'Ex.Record'.+--------------------------------------------------------------------------------++module Data.Extensible.Envy.Instances where+++import Data.Extensible (Forall, Instance1)+import qualified Data.Extensible as Ex+import Data.Extensible.Envy+import Data.Kind (Type)+import GHC.TypeLits (KnownSymbol)+import qualified System.Envy as Env+++-- | Returns 'recordFromEnv' when the first argument of 'Env.fromEnv' is @Nothing@.+instance Forall (Ex.KeyTargetAre KnownSymbol (Instance1 Env.Var h)) xs+ => Env.FromEnv (Ex.RecordOf (h :: Type -> Type) xs)+ where+ fromEnv (Just r) = recordFromEnvWithDefault r (defaultFieldLabelToEnvName "")+ fromEnv Nothing = recordFromEnv+ {-# INLINE fromEnv #-}