-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Type Literal based field definitions #1343
Merged
parsonsmatt
merged 11 commits into
yesodweb:matt/finalize-type-lit-pr
from
danbroooks:persist-entity-type-lits
Apr 12, 2022
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3a10b80
Implement typelit field definitions
danbroooks 8f46fd8
Some cleanup
danbroooks e0eeaa7
Sort indentation and add PR link in changelog
danbroooks 06e93e7
Merge branch 'master' into persist-entity-type-lits
danbroooks 929bc97
Move location of exposed type
danbroooks cc95228
Merge branch 'master' into persist-entity-type-lits
danbroooks 1ddbc3b
Merge branch 'master' into persist-entity-type-lits
danbroooks b5d8cfb
Merge branch 'master' into persist-entity-type-lits
parsonsmatt 4986a51
Merge branch 'master' into persist-entity-type-lits
parsonsmatt 18ed3e8
Merge branch 'master' into persist-entity-type-lits
parsonsmatt dc436da
Merge branch 'master' into persist-entity-type-lits
parsonsmatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice - this is great. Moving away from ad-hoc text munging and instead using more idiomatic Haskell classes.