-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Type Literal based field definitions (#1343)
* Implement typelit field definitions * Some cleanup * Sort indentation and add PR link in changelog * Move location of exposed type Co-authored-by: Matt Parsons <parsonsmatt@gmail.com>
- Loading branch information
1 parent
b179809
commit c3886bf
Showing
15 changed files
with
255 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
{-# OPTIONS_GHC -Wno-unused-top-binds #-} | ||
|
||
module TypeLitFieldDefsTest (specsWith, typeLitFieldDefsMigrate) where | ||
|
||
import Data.Maybe (fromJust) | ||
import GHC.TypeLits | ||
import Init | ||
|
||
newtype Finite (n :: Nat) = Finite Int | ||
deriving (Show, Eq) | ||
|
||
instance PersistField (Finite n) where | ||
toPersistValue (Finite n) = toPersistValue n | ||
fromPersistValue = fmap Finite . fromPersistValue | ||
|
||
instance PersistFieldSql (Finite n) where | ||
sqlType _ = sqlType (Proxy :: Proxy Int) | ||
|
||
newtype Labelled (t :: Symbol) = Labelled Int | ||
deriving (Show, Eq) | ||
|
||
instance PersistField (Labelled n) where | ||
toPersistValue (Labelled n) = toPersistValue n | ||
fromPersistValue = fmap Labelled . fromPersistValue | ||
|
||
instance PersistFieldSql (Labelled n) where | ||
sqlType _ = sqlType (Proxy :: Proxy Int) | ||
|
||
share [mkPersist sqlSettings { mpsGeneric = True }, mkMigrate "typeLitFieldDefsMigrate"] [persistLowerCase| | ||
TypeLitFieldDefsNumeric | ||
one (Finite 1) | ||
twenty (Finite 20) | ||
deriving Eq Show | ||
|
||
TypeLitFieldDefsLabelled | ||
one (Labelled "one") | ||
twenty (Labelled "twenty") | ||
deriving Eq Show | ||
|] | ||
|
||
one :: Finite 1 | ||
one = Finite 1 | ||
|
||
oneLabelled :: Labelled "one" | ||
oneLabelled = Labelled 1 | ||
|
||
twenty :: Finite 20 | ||
twenty = Finite 20 | ||
|
||
twentyLabelled :: Labelled "twenty" | ||
twentyLabelled = Labelled 20 | ||
|
||
specsWith :: Runner backend m => RunDb backend m -> Spec | ||
specsWith runDb = | ||
describe "Type Lit Field Definitions" $ do | ||
it "runs appropriate migrations" $ runDb $ do | ||
numKey <- insert $ TypeLitFieldDefsNumeric one twenty | ||
num <- getJust numKey | ||
liftIO $ typeLitFieldDefsNumericOne num @?= one | ||
liftIO $ typeLitFieldDefsNumericTwenty num @?= twenty | ||
|
||
labelledKey <- insert $ TypeLitFieldDefsLabelled oneLabelled twentyLabelled | ||
lbl <- getJust labelledKey | ||
liftIO $ typeLitFieldDefsLabelledOne lbl @?= oneLabelled | ||
liftIO $ typeLitFieldDefsLabelledTwenty lbl @?= twentyLabelled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.