{-# 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 (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.EncodeLazy d
a ->
      x -> Sem r x
forall a. a -> Sem r a
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 a. a -> Sem r a
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 a. a -> Sem r 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 a b c. (a -> b) -> Either a c -> Either b c
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 #-}

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 =
  Sem (Error EntityError : r) d -> Sem r (Either EntityError d)
forall e (r :: [(* -> *) -> * -> *]) a.
Sem (Error e : r) a -> Sem r (Either e a)
runError do
    j
raw <- Either EntityError j -> Sem (Error EntityError : r) j
forall e (r :: [(* -> *) -> * -> *]) a.
Member (Error e) r =>
Either e a -> Sem r a
fromEither (Either EntityError j -> Sem (Error EntityError : r) j)
-> Sem (Error EntityError : r) (Either EntityError j)
-> Sem (Error EntityError : r) j
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (s -> Either String j)
-> s -> Sem (Error EntityError : r) (Either EntityError j)
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
    Either EntityError d -> Sem (Error EntityError : r) d
forall e (r :: [(* -> *) -> * -> *]) a.
Member (Error e) r =>
Either e a -> Sem r a
fromEither (Either EntityError d -> Sem (Error EntityError : r) d)
-> (Either Text d -> Either EntityError d)
-> Either Text d
-> Sem (Error EntityError : r) d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> EntityError) -> Either Text d -> Either EntityError d
forall a b c. (a -> b) -> Either a c -> Either b c
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)) (Either Text d -> Sem (Error EntityError : r) d)
-> Sem (Error EntityError : r) (Either Text d)
-> Sem (Error EntityError : r) d
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Sem r (Either Text d)
-> Sem (Error EntityError : r) (Either Text d)
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 (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.DecodeLazy ByteString
body ->
      (ByteString -> Either String j)
-> (j -> Sem r (Either Text d))
-> ByteString
-> Sem r (Either EntityError d)
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 ByteString -> Either String j
forall a. FromJSON a => ByteString -> Either String a
eitherDecode' j -> Sem r (Either Text d)
convert ByteString
body
    Entity.DecodeStrict ByteString
body ->
      (ByteString -> Either String j)
-> (j -> Sem r (Either Text d))
-> ByteString
-> Sem r (Either EntityError d)
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 ByteString -> Either String j
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 (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.DecodeLazy ByteString
body ->
      (j -> d) -> Either EntityError j -> Either EntityError d
forall a b.
(a -> b) -> Either EntityError a -> Either EntityError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap j -> d
convert (Either EntityError j -> x)
-> Sem r (Either EntityError j) -> Sem r x
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 a b.
(a -> b) -> Either EntityError a -> Either EntityError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap j -> d
convert (Either EntityError j -> x)
-> Sem r (Either EntityError j) -> Sem r x
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 #-}