generated from tpl/purs
feat: optional
This commit is contained in:
parent
80e264e7c6
commit
9924a2ec59
@ -5,8 +5,9 @@ import Prelude
|
||||
import Control.Monad.Error.Class (liftMaybe)
|
||||
import Data.Array as Array
|
||||
import Data.Either (Either)
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Data.Maybe (Maybe, fromMaybe, isJust)
|
||||
import Data.Postgres.Types (RawRow(..))
|
||||
import Data.Traversable (sequence, traverse)
|
||||
import Database.PostgreSQL (class FromSQLValue, fromSQLValue)
|
||||
import Foreign (Foreign)
|
||||
import Prim.Row (class Cons, class Lacks)
|
||||
@ -16,6 +17,15 @@ import Type.Prelude (class IsSymbol, Proxy(..))
|
||||
|
||||
data ExtractRecord a = ExtractRecord (Array Foreign) a
|
||||
|
||||
instance Functor ExtractRecord where
|
||||
map f (ExtractRecord rs a) = ExtractRecord rs (f a)
|
||||
|
||||
instance Apply ExtractRecord where
|
||||
apply (ExtractRecord _ f) (ExtractRecord rs a) = ExtractRecord rs (f a)
|
||||
|
||||
instance Applicative ExtractRecord where
|
||||
pure a = ExtractRecord [] a
|
||||
|
||||
extract :: forall a. (ExtractRecord {} -> Either String $ ExtractRecord a) -> RawRow -> Either String a
|
||||
extract f = map finish <<< f <<< start
|
||||
|
||||
@ -43,3 +53,21 @@ field (ExtractRecord row r) = do
|
||||
col <- liftMaybe eempty $ Array.head row
|
||||
val <- fromSQLValue col
|
||||
pure $ skip $ ExtractRecord row $ Record.insert (Proxy @k) val r
|
||||
|
||||
optional
|
||||
:: forall a b @k @v
|
||||
. Show (Record a)
|
||||
=> Lacks k a
|
||||
=> Cons k v a b
|
||||
=> IsSymbol k
|
||||
=> FromSQLValue (Maybe v)
|
||||
=> ExtractRecord $ Record a
|
||||
-> Either String $ ExtractRecord $ Maybe $ Record b
|
||||
optional (ExtractRecord row r) = do
|
||||
let
|
||||
col = Array.head row
|
||||
skip' = if isJust col then skip else identity
|
||||
val :: Maybe v <- map join $ traverse fromSQLValue col
|
||||
pure
|
||||
$ sequence
|
||||
$ map (\v -> skip' $ ExtractRecord row $ Record.insert (Proxy @k) v r) val
|
||||
|
Loading…
Reference in New Issue
Block a user