From 19a1aa26e48dedf898d87f4844bf80d138cfd574 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Thu, 20 Jun 2024 20:10:51 +0200 Subject: [PATCH] Minor fixes --- README.md | 1 + lib/Database/PostgreSQL/Opium/FromRow.hs | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9b07346..a5518db 100644 --- a/README.md +++ b/README.md @@ -83,3 +83,4 @@ getScoreByAge conn = do - [ ] `FromRow` - [ ] Custom `FromField` impls - [ ] Improve type errors when trying to `instance` a type that isn't a record (e.g. sum type) +- [ ] Improve documentation for `fromRow` module diff --git a/lib/Database/PostgreSQL/Opium/FromRow.hs b/lib/Database/PostgreSQL/Opium/FromRow.hs index 91fd3ae..de5a4e6 100644 --- a/lib/Database/PostgreSQL/Opium/FromRow.hs +++ b/lib/Database/PostgreSQL/Opium/FromRow.hs @@ -89,7 +89,7 @@ type family NumberOfMembers f where NumberOfMembers (M1 D _ f) = NumberOfMembers f -- The constructor has as many members as the type that it contains. NumberOfMembers (M1 C _ f) = NumberOfMembers f - -- A product type has as many types as its subtypes have together. + -- A product type has as many members as its subtypes have together. NumberOfMembers (f :*: g) = NumberOfMembers f + NumberOfMembers g -- A selector has/is exactly one member. NumberOfMembers (M1 S _ f) = 1 @@ -99,19 +99,23 @@ data FromRowCtx = FromRowCtx Result -- ^ Obtained from 'LibPQ.execParams'. ColumnTable -- ^ 'Vector' of expected columns indices and OIDs. +-- Specialized proxy type to be used instead of `Proxy (n, f)` data FRProxy (n :: Nat) (f :: Type -> Type) = FRProxy class FromRow' (n :: Nat) (f :: Type -> Type) where fromRow' :: FRProxy n f -> FromRowCtx -> Row -> ExceptT Error IO (f p) instance FromRow' n f => FromRow' n (M1 D c f) where - fromRow' FRProxy ctx row = M1 <$> fromRow' @n FRProxy ctx row + fromRow' FRProxy ctx row = + M1 <$> fromRow' @n FRProxy ctx row instance FromRow' n f => FromRow' n (M1 C c f) where - fromRow' FRProxy ctx row = M1 <$> fromRow' @n FRProxy ctx row + fromRow' FRProxy ctx row = + M1 <$> fromRow' @n FRProxy ctx row instance (FromRow' n f, FromRow' (n + NumberOfMembers f) g) => FromRow' n (f :*: g) where - fromRow' FRProxy ctx row = (:*:) <$> fromRow' @n FRProxy ctx row <*> fromRow' @(n + NumberOfMembers f) FRProxy ctx row + fromRow' FRProxy ctx row = + (:*:) <$> fromRow' @n FRProxy ctx row <*> fromRow' @(n + NumberOfMembers f) FRProxy ctx row instance {-# OVERLAPPABLE #-} (KnownNat n, KnownSymbol nameSym, FromField t) => FromRow' n (M1 S ('MetaSel ('Just nameSym) nu ns dl) (Rec0 t)) where fromRow' FRProxy = decodeField memberIndex nameText $ \row -> @@ -158,4 +162,3 @@ decodeField memberIndex nameText g (FromRowCtx result columnTable) row = do first (ErrorInvalidField (ErrorPosition row nameText) oid field) (Just <$> fromField field) -