@@ -1,11 +1,14 @@
1+{-# LANGUAGE TemplateHaskell #-}
1{-# OPTIONS_GHC -Wno-orphans #-}
2{-# OPTIONS_GHC -Wno-orphans #-}
3module Data.Id
4module Data.Id
8import Control.DeepSeq (NFData)
10import Control.DeepSeq (NFData)
9import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
12import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
10import Data.Binary (Binary)
13import Data.Binary (Binary)
11import Data.Coerce (coerce)
14import Data.Coerce (coerce)
@@ -15,22 +18,32 @@ import qualified Data.UUID as UUID
15import Database.PostgreSQL.Simple.FromField (FromField)
18import Database.PostgreSQL.Simple.FromField (FromField)
16import Database.PostgreSQL.Simple.ToField (ToField)
19import Database.PostgreSQL.Simple.ToField (ToField)
17import GHC.Generics (Generic)
20import GHC.Generics (Generic)
21+import Test.QuickCheck
18import Yesod.Core (PathPiece(..))
22import Yesod.Core (PathPiece(..))
21−newtype Id (t :: k) = Id { unId :: UUID }
25+newtype Id t = Id { unId :: UUID }
23 ( Eq, Ord, Generic, Read, Show, ToField, FromField, PathPiece, FromJSON
27 ( Eq, Ord, Generic, Read, Show, ToField, FromField, PathPiece, FromJSON
24 , ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey)
28 , ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey)
26type role Id nominal
30type role Id nominal
32+instance Arbitrary (Id t) where
33+ arbitrary = fmap Id $ UUID.fromWords
28instance Binary (Id t) where
39instance Binary (Id t) where
30instance PathPiece UUID.UUID where
41instance PathPiece UUID.UUID where
31 fromPathPiece = UUID.fromText
42 fromPathPiece = UUID.fromText
32 toPathPiece = UUID.toText
43 toPathPiece = UUID.toText
34-- | This is a more \"explicit\" 'coerce' specifically for 'Id'.
47-- | This is a more \"explicit\" 'coerce' specifically for 'Id'.
35-- You are forced to explicitly specify the phantom types you are converting
48-- You are forced to explicitly specify the phantom types you are converting
36-- via the @TypeApplications@ compiler extension.
49-- via the @TypeApplications@ compiler extension.