diff --git a/src/Data.Postgres.Extract.Record.purs b/src/Data.Postgres.Extract.Record.purs index 82555b2..98b69ee 100644 --- a/src/Data.Postgres.Extract.Record.purs +++ b/src/Data.Postgres.Extract.Record.purs @@ -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 diff --git a/test/Spec.Data.Postgres.Record.purs b/test/Spec.Data.Postgres.Record.purs index f4911f6..2704bb3 100644 --- a/test/Spec.Data.Postgres.Record.purs +++ b/test/Spec.Data.Postgres.Record.purs @@ -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 ] }