fix: pin unresult api to rept

This commit is contained in:
orion 2024-04-05 22:14:59 -05:00
parent 43ce1354ea
commit de1aaccfb6
Signed by: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 11 additions and 12 deletions

View File

@ -8,13 +8,13 @@ import Control.Monad.Error.Class (class MonadError, class MonadThrow)
import Control.Monad.Fork.Class (class MonadBracket, class MonadFork, class MonadKill, bracket, kill, never, uninterruptible)
import Control.Monad.Postgres.Base (PostgresT, transaction)
import Control.Monad.Postgres.Session (class MonadSession, SessionT, exec, exec_, query)
import Control.Monad.Reader (class MonadAsk, class MonadReader, ReaderT, ask, local, mapReader, runReaderT, withReader, withReaderT)
import Control.Monad.Reader (class MonadAsk, class MonadReader, ReaderT, ask, local, runReaderT)
import Control.Monad.Trans.Class (class MonadTrans, lift)
import Control.Parallel (class Parallel, parallel, sequential)
import Data.Array as Array
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype, unwrap, wrap)
import Data.Postgres (class Deserialize, RepT, deserialize, smash)
import Data.Postgres (RepT, smash)
import Data.Postgres.Query (class AsQuery, asQuery)
import Data.Postgres.Raw (Raw)
import Data.Postgres.Result (class FromRow, fromRow)

View File

@ -2,27 +2,26 @@ module Data.Postgres.Unresult where
import Prelude
import Control.Monad.Error.Class (class MonadThrow, liftMaybe)
import Control.Monad.Error.Class (liftMaybe)
import Control.Monad.State (StateT, runStateT, state)
import Data.Array as Array
import Data.Postgres (class Deserialize, deserialize, smash)
import Data.Postgres (class Deserialize, RepT, deserialize, smash)
import Data.Postgres.Raw (Raw)
import Data.Tuple (fst)
import Data.Tuple.Nested ((/\))
import Effect.Aff (error)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Exception (Error)
import Effect.Class (liftEffect)
import Foreign (ForeignError(..))
-- | Monad used to incrementally deserialize columns from a row
type UnresultT m a = StateT {ix :: Int, row :: Array Raw} m a
type Unresult a = StateT {ix :: Int, row :: Array Raw} RepT a
-- | Run an `UnresultT`
unresult :: forall m a. Monad m => Array Raw -> UnresultT m a -> m a
unresult row m = fst <$> runStateT m {ix: 0, row}
unresult :: forall a. Unresult a -> Array Raw -> RepT a
unresult m row = fst <$> runStateT m {ix: 0, row}
-- | Take the next column from the row, unmarshalling into `a`
take :: forall m a. MonadThrow Error m => Deserialize a => MonadEffect m => UnresultT m a
take :: forall a. Deserialize a => Unresult a
take = do
raw <- state (\r -> Array.index r.row r.ix /\ r {ix = r.ix + 1})
raw' <- liftMaybe (error "Ran out of columns!") raw
raw' <- liftMaybe (pure $ ForeignError $ "Ran out of columns!") raw
liftEffect $ smash $ deserialize raw'