{-# options_haddock prune #-}

-- |Description: Entity Aeson Interpreters, Internal
module Polysemy.Http.Interpreter.AesonEntity where

import Data.Aeson (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 (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.EncodeLazy d
a ->
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. ToJSON a => a -> ByteString
encode (d -> j
convert d
a))
    Entity.EncodeStrict d
a ->
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall l s. LazyStrict l s => l -> s
toStrict (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 =
  forall j d (r :: [(* -> *) -> * -> *]) a.
ToJSON j =>
(d -> j) -> Sem (EntityEncode d : r) a -> Sem r a
interpretEntityEncodeAesonAs 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 =
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> Text -> EntityError
EntityError (forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 s
body) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToText a => a -> Text
toText) forall a b. (a -> b) -> a -> b
$ s -> Either String a
dec s
body
{-# inline decodeWith #-}

convertWith ::
  ConvertUtf8 Text s =>
  (s -> Either String j) ->
  (j -> Sem r (Either Text d)) ->
  s ->
  Sem r (Either EntityError d)
convertWith :: forall s j (r :: [(* -> *) -> * -> *]) d.
ConvertUtf8 Text s =>
(s -> Either String j)
-> (j -> Sem r (Either Text d))
-> s
-> Sem r (Either EntityError d)
convertWith s -> Either String j
dec j -> Sem r (Either Text d)
convert s
body =
  forall e (r :: [(* -> *) -> * -> *]) a.
Sem (Error e : r) a -> Sem r (Either e a)
runError do
    j
raw <- forall e (r :: [(* -> *) -> * -> *]) a.
Member (Error e) r =>
Either e a -> Sem r a
fromEither forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s a (r :: [(* -> *) -> * -> *]).
ConvertUtf8 Text s =>
(s -> Either String a) -> s -> Sem r (Either EntityError a)
decodeWith s -> Either String j
dec s
body
    forall e (r :: [(* -> *) -> * -> *]) a.
Member (Error e) r =>
Either e a -> Sem r a
fromEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> Text -> EntityError
EntityError (forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 s
body)) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
Sem r a -> Sem (e : r) a
raise (j -> Sem r (Either Text d)
convert j
raw)

-- |Interpreter for 'EntityDecode' that uses Aeson and a different codec type.
-- The first parameter is the effectful conversion function.
interpretEntityDecodeAesonWith ::
  FromJSON j =>
  (j -> Sem r (Either Text d)) ->
  Sem (EntityDecode d : r) a ->
  Sem r a
interpretEntityDecodeAesonWith :: forall j (r :: [(* -> *) -> * -> *]) d a.
FromJSON j =>
(j -> Sem r (Either Text d))
-> Sem (EntityDecode d : r) a -> Sem r a
interpretEntityDecodeAesonWith j -> Sem r (Either Text d)
convert =
  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.DecodeLazy ByteString
body ->
      forall s j (r :: [(* -> *) -> * -> *]) d.
ConvertUtf8 Text s =>
(s -> Either String j)
-> (j -> Sem r (Either Text d))
-> s
-> Sem r (Either EntityError d)
convertWith forall a. FromJSON a => ByteString -> Either String a
eitherDecode' j -> Sem r (Either Text d)
convert ByteString
body
    Entity.DecodeStrict ByteString
body ->
      forall s j (r :: [(* -> *) -> * -> *]) d.
ConvertUtf8 Text s =>
(s -> Either String j)
-> (j -> Sem r (Either Text d))
-> s
-> Sem r (Either EntityError d)
convertWith forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict' j -> Sem r (Either Text d)
convert ByteString
body
{-# inline interpretEntityDecodeAesonWith #-}

-- |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 (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.DecodeLazy ByteString
body ->
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap j -> d
convert forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a (r :: [(* -> *) -> * -> *]).
ConvertUtf8 Text s =>
(s -> Either String a) -> s -> Sem r (Either EntityError a)
decodeWith forall a. FromJSON a => ByteString -> Either String a
eitherDecode' ByteString
body
    Entity.DecodeStrict ByteString
body ->
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap j -> d
convert forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a (r :: [(* -> *) -> * -> *]).
ConvertUtf8 Text s =>
(s -> Either String a) -> s -> Sem r (Either EntityError a)
decodeWith 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 =
  forall j d (r :: [(* -> *) -> * -> *]) a.
FromJSON j =>
(j -> d) -> Sem (EntityDecode d : r) a -> Sem r a
interpretEntityDecodeAesonAs forall a. a -> a
id
{-# inline interpretEntityDecodeAeson #-}