fix: rm optional, add proxy arg

This commit is contained in:
orion 2023-12-19 16:35:29 -06:00
parent 9924a2ec59
commit c73b37b972
Signed by: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 10 additions and 27 deletions

View File

@ -5,9 +5,8 @@ import Prelude
import Control.Monad.Error.Class (liftMaybe)
import Data.Array as Array
import Data.Either (Either)
import Data.Maybe (Maybe, fromMaybe, isJust)
import Data.Maybe (fromMaybe)
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)
@ -39,35 +38,18 @@ skip :: forall a. ExtractRecord a -> ExtractRecord a
skip (ExtractRecord r a) = ExtractRecord (fromMaybe [] $ Array.tail r) a
field
:: forall a b @k @v
:: forall a b k @v
. Show (Record a)
=> Lacks k a
=> Cons k v a b
=> IsSymbol k
=> FromSQLValue v
=> ExtractRecord $ Record a
=> Proxy k
-> ExtractRecord $ Record a
-> Either String $ ExtractRecord $ Record b
field (ExtractRecord row r) = do
field _ (ExtractRecord row r) = do
let
eempty = "no more values to consume, built: " <> show r
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

View File

@ -11,6 +11,7 @@ import Effect.Exception (error)
import Foreign (unsafeToForeign)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Type.Prelude (Proxy(..))
spec :: Spec Unit
spec =
@ -21,7 +22,7 @@ spec =
it "fails when type incorrect" do
let
raw = RawRow [ unsafeToForeign 12 ]
actual = map Rec.finish $ Rec.field @"foo" @String $ Rec.start raw
actual = map Rec.finish $ Rec.field @String (Proxy @"foo") $ Rec.start raw
when (isRight actual) $ throwError $ error "should have failed"
it "succeeds" do
let
@ -36,10 +37,10 @@ spec =
liftEither
$ lmap error
$ Rec.extract
( Rec.field @"num"
>=> Rec.field @"str"
( Rec.field (Proxy @"num")
>=> Rec.field (Proxy @"str")
>=> (pure <<< Rec.skip)
>=> Rec.field @"arr"
>=> Rec.field (Proxy @"arr")
)
$ raw
actual `shouldEqual` { num: 12, str: "hello", arr: [ 1, 2, 3 ] }