servant-checked-exceptions 0.3.0.2 → 0.4.0.0
raw patch · 5 files changed
+90/−10 lines, 5 filesdep +profunctorsdep +taggeddep −lensPVP ok
version bump matches the API change (PVP)
Dependencies added: profunctors, tagged
Dependencies removed: lens
API changes (from Hackage documentation)
+ Servant.Checked.Exceptions.Internal.Prism: (<>~) :: Monoid a => ASetter s t a a -> a -> s -> t
+ Servant.Checked.Exceptions.Internal.Prism: infixr 4 <>~
+ Servant.Checked.Exceptions.Internal.Prism: iso :: (s -> a) -> (b -> t) -> Iso s t a b
+ Servant.Checked.Exceptions.Internal.Prism: preview :: Prism' s a -> s -> Maybe a
+ Servant.Checked.Exceptions.Internal.Prism: prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
+ Servant.Checked.Exceptions.Internal.Prism: prism' :: (a -> s) -> (s -> Maybe a) -> Prism' s a
+ Servant.Checked.Exceptions.Internal.Prism: review :: Prism' t b -> b -> t
+ Servant.Checked.Exceptions.Internal.Prism: type Prism' s a = Prism s s a a
+ Servant.Checked.Exceptions.Internal.Prism: type Iso s t a b = forall p f. (Profunctor p, Functor f) => p a (f b) -> p s (f t)
Files
- servant-checked-exceptions.cabal +4/−2
- src/Servant/Checked/Exceptions/Internal/Envelope.hs +4/−3
- src/Servant/Checked/Exceptions/Internal/Prism.hs +75/−0
- src/Servant/Checked/Exceptions/Internal/Servant/Docs.hs +2/−1
- src/Servant/Checked/Exceptions/Internal/Union.hs +5/−4
servant-checked-exceptions.cabal view
@@ -1,5 +1,5 @@ name: servant-checked-exceptions-version: 0.3.0.2+version: 0.4.0.0 synopsis: Checked exceptions for Servant APIs. description: Please see <https://github.com/cdepillabout/servant-checked-exceptions#readme README.md>. homepage: https://github.com/cdepillabout/servant-checked-exceptions@@ -22,6 +22,7 @@ exposed-modules: Servant.Checked.Exceptions , Servant.Checked.Exceptions.Internal , Servant.Checked.Exceptions.Internal.Envelope+ , Servant.Checked.Exceptions.Internal.Prism , Servant.Checked.Exceptions.Internal.Product , Servant.Checked.Exceptions.Internal.Servant , Servant.Checked.Exceptions.Internal.Servant.API@@ -35,7 +36,8 @@ , bytestring , deepseq , http-media- , lens+ , profunctors+ , tagged , servant >= 0.9 , servant-client >= 0.9 , servant-docs >= 0.9
src/Servant/Checked/Exceptions/Internal/Envelope.hs view
@@ -60,7 +60,6 @@ ) where import Control.Applicative ((<|>))-import Control.Lens (Iso, Prism, Prism', iso, preview, prism) import Control.Monad.Fix (MonadFix(mfix)) import Data.Aeson (FromJSON(parseJSON), ToJSON(toJSON), Value, (.=), (.:), object,@@ -71,6 +70,8 @@ import Data.Typeable (Typeable) import GHC.Generics (Generic) +import Servant.Checked.Exceptions.Internal.Prism+ (Iso, Prism, Prism', iso, preview, prism) import Servant.Checked.Exceptions.Internal.Product (ToOpenProduct) import Servant.Checked.Exceptions.Internal.Union (IsMember, OpenUnion, catchesOpenUnion, openUnionLift,@@ -81,12 +82,12 @@ -- $setup -- >>> :set -XDataKinds -- >>> :set -XTypeOperators--- >>> import Control.Lens (preview, review) -- >>> import Data.Aeson (encode) -- >>> import Data.ByteString.Lazy.Char8 (hPutStrLn) -- >>> import Data.Text (Text) -- >>> import System.IO (stdout) -- >>> import Text.Read (readMaybe)+-- >>> import Servant.Checked.Exceptions.Internal.Prism (review) -- >>> let putByteStrLn = hPutStrLn stdout @@ -425,7 +426,7 @@ -- don't overlap. -- -- For an explanation, see the documentation on the 'FromJSON' instance for--- 'Union'.+-- 'Servant.Checked.Exceptions.Internal.Union.Union'. instance (FromJSON (OpenUnion es), FromJSON a) => FromJSON (Envelope es a) where parseJSON :: Value -> Parser (Envelope es a) parseJSON = withObject "Envelope" $ \obj ->
+ src/Servant/Checked/Exceptions/Internal/Prism.hs view
@@ -0,0 +1,75 @@+{-# LANGUAGE RankNTypes #-}++{- |+Module : Servant.Checked.Exceptions.Internal.Envelope+License : BSD3+Maintainer : Dennis Gosnell (cdep.illabout@gmail.com)+Stability : experimental+Portability : unknown++These functions are for working with Optics popularized by the+<https://hackage.haskell.org/package/lens lens> package. Documentation can be+found in the lens package. These functions are redefined here to remove the+dependency on the lens package.+-}++module Servant.Checked.Exceptions.Internal.Prism+ ( Prism+ , prism+ , Prism'+ , prism'+ , Iso+ , iso+ , review+ , preview+ , (<>~)+ ) where++import Data.Profunctor.Unsafe((#.))+import Control.Applicative+import Data.Coerce+import Data.Functor.Identity+import Data.Monoid+import Data.Profunctor+import Data.Tagged++type Iso s t a b+ = forall p f. (Profunctor p, Functor f) =>+ p a (f b) -> p s (f t)++type Prism s t a b+ = forall p f. (Choice p, Applicative f) =>+ p a (f b) -> p s (f t)++type Prism' s a = Prism s s a a++type ASetter s t a b = (a -> Identity b) -> s -> Identity t++iso :: (s -> a) -> (b -> t) -> Iso s t a b+iso sa bt = dimap sa (fmap bt)+{-# INLINE iso #-}++prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b+prism bt seta = dimap seta (either pure (fmap bt)) . right'+{-# INLINE prism #-}++prism' :: (a -> s) -> (s -> Maybe a) -> Prism' s a+prism' bs sma = prism bs (\s -> maybe (Left s) Right (sma s))+{-# INLINE prism' #-}++review :: Prism' t b -> b -> t+review p = coerce . p . Tagged . Identity+{-# INLINE review #-}++preview :: Prism' s a -> s -> Maybe a+preview l = coerce . l (Const . First . Just)+{-# INLINE preview #-}++over :: ASetter s t a b -> (a -> b) -> s -> t+over l f = runIdentity #. l (Identity #. f)+{-# INLINE over #-}++infixr 4 <>~+(<>~) :: Monoid a => ASetter s t a a -> a -> s -> t+l <>~ n = over l (`mappend` n)+{-# INLINE (<>~) #-}
src/Servant/Checked/Exceptions/Internal/Servant/Docs.hs view
@@ -28,9 +28,9 @@ module Servant.Checked.Exceptions.Internal.Servant.Docs where -import Control.Lens ((&), (<>~)) import Data.Proxy (Proxy(Proxy)) import Data.ByteString.Lazy (ByteString)+import Data.Function ((&)) import Data.Monoid ((<>)) import Data.Text (Text) import Network.HTTP.Media (MediaType)@@ -43,6 +43,7 @@ import Servant.Checked.Exceptions.Internal.Envelope (Envelope, toErrEnvelope, toSuccEnvelope)+import Servant.Checked.Exceptions.Internal.Prism ((<>~)) import Servant.Checked.Exceptions.Internal.Servant.API (Throws, Throwing) import Servant.Checked.Exceptions.Internal.Util (Snoc)
src/Servant/Checked/Exceptions/Internal/Union.hs view
@@ -67,7 +67,6 @@ -- Imports for Union stuff import Control.Applicative ((<|>))-import Control.Lens (Prism, Prism', iso, preview, prism, prism', review) import Control.DeepSeq (NFData(rnf)) import Data.Aeson (FromJSON(parseJSON), ToJSON(toJSON), Value)@@ -76,6 +75,7 @@ import Data.Typeable (Typeable) import Text.Read (Read(readPrec), ReadPrec, (<++)) +import Servant.Checked.Exceptions.Internal.Prism (Prism, Prism', iso, preview, prism, prism', review) import Servant.Checked.Exceptions.Internal.Product (Product(Cons, Nil), ToOpenProduct, ToProduct, tupleToOpenProduct, tupleToProduct)@@ -492,9 +492,10 @@ -- >>> p >>= openUnionMatch :: Maybe String -- Just "hello" ----- However, imagine are we working with a 'Union' of a 'String' and 'Text'.--- @\"hello\"@ can be 'read' as both a 'String' and 'Text'. However, in the--- following example, it can only be read as a 'String':+-- However, imagine are we working with a 'Union' of a 'String' and+-- 'Data.Text.Text'. @\"hello\"@ can be 'read' as both a 'String' and+-- 'Data.Text.Text'. However, in the following example, it can only be read as+-- a 'String': -- -- >>> let q = readMaybe "Identity \"hello\"" :: Maybe (Union Identity '[String, Text]) -- >>> q