generated from tpl/purs
fix: rm optional, add proxy arg
This commit is contained in:
parent
9924a2ec59
commit
c73b37b972
@ -5,9 +5,8 @@ import Prelude
|
|||||||
import Control.Monad.Error.Class (liftMaybe)
|
import Control.Monad.Error.Class (liftMaybe)
|
||||||
import Data.Array as Array
|
import Data.Array as Array
|
||||||
import Data.Either (Either)
|
import Data.Either (Either)
|
||||||
import Data.Maybe (Maybe, fromMaybe, isJust)
|
import Data.Maybe (fromMaybe)
|
||||||
import Data.Postgres.Types (RawRow(..))
|
import Data.Postgres.Types (RawRow(..))
|
||||||
import Data.Traversable (sequence, traverse)
|
|
||||||
import Database.PostgreSQL (class FromSQLValue, fromSQLValue)
|
import Database.PostgreSQL (class FromSQLValue, fromSQLValue)
|
||||||
import Foreign (Foreign)
|
import Foreign (Foreign)
|
||||||
import Prim.Row (class Cons, class Lacks)
|
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
|
skip (ExtractRecord r a) = ExtractRecord (fromMaybe [] $ Array.tail r) a
|
||||||
|
|
||||||
field
|
field
|
||||||
:: forall a b @k @v
|
:: forall a b k @v
|
||||||
. Show (Record a)
|
. Show (Record a)
|
||||||
=> Lacks k a
|
=> Lacks k a
|
||||||
=> Cons k v a b
|
=> Cons k v a b
|
||||||
=> IsSymbol k
|
=> IsSymbol k
|
||||||
=> FromSQLValue v
|
=> FromSQLValue v
|
||||||
=> ExtractRecord $ Record a
|
=> Proxy k
|
||||||
|
-> ExtractRecord $ Record a
|
||||||
-> Either String $ ExtractRecord $ Record b
|
-> Either String $ ExtractRecord $ Record b
|
||||||
field (ExtractRecord row r) = do
|
field _ (ExtractRecord row r) = do
|
||||||
let
|
let
|
||||||
eempty = "no more values to consume, built: " <> show r
|
eempty = "no more values to consume, built: " <> show r
|
||||||
col <- liftMaybe eempty $ Array.head row
|
col <- liftMaybe eempty $ Array.head row
|
||||||
val <- fromSQLValue col
|
val <- fromSQLValue col
|
||||||
pure $ skip $ ExtractRecord row $ Record.insert (Proxy @k) val r
|
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
|
|
||||||
|
@ -11,6 +11,7 @@ import Effect.Exception (error)
|
|||||||
import Foreign (unsafeToForeign)
|
import Foreign (unsafeToForeign)
|
||||||
import Test.Spec (Spec, describe, it)
|
import Test.Spec (Spec, describe, it)
|
||||||
import Test.Spec.Assertions (shouldEqual)
|
import Test.Spec.Assertions (shouldEqual)
|
||||||
|
import Type.Prelude (Proxy(..))
|
||||||
|
|
||||||
spec :: Spec Unit
|
spec :: Spec Unit
|
||||||
spec =
|
spec =
|
||||||
@ -21,7 +22,7 @@ spec =
|
|||||||
it "fails when type incorrect" do
|
it "fails when type incorrect" do
|
||||||
let
|
let
|
||||||
raw = RawRow [ unsafeToForeign 12 ]
|
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"
|
when (isRight actual) $ throwError $ error "should have failed"
|
||||||
it "succeeds" do
|
it "succeeds" do
|
||||||
let
|
let
|
||||||
@ -36,10 +37,10 @@ spec =
|
|||||||
liftEither
|
liftEither
|
||||||
$ lmap error
|
$ lmap error
|
||||||
$ Rec.extract
|
$ Rec.extract
|
||||||
( Rec.field @"num"
|
( Rec.field (Proxy @"num")
|
||||||
>=> Rec.field @"str"
|
>=> Rec.field (Proxy @"str")
|
||||||
>=> (pure <<< Rec.skip)
|
>=> (pure <<< Rec.skip)
|
||||||
>=> Rec.field @"arr"
|
>=> Rec.field (Proxy @"arr")
|
||||||
)
|
)
|
||||||
$ raw
|
$ raw
|
||||||
actual `shouldEqual` { num: 12, str: "hello", arr: [ 1, 2, 3 ] }
|
actual `shouldEqual` { num: 12, str: "hello", arr: [ 1, 2, 3 ] }
|
||||||
|
Loading…
Reference in New Issue
Block a user