@@ -4,9 +4,12 @@
44{-# OPTIONS_GHC -Wno-orphans #-}
55
66module Data.Id
7− ( Id, mkId, unId, randomId, unsafeIdTagConvert, coerceId, _Id, nilId
7+ ( Id, mkId, unId, unsafeIdTagConvert, coerceId, _Id, nilId
88 , IntId, mkIntId, unIntId, unsafeIntIdTagConvert, coerceIntId, _IntId
99 , Name, mkName, unName, unsafeNameTagConvert, coerceName, _Name
10+#ifdef USE_UUID
11+ , randomId
12+#endif
1013 )
1114 where
1215
@@ -18,13 +21,22 @@ import Data.Binary (Binary)
1821import Data.Coerce (coerce)
1922import Data.Data
2023import Data.Hashable (Hashable)
21−import Data.OpenApi
2224import Data.String
2325import Data.Text as T
24−import Data.UUID (UUID)
25−import qualified Data.UUID as UUID
26+import Data.UUID.Types (UUID)
27+import qualified Data.UUID.Types as UUID
28+import GHC.TypeLits
29+import Prelude as P
30+import Text.ParserCombinators.ReadP
31+import Web.HttpApiData
32+import Web.PathPieces (PathPiece(..))
33+
34+#ifdef USE_UUID
2635import qualified Data.UUID.V4 as UUID.V4
27−#ifndef ghcjs_HOST_OS
36+#endif
37+#ifdef BACKEND
38+import Data.OpenApi
39+import Test.QuickCheck
2840#ifdef USE_CASSAVA
2941import Data.Csv as Csv hiding(Name)
3042#endif
@@ -33,11 +45,7 @@ import Database.PostgreSQL.Simple.FromField as PG (FromField)
3345import Database.PostgreSQL.Simple.ToField as PG (ToField)
3446#endif
3547#endif
36−import GHC.TypeLits
37−import Test.QuickCheck
38−import Text.ParserCombinators.ReadP
39−import Web.HttpApiData
40−import Web.PathPieces (PathPiece(..))
48+
4149#ifdef USE_FLAT
4250import Flat as F
4351#endif
@@ -54,8 +62,11 @@ newtype Id t = Id { unId :: UUID }
5462 , PG.ToField, PG.FromField
5563#endif
5664#endif
57− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
58− , ToParamSchema, FromHttpApiData, ToHttpApiData )
65+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey
66+#ifdef BACKEND
67+ , ToSchema, ToParamSchema
68+#endif
69+ , FromHttpApiData, ToHttpApiData )
5970
6071type role Id nominal
6172
@@ -65,12 +76,14 @@ mkId :: forall s. UUID -> Id s
6576mkId = coerce
6677{-# INLINE mkId #-}
6778
79+#ifdef USE_UUID
6880randomId :: forall s. IO (Id s)
6981randomId = coerce <$> UUID.V4.nextRandom
7082{-# INLINE randomId #-}
83+#endif
7184
7285instance KnownSymbol s => Show (Id s) where
73− show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> show v
86+ show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> P.show v
7487
7588instance KnownSymbol s => Read (Id s) where
7689 readsPrec _ = readP_to_S $ do
@@ -91,12 +104,14 @@ instance Data.Store.Store (Id s) where
91104 size = Data.Store.ConstSize 16
92105#endif
93106
107+#ifdef BACKEND
94108instance Arbitrary (Id t) where
95109 arbitrary = fmap Id $ UUID.fromWords
96110 <$> arbitrary
97111 <*> arbitrary
98112 <*> arbitrary
99113 <*> arbitrary
114+#endif
100115
101116instance PathPiece (Id t) where
102117 fromPathPiece = coerce . UUID.fromText
@@ -145,9 +160,17 @@ newtype IntId t = IntId { unIntId :: Integer }
145160 , Csv.ToField, Csv.FromField
146161#endif
147162#endif
148− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
149− , ToParamSchema, FromHttpApiData, ToHttpApiData, PathPiece, Flat, Arbitrary
150− , Num, Integral, Real, Enum )
163+#ifdef USE_FLAT
164+ , Flat
165+#endif
166+#ifdef USE_STORE
167+ , Data.Store.Store
168+#endif
169+#ifdef BACKEND
170+ , ToSchema, ToParamSchema, Arbitrary
171+#endif
172+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
173+ , ToHttpApiData, PathPiece, Num, Integral, Real, Enum )
151174
152175type role IntId nominal
153176
@@ -158,7 +181,7 @@ mkIntId = coerce
158181{-# INLINE mkIntId #-}
159182
160183instance KnownSymbol s => Show (IntId s) where
161− show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> show v
184+ show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> P.show v
162185
163186instance KnownSymbol s => Read (IntId s) where
164187 readsPrec _ = readP_to_S $ do
@@ -188,9 +211,17 @@ newtype Name t = Name { unName :: Text }
188211 , Csv.ToField, Csv.FromField
189212#endif
190213#endif
191− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
192− , ToParamSchema, FromHttpApiData, ToHttpApiData, PathPiece, Flat
193− , IsString, Semigroup, Monoid )
214+#ifdef USE_FLAT
215+ , Flat
216+#endif
217+#ifdef USE_STORE
218+ , Data.Store.Store
219+#endif
220+#ifdef BACKEND
221+ , ToSchema, ToParamSchema
222+#endif
223+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
224+ , ToHttpApiData, PathPiece, IsString, Semigroup, Monoid )
194225
195226type role Name nominal
196227
@@ -201,15 +232,17 @@ mkName = coerce
201232{-# INLINE mkName #-}
202233
203234instance KnownSymbol s => Show (Name s) where
204− show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> show v
235+ show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> P.show v
205236
206237instance KnownSymbol s => Read (Name s) where
207238 readsPrec _ = readP_to_S $ do
208239 _ <- string ("Name-" <> symbolVal (Proxy @s) <> "-")
209240 mkName <$> readS_to_P reads
210241
242+#ifdef BACKEND
211243instance Arbitrary (Name s) where
212244 arbitrary = coerce . T.pack . getPrintableString <$> arbitrary
245+#endif
213246
214247-- | This is a more \"explicit\" 'coerce' specifically for 'Name'.
215248-- You are forced to explicitly specify the phantom types you are converting