Skip to content

Commit

Permalink
Fix type error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
parsonsmatt committed Apr 29, 2024
1 parent 9701360 commit 66bd932
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions persistent/Database/Persist/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,9 +1571,13 @@ mkToFieldNames pairs = do
names'

mkUniqueToValues :: [UniqueDef] -> Q Dec
mkUniqueToValues pairs = do
pairs' <- mapM go pairs
return $ FunD 'persistUniqueToValues $ degen pairs'
mkUniqueToValues uniqs =
case uniqs of
[] ->
pure $ FunD 'persistUniqueToValues $ degen []
pairs -> do
pairs' <- mapM go pairs
return $ FunD 'persistUniqueToValues pairs'
where
go :: UniqueDef -> Q Clause
go (UniqueDef constr _ names _) = do
Expand Down Expand Up @@ -2262,14 +2266,12 @@ mkUniqueKeyInstances :: MkPersistSettings -> UnboundEntityDef -> Q [Dec]
mkUniqueKeyInstances mps entDef = do
requirePersistentExtensions
case entityUniques (unboundEntityDef entDef) of
[] -> mappend <$> typeErrorSingle <*> typeErrorAtLeastOne
[] -> mappend <$> mkOnlyUniqueError typeErrorNoneCtx <*> mkAtLeastOneUniqueError typeErrorNoneCtx
[_] -> mappend <$> singleUniqueKey <*> atLeastOneKey
(_:_) -> mappend <$> typeErrorMultiple <*> atLeastOneKey
(_:_) -> mappend <$> mkOnlyUniqueError typeErrorMultipleCtx <*> atLeastOneKey
where
requireUniquesPName = 'requireUniquesP
onlyUniquePName = 'onlyUniqueP
typeErrorSingle = mkOnlyUniqueError typeErrorNoneCtx
typeErrorMultiple = mkOnlyUniqueError typeErrorMultipleCtx

withPersistStoreWriteCxt =
if mpsGeneric mps
Expand All @@ -2293,6 +2295,12 @@ mkUniqueKeyInstances mps entDef = do
let impl = mkImpossible onlyUniquePName
pure [instanceD ctx onlyOneUniqueKeyClass impl]

mkAtLeastOneUniqueError :: Q Cxt -> Q [Dec]
mkAtLeastOneUniqueError mkCtx = do
let impl = mkImpossible requireUniquesPName
cxt <- mkCtx
pure [instanceD cxt atLeastOneUniqueKeyClass impl]

mkImpossible name =
[ FunD name
[ Clause
Expand All @@ -2304,12 +2312,6 @@ mkUniqueKeyInstances mps entDef = do
]
]

typeErrorAtLeastOne :: Q [Dec]
typeErrorAtLeastOne = do
let impl = mkImpossible requireUniquesPName
cxt <- typeErrorMultipleCtx
pure [instanceD cxt atLeastOneUniqueKeyClass impl]

singleUniqueKey :: Q [Dec]
singleUniqueKey = do
expr <- [e| head . persistUniqueKeys|]
Expand Down

0 comments on commit 66bd932

Please sign in to comment.