on master help sign in
2 files changed+54−22
since splitsnapshots
unifiedsplit
id.cabal+20−5 src/Data/Id.hs+34−17
id.cabal+20−5
@@ -23,6 +23,12 @@ flag store
2323 default: False
2424 manual: False
2525
26+flag uuid-impl
27+ description: Include some functions with extra dependencies.
28+ Disable to reduce dependencies.
29+ default: True
30+ manual: False
31+
2632library
2733 exposed-modules: Data.Id
2834 build-depends:
@@ -33,17 +39,26 @@ library
3339 , hashable
3440 , http-api-data
3541 , lens
36− , openapi3
3742 , path-pieces
38− , QuickCheck
3943 , text
40− , uuid >=1.3.9
44+ , uuid-types
45+ if flag(uuid-impl)
46+ build-depends:
47+ , uuid >=1.3.9
48+ cpp-options: -DUSE_UUID
49+
50+ if !(impl(ghcjs) || arch(javascript) || arch(wasm32))
51+ build-depends:
52+ , openapi3
53+ , QuickCheck
54+
55+ cpp-options: -DBACKEND
4156
42− if (flag(postgres) && !(impl(ghcjs) || arch(javascript)))
57+ if (flag(postgres) && !(impl(ghcjs) || arch(javascript) || arch(wasm32)))
4358 build-depends: postgresql-simple
4459 cpp-options: -DUSE_POSTGRES
4560
46− if (flag(cassava) && !(impl(ghcjs) || arch(javascript)))
61+ if (flag(cassava) && !(impl(ghcjs) || arch(javascript) || arch(wasm32)))
4762 build-depends: cassava
4863 cpp-options: -DUSE_CASSAVA
4964
src/Data/Id.hs+34−17
@@ -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,19 +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 qualified Data.UUID.V4 as UUID.V4
26+import Data.UUID.Types (UUID)
27+import qualified Data.UUID.Types as UUID
2728import GHC.TypeLits
2829import Prelude as P
29−import Test.QuickCheck
3030import Text.ParserCombinators.ReadP
3131import Web.HttpApiData
3232import Web.PathPieces (PathPiece(..))
3333
34+#ifdef USE_UUID
35+import qualified Data.UUID.V4 as UUID.V4
36+#endif
37+#ifdef BACKEND
38+import Data.OpenApi
39+import Test.QuickCheck
3440#ifdef USE_CASSAVA
3541import Data.Csv as Csv hiding(Name)
3642#endif
@@ -38,6 +44,7 @@ import Data.Csv as Csv hiding(Name)
3844import Database.PostgreSQL.Simple.FromField as PG (FromField)
3945import Database.PostgreSQL.Simple.ToField as PG (ToField)
4046#endif
47+#endif
4148
4249#ifdef USE_FLAT
4350import Flat as F
@@ -55,9 +62,11 @@ newtype Id t = Id { unId :: UUID }
5562 , PG.ToField, PG.FromField
5663#endif
5764#endif
58− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
59− , ToParamSchema, FromHttpApiData, ToHttpApiData
60− )
65+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey
66+#ifdef BACKEND
67+ , ToSchema, ToParamSchema
68+#endif
69+ , FromHttpApiData, ToHttpApiData )
6170
6271type role Id nominal
6372
@@ -67,9 +76,11 @@ mkId :: forall s. UUID -> Id s
6776mkId = coerce
6877{-# INLINE mkId #-}
6978
79+#ifdef USE_UUID
7080randomId :: forall s. IO (Id s)
7181randomId = coerce <$> UUID.V4.nextRandom
7282{-# INLINE randomId #-}
83+#endif
7384
7485instance KnownSymbol s => Show (Id s) where
7586 show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> P.show v
@@ -93,12 +104,14 @@ instance Data.Store.Store (Id s) where
93104 size = Data.Store.ConstSize 16
94105#endif
95106
107+#ifdef BACKEND
96108instance Arbitrary (Id t) where
97109 arbitrary = fmap Id $ UUID.fromWords
98110 <$> arbitrary
99111 <*> arbitrary
100112 <*> arbitrary
101113 <*> arbitrary
114+#endif
102115
103116instance PathPiece (Id t) where
104117 fromPathPiece = coerce . UUID.fromText
@@ -153,10 +166,11 @@ newtype IntId t = IntId { unIntId :: Integer }
153166#ifdef USE_STORE
154167 , Data.Store.Store
155168#endif
156− , ToSchema, ToParamSchema, Arbitrary, FromJSON, ToJSON, NFData, Hashable
157− , FromJSONKey, ToJSONKey, FromHttpApiData, ToHttpApiData, PathPiece, Num
158− , Integral, Real, Enum
159− )
169+#ifdef BACKEND
170+ , ToSchema, ToParamSchema, Arbitrary
171+#endif
172+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
173+ , ToHttpApiData, PathPiece, Num, Integral, Real, Enum )
160174
161175type role IntId nominal
162176
@@ -203,10 +217,11 @@ newtype Name t = Name { unName :: Text }
203217#ifdef USE_STORE
204218 , Data.Store.Store
205219#endif
206− , ToSchema, ToParamSchema, FromJSON, ToJSON, NFData, Hashable, FromJSONKey
207− , ToJSONKey, FromHttpApiData, ToHttpApiData, PathPiece, IsString, Semigroup
208− , Monoid
209− )
220+#ifdef BACKEND
221+ , ToSchema, ToParamSchema
222+#endif
223+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
224+ , ToHttpApiData, PathPiece, IsString, Semigroup, Monoid )
210225
211226type role Name nominal
212227
@@ -224,8 +239,10 @@ instance KnownSymbol s => Read (Name s) where
224239 _ <- string ("Name-" <> symbolVal (Proxy @s) <> "-")
225240 mkName <$> readS_to_P reads
226241
242+#ifdef BACKEND
227243instance Arbitrary (Name s) where
228244 arbitrary = coerce . T.pack . getPrintableString <$> arbitrary
245+#endif
229246
230247-- | This is a more \"explicit\" 'coerce' specifically for 'Name'.
231248-- You are forced to explicitly specify the phantom types you are converting