Compare commits

...

2 Commits

Author SHA1 Message Date
5a36ec328f Use cabal2nix for flake expression 2024-06-25 22:30:15 +02:00
19a1aa26e4 Minor fixes 2024-06-20 20:10:51 +02:00
3 changed files with 25 additions and 30 deletions

View File

@ -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

View File

@ -3,33 +3,24 @@
outputs = { self, nixpkgs }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
opium = pkgs.haskellPackages.developPackage {
root = ./.;
modifier = drv:
pkgs.haskell.lib.addBuildTools drv [
pkgs.cabal-install
pkgs.haskellPackages.implicit-hie
pkgs.haskell-language-server
];
};
in {
apps.x86_64-linux.cabal = {
type = "app";
program = "${nixpkgs.legacyPackages.x86_64-linux.cabal-install}/bin/cabal";
packages.${system}.opium = pkgs.haskell.lib.overrideCabal opium {
# Currently the tests require a running Postgres instance.
# This is not automated yet, so don't export the tests.
doCheck = false;
};
devShells.x86_64-linux.default = pkgs.mkShell {
packages = [
pkgs.cabal-install
pkgs.haskellPackages.implicit-hie
(pkgs.ghc.withPackages (hp: with hp; [
attoparsec
containers
bytestring
hspec
postgresql-libpq
text
time
transformers
vector
]))
pkgs.haskell-language-server
];
shellHook = ''
PS1="<opium> ''${PS1}"
'';
};
devShells.${system}.default = opium.env;
};
}

View File

@ -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)