on 7994085 help sign in
3 files changed+70−138
since splitsnapshots
unifiedsplit
cabal.project−11 id.cabal+47−61 src/Data/Id.hs+23−66
cabal.projectdeleted−11
@@ -1,11 +0,0 @@
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+47−61
@@ -1,77 +1,63 @@
1−cabal-version: 2.4
2−name: id
3−version: 0.2.0.0
4−license-file: LICENSE
1+cabal-version: 2.4
52
6−flag cassava
7− description: Enable Postgresql support
8− default: True
9− manual: False
3+name: id
4+version: 0.1.0.0
5+license-file: LICENSE
106
11−flag postgres
12− description: Enable Postgresql support
13− default: True
14− manual: False
157
16−flag flat
17− description: Enable Flat serialization support
18− default: True
19− manual: False
8+Flag postgres
9+ Description: Enable Postgresql support
10+ Default: True
11+ Manual: False
2012
21−flag store
22− description: Enable Data.Store serialization support
23− default: False
24− manual: False
13+Flag flat
14+ Description: Enable Flat serialization support
15+ Default: True
16+ Manual: False
2517
26−flag uuid-impl
27− description: Include some functions with extra dependencies.
28− Disable to reduce dependencies.
29− default: True
30− manual: False
18+Flag store
19+ Description: Enable Data.Store serialization support
20+ Default: False
21+ Manual: False
3122
3223library
33− exposed-modules: Data.Id
24+ exposed-modules:
25+ Data.Id
26+ -- other-modules:
27+ -- other-extensions:
3428 build-depends:
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)
29+ aeson,
30+ base >=4.12 && <5,
31+ binary,
32+ deepseq,
33+ hashable,
34+ http-api-data,
35+ lens,
36+ openapi3,
37+ path-pieces,
38+ text,
39+ QuickCheck,
40+ uuid,
41+ if flag(postgres) && !impl(ghcjs)
4642 build-depends:
47− , uuid >=1.3.9
48− cpp-options: -DUSE_UUID
49−
50− if !(impl(ghcjs) || arch(javascript) || arch(wasm32))
43+ postgresql-simple
44+ cpp-options: -DUSE_POSTGRES
45+ if !impl(ghcjs)
5146 build-depends:
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−
47+ cassava
6548 if flag(flat)
66− build-depends: flat >=0.4
67− cpp-options: -DUSE_FLAT
68−
49+ build-depends:
50+ flat,
51+ cpp-options: -DUSE_FLAT
6952 if flag(store)
70− build-depends: store
71− cpp-options: -DUSE_STORE
53+ build-depends:
54+ store,
55+ cpp-options: -DUSE_STORE
7256
73− hs-source-dirs: src
74− default-language: Haskell2010
57+ hs-source-dirs:
58+ src
59+ default-language:
60+ Haskell2010
7561 default-extensions:
7662 AllowAmbiguousTypes
7763 DeriveGeneric
src/Data/Id.hs+23−66
@@ -4,12 +4,9 @@
44{-# OPTIONS_GHC -Wno-orphans #-}
55
66module Data.Id
7− ( Id, mkId, unId, unsafeIdTagConvert, coerceId, _Id, nilId
7+ ( Id, mkId, unId, randomId, 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
1310 )
1411 where
1512
@@ -21,31 +18,24 @@ import Data.Binary (Binary)
2118import Data.Coerce (coerce)
2219import Data.Data
2320import Data.Hashable (Hashable)
21+import Data.OpenApi
2422import Data.String
2523import Data.Text as T
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
24+import Data.UUID (UUID)
25+import qualified Data.UUID as UUID
3526import qualified Data.UUID.V4 as UUID.V4
36−#endif
37−#ifdef BACKEND
38−import Data.OpenApi
39−import Test.QuickCheck
40−#ifdef USE_CASSAVA
27+#ifndef ghcjs_HOST_OS
4128import Data.Csv as Csv hiding(Name)
42−#endif
4329#ifdef USE_POSTGRES
4430import Database.PostgreSQL.Simple.FromField as PG (FromField)
4531import Database.PostgreSQL.Simple.ToField as PG (ToField)
4632#endif
4733#endif
48−
34+import GHC.TypeLits
35+import Test.QuickCheck
36+import Text.ParserCombinators.ReadP
37+import Web.HttpApiData
38+import Web.PathPieces (PathPiece(..))
4939#ifdef USE_FLAT
5040import Flat as F
5141#endif
@@ -62,11 +52,8 @@ newtype Id t = Id { unId :: UUID }
6252 , PG.ToField, PG.FromField
6353#endif
6454#endif
65− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey
66−#ifdef BACKEND
67− , ToSchema, ToParamSchema
68−#endif
69− , FromHttpApiData, ToHttpApiData )
55+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
56+ , ToParamSchema, FromHttpApiData, ToHttpApiData )
7057
7158type role Id nominal
7259
@@ -76,14 +63,12 @@ mkId :: forall s. UUID -> Id s
7663mkId = coerce
7764{-# INLINE mkId #-}
7865
79−#ifdef USE_UUID
8066randomId :: forall s. IO (Id s)
8167randomId = coerce <$> UUID.V4.nextRandom
8268{-# INLINE randomId #-}
83−#endif
8469
8570instance KnownSymbol s => Show (Id s) where
86− show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> P.show v
71+ show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> show v
8772
8873instance KnownSymbol s => Read (Id s) where
8974 readsPrec _ = readP_to_S $ do
@@ -104,14 +89,12 @@ instance Data.Store.Store (Id s) where
10489 size = Data.Store.ConstSize 16
10590#endif
10691
107−#ifdef BACKEND
10892instance Arbitrary (Id t) where
10993 arbitrary = fmap Id $ UUID.fromWords
11094 <$> arbitrary
11195 <*> arbitrary
11296 <*> arbitrary
11397 <*> arbitrary
114−#endif
11598
11699instance PathPiece (Id t) where
117100 fromPathPiece = coerce . UUID.fromText
@@ -119,14 +102,12 @@ instance PathPiece (Id t) where
119102
120103#ifndef ghcjs_HOST_OS
121104
122−#ifdef USE_CASSAVA
123105instance Csv.FromField (Id tag) where
124106 parseField = parseField
125107 >=> fmap mkId . maybe (fail "invalid UUID in CSV") pure . UUID.fromText
126108
127109instance Csv.ToField (Id tag) where
128110 toField = Csv.toField . UUID.toText . coerce
129−#endif
130111
131112#endif
132113
@@ -154,23 +135,12 @@ newtype IntId t = IntId { unIntId :: Integer }
154135 ( Eq, Ord, Binary
155136#ifndef ghcjs_HOST_OS
156137#ifdef USE_POSTGRES
157− , PG.ToField, PG.FromField
158−#endif
159−#ifdef USE_CASSAVA
160− , Csv.ToField, Csv.FromField
161−#endif
162−#endif
163−#ifdef USE_FLAT
164− , Flat
165−#endif
166−#ifdef USE_STORE
167− , Data.Store.Store
138+ , PG.ToField, PG.FromField, Csv.ToField, Csv.FromField
168139#endif
169−#ifdef BACKEND
170− , ToSchema, ToParamSchema, Arbitrary
171140#endif
172− , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, FromHttpApiData
173− , ToHttpApiData, PathPiece, Num, Integral, Real, Enum )
141+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
142+ , ToParamSchema, FromHttpApiData, ToHttpApiData, PathPiece, Flat, Arbitrary
143+ , Num, Integral, Real, Enum )
174144
175145type role IntId nominal
176146
@@ -181,7 +151,7 @@ mkIntId = coerce
181151{-# INLINE mkIntId #-}
182152
183153instance KnownSymbol s => Show (IntId s) where
184− show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> P.show v
154+ show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> show v
185155
186156instance KnownSymbol s => Read (IntId s) where
187157 readsPrec _ = readP_to_S $ do
@@ -205,23 +175,12 @@ newtype Name t = Name { unName :: Text }
205175 ( Eq, Ord, Binary
206176#ifndef ghcjs_HOST_OS
207177#ifdef USE_POSTGRES
208− , PG.ToField, PG.FromField
209−#endif
210−#ifdef USE_CASSAVA
211− , Csv.ToField, Csv.FromField
178+ , PG.ToField, PG.FromField, Csv.ToField, Csv.FromField
212179#endif
213180#endif
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 )
181+ , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
182+ , ToParamSchema, FromHttpApiData, ToHttpApiData, PathPiece, Flat
183+ , IsString, Semigroup, Monoid )
225184
226185type role Name nominal
227186
@@ -232,17 +191,15 @@ mkName = coerce
232191{-# INLINE mkName #-}
233192
234193instance KnownSymbol s => Show (Name s) where
235− show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> P.show v
194+ show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> show v
236195
237196instance KnownSymbol s => Read (Name s) where
238197 readsPrec _ = readP_to_S $ do
239198 _ <- string ("Name-" <> symbolVal (Proxy @s) <> "-")
240199 mkName <$> readS_to_P reads
241200
242−#ifdef BACKEND
243201instance Arbitrary (Name s) where
244202 arbitrary = coerce . T.pack . getPrintableString <$> arbitrary
245−#endif
246203
247204-- | This is a more \"explicit\" 'coerce' specifically for 'Name'.
248205-- You are forced to explicitly specify the phantom types you are converting