-
Notifications
You must be signed in to change notification settings - Fork 301
Support promoted types in Quasi Quoter #1258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3ca561
Add a test, but don't use ticked types
danbroooks 506563c
Tick those types
danbroooks 5638595
Refactor to use lambda case
danbroooks 7e141b9
Satisfy ticket
danbroooks fc01faa
Support older GHC
danbroooks 2e11e0b
Post review tweaks
danbroooks baa3848
Update changelog
danbroooks 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -349,6 +349,8 @@ mEmbedded _ (FTTypeCon Just{} _) = | |
| Left Nothing | ||
| mEmbedded ents (FTTypeCon Nothing (EntityNameHS -> name)) = | ||
| maybe (Left Nothing) Right $ M.lookup name ents | ||
| mEmbedded ents (FTTypePromoted (EntityNameHS -> name)) = | ||
| Left Nothing | ||
| mEmbedded ents (FTList x) = | ||
| mEmbedded ents x | ||
| mEmbedded ents (FTApp x y) = | ||
|
|
@@ -1758,13 +1760,21 @@ maybeNullable :: FieldDef -> Bool | |
| maybeNullable fd = nullable (fieldAttrs fd) == Nullable ByMaybeAttr | ||
|
|
||
| ftToType :: FieldType -> Type | ||
| ftToType (FTTypeCon Nothing t) = ConT $ mkName $ unpack t | ||
| -- This type is generated from the Quasi-Quoter. | ||
| -- Adding this special case avoids users needing to import Data.Int | ||
| ftToType (FTTypeCon (Just "Data.Int") "Int64") = ConT ''Int64 | ||
| ftToType (FTTypeCon (Just m) t) = ConT $ mkName $ unpack $ concat [m, ".", t] | ||
| ftToType (FTApp x y) = ftToType x `AppT` ftToType y | ||
| ftToType (FTList x) = ListT `AppT` ftToType x | ||
| ftToType = \case | ||
| FTTypeCon Nothing t -> | ||
| ConT $ mkName $ T.unpack t | ||
| -- This type is generated from the Quasi-Quoter. | ||
| -- Adding this special case avoids users needing to import Data.Int | ||
| FTTypeCon (Just "Data.Int") "Int64" -> | ||
| ConT ''Int64 | ||
| FTTypeCon (Just m) t -> | ||
| ConT $ mkName $ unpack $ concat [m, ".", t] | ||
| FTTypePromoted t -> | ||
| PromotedT $ mkName $ T.unpack t | ||
| FTApp x y -> | ||
| ftToType x `AppT` ftToType y | ||
| FTList x -> | ||
| ListT `AppT` ftToType x | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👨🏻🍳 |
||
|
|
||
| infixr 5 ++ | ||
| (++) :: Text -> Text -> Text | ||
|
|
||
This file contains hidden or 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 hidden or 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,38 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
| {-# LANGUAGE FlexibleInstances #-} | ||
| {-# LANGUAGE GADTs #-} | ||
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
| {-# LANGUAGE OverloadedLabels #-} | ||
| {-# LANGUAGE QuasiQuotes #-} | ||
| {-# LANGUAGE StandaloneDeriving #-} | ||
| {-# LANGUAGE TemplateHaskell #-} | ||
| {-# LANGUAGE UndecidableInstances #-} | ||
|
|
||
| module Database.Persist.TH.KindEntitiesSpec where | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beautiful tests <3 |
||
|
|
||
| import Database.Persist.TH.KindEntitiesSpecImports | ||
| import TemplateTestImports | ||
|
|
||
| mkPersist sqlSettings [persistLowerCase| | ||
|
|
||
| Customer | ||
| name String | ||
| age Int | ||
|
|
||
| CustomerTransfer | ||
| customerId CustomerId | ||
| moneyAmount (MoneyAmount 'CustomerOwned 'Debit) | ||
| |] | ||
|
|
||
| spec :: Spec | ||
| spec = describe "KindEntities" $ do | ||
| it "should support DataKinds in entity definition" $ do | ||
| let mkTransfer :: CustomerId -> MoneyAmount 'CustomerOwned 'Debit -> CustomerTransfer | ||
| mkTransfer = CustomerTransfer | ||
| getAmount :: CustomerTransfer -> MoneyAmount 'CustomerOwned 'Debit | ||
| getAmount = customerTransferMoneyAmount | ||
| compiles | ||
|
|
||
| compiles :: Expectation | ||
| compiles = True `shouldBe` True | ||
22 changes: 22 additions & 0 deletions
22
persistent/test/Database/Persist/TH/KindEntitiesSpecImports.hs
This file contains hidden or 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,22 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
|
|
||
| module Database.Persist.TH.KindEntitiesSpecImports where | ||
|
|
||
| import Data.Proxy | ||
| import qualified Data.Text as T | ||
| import TemplateTestImports | ||
|
|
||
| data Owner = MerchantOwned | CustomerOwned | ||
| data AccountKind = Debit | Credit | ||
|
|
||
| newtype MoneyAmount (a :: Owner) (b :: AccountKind) = MoneyAmount Rational | ||
|
|
||
| instance PersistFieldSql (MoneyAmount a b) where | ||
| sqlType _ = sqlType (Proxy :: Proxy Rational) | ||
|
|
||
| instance PersistField (MoneyAmount a b) where | ||
| toPersistValue (MoneyAmount n) = | ||
| toPersistValue n | ||
| fromPersistValue v = | ||
| MoneyAmount <$> fromPersistValue v |
This file contains hidden or 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 hidden or 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
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.
Looks great 👍🏻