on master help sign in
4 files changed+119−145
since splitsnapshots
unifiedsplit
cabal.project+11 id.cabal+61−74 src/Data/Id.hs+46−58 stack.yaml+1−13
cabal.projectnew file+11
@@ -0,0 +1,11 @@
1+packages:
2+ .
3+
4+-- To compile with js-backend use this:
5+source-repository-package
6+ type: git
7+ location: https://github.com/typeable/uuid.git
8+ tag: 49f50ae517de0e4d5dfbf783edc61e71cfd54d6b
9+ subdir:
10+ uuid
11+ uuid-types
id.cabal+61−74
@@ -1,90 +1,77 @@
1−cabal-version: 2.4
1+cabal-version: 2.4
2+name: id
3+version: 0.2.0.0
4+license-file: LICENSE
25
3−name: id
4−version: 0.2.0.0
5−license-file: LICENSE
6+flag cassava
7+ description: Enable Postgresql support
8+ default: True
9+ manual: False
610
7−Flag cassava
8− Description: Enable Cassava support
9− Default: True
10− Manual: False
11+flag postgres
12+ description: Enable Postgresql support
13+ default: True
14+ manual: False
1115
12−Flag postgres
13− Description: Enable Postgresql support
14− Default: True
15− Manual: False
16+flag flat
17+ description: Enable Flat serialization support
18+ default: True
19+ manual: False
1620
17−Flag rel8
18− Description: Enable Rel8 support
19− Default: False
20− Manual: False
21+flag store
22+ description: Enable Data.Store serialization support
23+ default: False
24+ manual: False
2125
22−Flag flat
23− Description: Enable Flat serialization support
24− Default: True
25− Manual: False
26−
27−Flag store
28− Description: Enable Data.Store serialization support
29− Default: False
30− Manual: False
31−
32−Flag openapi
33− Description: Enable OpenAPI instances
34− Default: True
35− Manual: False
36−
37−Flag lens
38− Description: Generate lenses
39− Default: True
40− Manual: False
26+flag uuid-impl
27+ description: Include some functions with extra dependencies.
28+ Disable to reduce dependencies.
29+ default: True
30+ manual: False
4131
4232library
43− exposed-modules:
44− Data.Id
45− -- other-modules:
46− -- other-extensions:
33+ exposed-modules: Data.Id
4734 build-depends:
48− aeson,
49− base >=4.12 && <5,
50− binary,
51− deepseq,
52− hashable,
53− http-api-data,
54− path-pieces,
55− text,
56− QuickCheck,
57− uuid,
58− if flag(postgres) && !impl(ghcjs)
35+ , aeson >=1.0
36+ , base >=4.12 && <5
37+ , binary
38+ , deepseq
39+ , hashable
40+ , http-api-data
41+ , lens
42+ , path-pieces
43+ , text
44+ , uuid-types
45+ if flag(uuid-impl)
5946 build-depends:
60− postgresql-simple
61− cpp-options: -DUSE_POSTGRES
62− if flag(rel8) && !impl(ghcjs)
63− build-depends: rel8
64− cpp-options: -DUSE_REL8
65− if flag(cassava) && !impl(ghcjs)
47+ , uuid >=1.3.9
48+ cpp-options: -DUSE_UUID
49+
50+ if !(impl(ghcjs) || arch(javascript) || arch(wasm32))
6651 build-depends:
67− cassava
68− cpp-options: -DUSE_CASSAVA
52+ , openapi3
53+ , QuickCheck
54+
55+ cpp-options: -DBACKEND
56+
57+ if (flag(postgres) && !(impl(ghcjs) || arch(javascript) || arch(wasm32)))
58+ build-depends: postgresql-simple
59+ cpp-options: -DUSE_POSTGRES
60+
61+ if (flag(cassava) && !(impl(ghcjs) || arch(javascript) || arch(wasm32)))
62+ build-depends: cassava
63+ cpp-options: -DUSE_CASSAVA
64+
6965 if flag(flat)
70− build-depends:
71− flat,
72− cpp-options: -DUSE_FLAT
66+ build-depends: flat >=0.4
67+ cpp-options: -DUSE_FLAT
68+
7369 if flag(store)
74− build-depends:
75− store,
76− cpp-options: -DUSE_STORE
77− if flag(lens)
78− build-depends: lens
79− cpp-options: -DUSE_LENS
80− if flag(openapi)
81− build-depends: openapi3
82− cpp-options: -DUSE_OPENAPI
70+ build-depends: store
71+ cpp-options: -DUSE_STORE
8372
84− hs-source-dirs:
85− src
86− default-language:
87− Haskell2010
73+ hs-source-dirs: src
74+ default-language: Haskell2010
8875 default-extensions:
8976 AllowAmbiguousTypes
9077 DeriveGeneric
src/Data/Id.hs+46−58
@@ -4,35 +4,39 @@
44{-# OPTIONS_GHC -Wno-orphans #-}
55
66module Data.Id
7− ( Id, mkId, unId, randomId, unsafeIdTagConvert, coerceId, nilId
8− , IntId, mkIntId, unIntId, unsafeIntIdTagConvert, coerceIntId
9− , Name, mkName, unName, unsafeNameTagConvert, coerceName
10−#ifdef USE_LENS
11− , _Id, _IntId, _Name
7+ ( Id, mkId, unId, unsafeIdTagConvert, coerceId, _Id, nilId
8+ , IntId, mkIntId, unIntId, unsafeIntIdTagConvert, coerceIntId, _IntId
9+ , Name, mkName, unName, unsafeNameTagConvert, coerceName, _Name
10+#ifdef USE_UUID
11+ , randomId
1212#endif
1313 )
1414 where
1515
1616import Control.DeepSeq (NFData)
17−#ifdef USE_LENS
1817import Control.Lens
19−#endif
2018import Control.Monad
2119import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
2220import Data.Binary (Binary)
2321import Data.Coerce (coerce)
2422import Data.Data
2523import Data.Hashable (Hashable)
26−import Data.Int (Int64)
27−#ifdef USE_OPENAPI
28−import Data.OpenApi
29−#endif
3024import Data.String
3125import Data.Text as T
32−import Data.UUID (UUID)
33−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
3435import qualified Data.UUID.V4 as UUID.V4
35−#ifndef ghcjs_HOST_OS
36+#endif
37+#ifdef BACKEND
38+import Data.OpenApi
39+import Test.QuickCheck
3640#ifdef USE_CASSAVA
3741import Data.Csv as Csv hiding(Name)
3842#endif
@@ -41,20 +45,13 @@ import Database.PostgreSQL.Simple.FromField as PG (FromField)
4145import Database.PostgreSQL.Simple.ToField as PG (ToField)
4246#endif
4347#endif
44−import GHC.TypeLits
45−import Test.QuickCheck
46−import Text.ParserCombinators.ReadP
47−import Web.HttpApiData
48−import Web.PathPieces (PathPiece(..))
48+
4949#ifdef USE_FLAT
5050import Flat as F
5151#endif
5252#ifdef USE_STORE
5353import qualified Data.Store
5454#endif
55−#ifdef USE_REL8
56−import Rel8 (DBType(..))
57−#endif
5855
5956newtype Id t = Id { unId :: UUID }
6057 deriving stock Data
@@ -64,28 +61,29 @@ newtype Id t = Id { unId :: UUID }
6461#ifdef USE_POSTGRES
6562 , PG.ToField, PG.FromField
6663#endif
67−#ifdef USE_REL8
68− , DBType
69−#endif
7064#endif
7165 , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey
72−#ifdef USE_OPENAPI
66+#ifdef BACKEND
7367 , ToSchema, ToParamSchema
7468#endif
7569 , FromHttpApiData, ToHttpApiData )
7670
7771type role Id nominal
7872
73+makePrisms ''Id
74+
7975mkId :: forall s. UUID -> Id s
8076mkId = coerce
8177{-# INLINE mkId #-}
8278
79+#ifdef USE_UUID
8380randomId :: forall s. IO (Id s)
8481randomId = coerce <$> UUID.V4.nextRandom
8582{-# INLINE randomId #-}
83+#endif
8684
8785instance KnownSymbol s => Show (Id s) where
88− show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> show v
86+ show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> P.show v
8987
9088instance KnownSymbol s => Read (Id s) where
9189 readsPrec _ = readP_to_S $ do
@@ -106,12 +104,14 @@ instance Data.Store.Store (Id s) where
106104 size = Data.Store.ConstSize 16
107105#endif
108106
107+#ifdef BACKEND
109108instance Arbitrary (Id t) where
110109 arbitrary = fmap Id $ UUID.fromWords
111110 <$> arbitrary
112111 <*> arbitrary
113112 <*> arbitrary
114113 <*> arbitrary
114+#endif
115115
116116instance PathPiece (Id t) where
117117 fromPathPiece = coerce . UUID.fromText
@@ -148,7 +148,7 @@ type family Ambiguous (a :: k) :: j where
148148 Ambiguous x = x
149149
150150----------------- IntId
151−newtype IntId t = IntId Int64
151+newtype IntId t = IntId { unIntId :: Integer }
152152 deriving stock Data
153153 deriving newtype
154154 ( Eq, Ord, Binary
@@ -159,35 +159,29 @@ newtype IntId t = IntId Int64
159159#ifdef USE_CASSAVA
160160 , Csv.ToField, Csv.FromField
161161#endif
162+#endif
162163#ifdef USE_FLAT
163164 , Flat
164165#endif
165166#ifdef USE_STORE
166167 , Data.Store.Store
167168#endif
168−#ifdef USE_REL8
169− , DBType
169+#ifdef BACKEND
170+ , ToSchema, ToParamSchema, Arbitrary
170171#endif
171−#endif
172− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey
173−#ifdef USE_OPENAPI
174− , ToSchema, ToParamSchema
175−#endif
176− , FromHttpApiData, ToHttpApiData, PathPiece, Arbitrary
177− , Num, Integral, Real, Enum )
172+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
173+ , ToHttpApiData, PathPiece, Num, Integral, Real, Enum )
178174
179175type role IntId nominal
180176
177+makePrisms ''IntId
178+
181179mkIntId :: forall s. Integer -> IntId s
182−mkIntId = fromInteger
180+mkIntId = coerce
183181{-# INLINE mkIntId #-}
184182
185−unIntId :: IntId s -> Integer
186−unIntId (IntId i) = fromIntegral i
187−{-# INLINE unIntId #-}
188−
189183instance KnownSymbol s => Show (IntId s) where
190− show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> show v
184+ show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> P.show v
191185
192186instance KnownSymbol s => Read (IntId s) where
193187 readsPrec _ = readP_to_S $ do
@@ -216,39 +210,39 @@ newtype Name t = Name { unName :: Text }
216210#ifdef USE_CASSAVA
217211 , Csv.ToField, Csv.FromField
218212#endif
213+#endif
219214#ifdef USE_FLAT
220215 , Flat
221216#endif
222217#ifdef USE_STORE
223218 , Data.Store.Store
224219#endif
225−#ifdef USE_REL8
226− , DBType
227−#endif
228−#endif
229− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey
230−#ifdef USE_OPENAPI
220+#ifdef BACKEND
231221 , ToSchema, ToParamSchema
232222#endif
233− , FromHttpApiData, ToHttpApiData, PathPiece
234− , IsString, Semigroup, Monoid )
223+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
224+ , ToHttpApiData, PathPiece, IsString, Semigroup, Monoid )
235225
236226type role Name nominal
237227
228+makePrisms ''Name
229+
238230mkName :: forall s. Text -> Name s
239231mkName = coerce
240232{-# INLINE mkName #-}
241233
242234instance KnownSymbol s => Show (Name s) where
243− show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> show v
235+ show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> P.show v
244236
245237instance KnownSymbol s => Read (Name s) where
246238 readsPrec _ = readP_to_S $ do
247239 _ <- string ("Name-" <> symbolVal (Proxy @s) <> "-")
248240 mkName <$> readS_to_P reads
249241
242+#ifdef BACKEND
250243instance Arbitrary (Name s) where
251244 arbitrary = coerce . T.pack . getPrintableString <$> arbitrary
245+#endif
252246
253247-- | This is a more \"explicit\" 'coerce' specifically for 'Name'.
254248-- You are forced to explicitly specify the phantom types you are converting
@@ -259,9 +253,3 @@ coerceName = coerce
259253{-# INLINE coerceName #-}
260254unsafeNameTagConvert = coerce
261255{-# INLINE unsafeNameTagConvert #-}
262−
263−#ifdef USE_LENS
264−makePrisms ''Id
265−makePrisms ''IntId
266−makePrisms ''Name
267−#endif
stack.yaml+1−13
@@ -1,16 +1,4 @@
1−resolver: lts-19.31
1+resolver: lts-18.18
22
33packages:
44- .
5−
6−nix:
7− packages: [ postgresql ]
8−
9−flags:
10− id:
11− rel8: false
12− openapi: false
13− lens: false
14− cassava: false
15− flat: false
16− store: false