{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}

-- | Helpers for rendering responses via Jordan.
module Jordan.Servant.Response where

import Data.Attoparsec.ByteString.Lazy (parseOnly)
import Data.Proxy
import GHC.Generics
import Jordan
import Servant.API
import Servant.API.ContentTypes
import Servant.API.Modifiers

-- | Wrapper to perform JSON serialization via Jordan.
--
-- Types used with this wrapper should have isomorphic 'Jordan.ToJSON' and 'Jordan.FromJSON' instances.
-- A utility is provided to check this.
newtype ViaJordan a = ViaJordan {ViaJordan a -> a
getViaJordan :: a}
  deriving (Int -> ViaJordan a -> ShowS
[ViaJordan a] -> ShowS
ViaJordan a -> String
(Int -> ViaJordan a -> ShowS)
-> (ViaJordan a -> String)
-> ([ViaJordan a] -> ShowS)
-> Show (ViaJordan a)
forall a. Show a => Int -> ViaJordan a -> ShowS
forall a. Show a => [ViaJordan a] -> ShowS
forall a. Show a => ViaJordan a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ViaJordan a] -> ShowS
$cshowList :: forall a. Show a => [ViaJordan a] -> ShowS
show :: ViaJordan a -> String
$cshow :: forall a. Show a => ViaJordan a -> String
showsPrec :: Int -> ViaJordan a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> ViaJordan a -> ShowS
Show, ReadPrec [ViaJordan a]
ReadPrec (ViaJordan a)
Int -> ReadS (ViaJordan a)
ReadS [ViaJordan a]
(Int -> ReadS (ViaJordan a))
-> ReadS [ViaJordan a]
-> ReadPrec (ViaJordan a)
-> ReadPrec [ViaJordan a]
-> Read (ViaJordan a)
forall a. Read a => ReadPrec [ViaJordan a]
forall a. Read a => ReadPrec (ViaJordan a)
forall a. Read a => Int -> ReadS (ViaJordan a)
forall a. Read a => ReadS [ViaJordan a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ViaJordan a]
$creadListPrec :: forall a. Read a => ReadPrec [ViaJordan a]
readPrec :: ReadPrec (ViaJordan a)
$creadPrec :: forall a. Read a => ReadPrec (ViaJordan a)
readList :: ReadS [ViaJordan a]
$creadList :: forall a. Read a => ReadS [ViaJordan a]
readsPrec :: Int -> ReadS (ViaJordan a)
$creadsPrec :: forall a. Read a => Int -> ReadS (ViaJordan a)
Read, ViaJordan a -> ViaJordan a -> Bool
(ViaJordan a -> ViaJordan a -> Bool)
-> (ViaJordan a -> ViaJordan a -> Bool) -> Eq (ViaJordan a)
forall a. Eq a => ViaJordan a -> ViaJordan a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ViaJordan a -> ViaJordan a -> Bool
$c/= :: forall a. Eq a => ViaJordan a -> ViaJordan a -> Bool
== :: ViaJordan a -> ViaJordan a -> Bool
$c== :: forall a. Eq a => ViaJordan a -> ViaJordan a -> Bool
Eq, Eq (ViaJordan a)
Eq (ViaJordan a)
-> (ViaJordan a -> ViaJordan a -> Ordering)
-> (ViaJordan a -> ViaJordan a -> Bool)
-> (ViaJordan a -> ViaJordan a -> Bool)
-> (ViaJordan a -> ViaJordan a -> Bool)
-> (ViaJordan a -> ViaJordan a -> Bool)
-> (ViaJordan a -> ViaJordan a -> ViaJordan a)
-> (ViaJordan a -> ViaJordan a -> ViaJordan a)
-> Ord (ViaJordan a)
ViaJordan a -> ViaJordan a -> Bool
ViaJordan a -> ViaJordan a -> Ordering
ViaJordan a -> ViaJordan a -> ViaJordan a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (ViaJordan a)
forall a. Ord a => ViaJordan a -> ViaJordan a -> Bool
forall a. Ord a => ViaJordan a -> ViaJordan a -> Ordering
forall a. Ord a => ViaJordan a -> ViaJordan a -> ViaJordan a
min :: ViaJordan a -> ViaJordan a -> ViaJordan a
$cmin :: forall a. Ord a => ViaJordan a -> ViaJordan a -> ViaJordan a
max :: ViaJordan a -> ViaJordan a -> ViaJordan a
$cmax :: forall a. Ord a => ViaJordan a -> ViaJordan a -> ViaJordan a
>= :: ViaJordan a -> ViaJordan a -> Bool
$c>= :: forall a. Ord a => ViaJordan a -> ViaJordan a -> Bool
> :: ViaJordan a -> ViaJordan a -> Bool
$c> :: forall a. Ord a => ViaJordan a -> ViaJordan a -> Bool
<= :: ViaJordan a -> ViaJordan a -> Bool
$c<= :: forall a. Ord a => ViaJordan a -> ViaJordan a -> Bool
< :: ViaJordan a -> ViaJordan a -> Bool
$c< :: forall a. Ord a => ViaJordan a -> ViaJordan a -> Bool
compare :: ViaJordan a -> ViaJordan a -> Ordering
$ccompare :: forall a. Ord a => ViaJordan a -> ViaJordan a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (ViaJordan a)
Ord, (forall x. ViaJordan a -> Rep (ViaJordan a) x)
-> (forall x. Rep (ViaJordan a) x -> ViaJordan a)
-> Generic (ViaJordan a)
forall x. Rep (ViaJordan a) x -> ViaJordan a
forall x. ViaJordan a -> Rep (ViaJordan a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ViaJordan a) x -> ViaJordan a
forall a x. ViaJordan a -> Rep (ViaJordan a) x
$cto :: forall a x. Rep (ViaJordan a) x -> ViaJordan a
$cfrom :: forall a x. ViaJordan a -> Rep (ViaJordan a) x
Generic)

instance FromJSON a => FromJSON (ViaJordan a) where
  fromJSON :: f (ViaJordan a)
fromJSON = a -> ViaJordan a
forall a. a -> ViaJordan a
ViaJordan (a -> ViaJordan a) -> f a -> f (ViaJordan a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
forall value (f :: * -> *).
(FromJSON value, JSONParser f) =>
f value
fromJSON

instance ToJSON a => ToJSON (ViaJordan a) where
  toJSON :: f (ViaJordan a)
toJSON = (ViaJordan a -> a) -> f a -> f (ViaJordan a)
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap ViaJordan a -> a
forall a. ViaJordan a -> a
getViaJordan f a
forall v (f :: * -> *). (ToJSON v, JSONSerializer f) => f v
toJSON

instance (HasStatus a) => HasStatus (ViaJordan a) where
  type StatusOf (ViaJordan a) = StatusOf a

-- | Overlapping instance: sidestep Aeson, use Jordan.
instance {-# OVERLAPPING #-} (ToJSON a) => MimeRender JSON (ViaJordan a) where
  mimeRender :: Proxy JSON -> ViaJordan a -> ByteString
mimeRender Proxy JSON
Proxy = a -> ByteString
forall a. ToJSON a => a -> ByteString
toJSONViaBuilder (a -> ByteString)
-> (ViaJordan a -> a) -> ViaJordan a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViaJordan a -> a
forall a. ViaJordan a -> a
getViaJordan

-- | Overlapping instance: sidestep Aeson, just Jordan.
instance {-# OVERLAPPING #-} (FromJSON a) => MimeUnrender JSON (ViaJordan a) where
  mimeUnrender :: Proxy JSON -> ByteString -> Either String (ViaJordan a)
mimeUnrender Proxy JSON
Proxy = Parser (ViaJordan a) -> ByteString -> Either String (ViaJordan a)
forall a. Parser a -> ByteString -> Either String a
parseOnly (a -> ViaJordan a
forall a. a -> ViaJordan a
ViaJordan (a -> ViaJordan a) -> Parser ByteString a -> Parser (ViaJordan a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString a
forall val. FromJSON val => Parser val
attoparsecParser)
  mimeUnrenderWithType :: Proxy JSON
-> MediaType -> ByteString -> Either String (ViaJordan a)
mimeUnrenderWithType Proxy JSON
Proxy MediaType
_ = Parser (ViaJordan a) -> ByteString -> Either String (ViaJordan a)
forall a. Parser a -> ByteString -> Either String a
parseOnly (a -> ViaJordan a
forall a. a -> ViaJordan a
ViaJordan (a -> ViaJordan a) -> Parser ByteString a -> Parser (ViaJordan a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString a
forall val. FromJSON val => Parser val
attoparsecParser)