module Hasql.Decoders.Value where

import Hasql.Prelude
import PostgreSQL.Binary.Decoding qualified as A

newtype Value a
  = Value (Bool -> A.Value a)
  deriving ((forall a b. (a -> b) -> Value a -> Value b)
-> (forall a b. a -> Value b -> Value a) -> Functor Value
forall a b. a -> Value b -> Value a
forall a b. (a -> b) -> Value a -> Value b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Value a -> Value b
fmap :: forall a b. (a -> b) -> Value a -> Value b
$c<$ :: forall a b. a -> Value b -> Value a
<$ :: forall a b. a -> Value b -> Value a
Functor)

instance Filterable Value where
  {-# INLINE mapMaybe #-}
  mapMaybe :: forall a b. (a -> Maybe b) -> Value a -> Value b
mapMaybe a -> Maybe b
fn =
    (a -> Either Text b) -> Value a -> Value b
forall a b. (a -> Either Text b) -> Value a -> Value b
refine (Either Text b -> (b -> Either Text b) -> Maybe b -> Either Text b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either Text b
forall a b. a -> Either a b
Left Text
"Invalid value") b -> Either Text b
forall a b. b -> Either a b
Right (Maybe b -> Either Text b) -> (a -> Maybe b) -> a -> Either Text b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Maybe b
fn)

{-# INLINE run #-}
run :: Value a -> Bool -> A.Value a
run :: forall a. Value a -> Bool -> Value a
run (Value Bool -> Value a
imp) Bool
integerDatetimes =
  Bool -> Value a
imp Bool
integerDatetimes

{-# INLINE decoder #-}
decoder :: (Bool -> A.Value a) -> Value a
decoder :: forall a. (Bool -> Value a) -> Value a
decoder =
  {-# SCC "decoder" #-}
  (Bool -> Value a) -> Value a
forall a. (Bool -> Value a) -> Value a
Value

{-# INLINE decoderFn #-}
decoderFn :: (Bool -> ByteString -> Either Text a) -> Value a
decoderFn :: forall a. (Bool -> ByteString -> Either Text a) -> Value a
decoderFn Bool -> ByteString -> Either Text a
fn =
  (Bool -> Value a) -> Value a
forall a. (Bool -> Value a) -> Value a
Value ((Bool -> Value a) -> Value a) -> (Bool -> Value a) -> Value a
forall a b. (a -> b) -> a -> b
$ \Bool
integerDatetimes -> (ByteString -> Either Text a) -> Value a
forall a. (ByteString -> Either Text a) -> Value a
A.fn ((ByteString -> Either Text a) -> Value a)
-> (ByteString -> Either Text a) -> Value a
forall a b. (a -> b) -> a -> b
$ Bool -> ByteString -> Either Text a
fn Bool
integerDatetimes

-- |
-- Refine a value decoder, lifting the possible error to the session level.
{-# INLINE refine #-}
refine :: (a -> Either Text b) -> Value a -> Value b
refine :: forall a b. (a -> Either Text b) -> Value a -> Value b
refine a -> Either Text b
fn (Value Bool -> Value a
run) = (Bool -> Value b) -> Value b
forall a. (Bool -> Value a) -> Value a
Value ((a -> Either Text b) -> Value a -> Value b
forall a b. (a -> Either Text b) -> Value a -> Value b
A.refine a -> Either Text b
fn (Value a -> Value b) -> (Bool -> Value a) -> Bool -> Value b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> Value a
run)