{-# options_haddock prune #-}
-- |Description: Entity Aeson Interpreters, Internal
module Polysemy.Http.Interpreter.AesonEntity where

import Data.Aeson (FromJSON, ToJSON, eitherDecode', eitherDecodeStrict', encode)

import Polysemy.Http.Effect.Entity (EntityDecode, EntityEncode, EntityError (EntityError))
import qualified Polysemy.Http.Effect.Entity as Entity (EntityDecode (..), EntityEncode (..))

-- |Interpreter for 'EntityEncode' that uses Aeson and a different codec type.
-- The first parameter is the conversion function.
interpretEntityEncodeAesonAs ::
  ToJSON j =>
  (d -> j) ->
  Sem (EntityEncode d : r) a ->
  Sem r a
interpretEntityEncodeAesonAs :: forall j d (r :: [(* -> *) -> * -> *]) a.
ToJSON j =>
(d -> j) -> Sem (EntityEncode d : r) a -> Sem r a
interpretEntityEncodeAesonAs d -> j
convert =
  (forall (rInitial :: [(* -> *) -> * -> *]) x.
 EntityEncode d (Sem rInitial) x -> Sem r x)
-> Sem (EntityEncode d : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall (rInitial :: [(* -> *) -> * -> *]) x.
 e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
    Entity.Encode d
a ->
      ByteString -> Sem r ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (j -> ByteString
forall a. ToJSON a => a -> ByteString
encode (d -> j
convert d
a))
    Entity.EncodeStrict d
a ->
      x -> Sem r x
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> x
forall l s. LazyStrict l s => l -> s
toStrict (j -> ByteString
forall a. ToJSON a => a -> ByteString
encode (d -> j
convert d
a)))
{-# inline interpretEntityEncodeAesonAs #-}

-- |Interpreter for 'EntityEncode' that uses Aeson.
interpretEntityEncodeAeson ::
  ToJSON d =>
  Sem (EntityEncode d : r) a ->
  Sem r a
interpretEntityEncodeAeson :: forall d (r :: [(* -> *) -> * -> *]) a.
ToJSON d =>
Sem (EntityEncode d : r) a -> Sem r a
interpretEntityEncodeAeson =
  (d -> d) -> Sem (EntityEncode d : r) a -> Sem r a
forall j d (r :: [(* -> *) -> * -> *]) a.
ToJSON j =>
(d -> j) -> Sem (EntityEncode d : r) a -> Sem r a
interpretEntityEncodeAesonAs d -> d
forall a. a -> a
id
{-# inline interpretEntityEncodeAeson #-}

decodeWith ::
  ConvertUtf8 Text s =>
  (s -> Either String a) ->
  s ->
  Sem r (Either EntityError a)
decodeWith :: forall s a (r :: [(* -> *) -> * -> *]).
ConvertUtf8 Text s =>
(s -> Either String a) -> s -> Sem r (Either EntityError a)
decodeWith s -> Either String a
dec s
body =
  Either EntityError a -> Sem r (Either EntityError a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either EntityError a -> Sem r (Either EntityError a))
-> (Either String a -> Either EntityError a)
-> Either String a
-> Sem r (Either EntityError a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> EntityError) -> Either String a -> Either EntityError a
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> Text -> EntityError
EntityError (s -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 s
body) (Text -> EntityError) -> (String -> Text) -> String -> EntityError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. ToText a => a -> Text
toText) (Either String a -> Sem r (Either EntityError a))
-> Either String a -> Sem r (Either EntityError a)
forall a b. (a -> b) -> a -> b
$ s -> Either String a
dec s
body
{-# inline decodeWith #-}

-- |Interpreter for 'EntityDecode' that uses Aeson and a different codec type.
-- The first parameter is the conversion function.
interpretEntityDecodeAesonAs ::
  FromJSON j =>
  (j -> d) ->
  Sem (EntityDecode d : r) a ->
  Sem r a
interpretEntityDecodeAesonAs :: forall j d (r :: [(* -> *) -> * -> *]) a.
FromJSON j =>
(j -> d) -> Sem (EntityDecode d : r) a -> Sem r a
interpretEntityDecodeAesonAs j -> d
convert =
  (forall (rInitial :: [(* -> *) -> * -> *]) x.
 EntityDecode d (Sem rInitial) x -> Sem r x)
-> Sem (EntityDecode d : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall (rInitial :: [(* -> *) -> * -> *]) x.
 e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
    Entity.Decode ByteString
body ->
      (j -> d) -> Either EntityError j -> Either EntityError d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap j -> d
convert (Either EntityError j -> Either EntityError d)
-> Sem r (Either EntityError j) -> Sem r (Either EntityError d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either String j)
-> ByteString -> Sem r (Either EntityError j)
forall s a (r :: [(* -> *) -> * -> *]).
ConvertUtf8 Text s =>
(s -> Either String a) -> s -> Sem r (Either EntityError a)
decodeWith ByteString -> Either String j
forall a. FromJSON a => ByteString -> Either String a
eitherDecode' ByteString
body
    Entity.DecodeStrict ByteString
body ->
      (j -> d) -> Either EntityError j -> Either EntityError d
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap j -> d
convert (Either EntityError j -> Either EntityError d)
-> Sem r (Either EntityError j) -> Sem r (Either EntityError d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either String j)
-> ByteString -> Sem r (Either EntityError j)
forall s a (r :: [(* -> *) -> * -> *]).
ConvertUtf8 Text s =>
(s -> Either String a) -> s -> Sem r (Either EntityError a)
decodeWith ByteString -> Either String j
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict' ByteString
body
{-# inline interpretEntityDecodeAesonAs #-}

-- |Interpreter for 'EntityDecode' that uses Aeson.
interpretEntityDecodeAeson ::
  FromJSON d =>
  Sem (EntityDecode d : r) a ->
  Sem r a
interpretEntityDecodeAeson :: forall d (r :: [(* -> *) -> * -> *]) a.
FromJSON d =>
Sem (EntityDecode d : r) a -> Sem r a
interpretEntityDecodeAeson =
  (d -> d) -> Sem (EntityDecode d : r) a -> Sem r a
forall j d (r :: [(* -> *) -> * -> *]) a.
FromJSON j =>
(j -> d) -> Sem (EntityDecode d : r) a -> Sem r a
interpretEntityDecodeAesonAs d -> d
forall a. a -> a
id
{-# inline interpretEntityDecodeAeson #-}