on master help sign in
Merge pull request #5 from typeable/add-store-and-flags

Make flat and postgres optional, add optional store

commit7bf424bcfc1a3cb1bec9c46cac4492675024f47d authorVille Tirronen <ville@typeable.io> dateThu 28 Jul 2022, 13:12 (4 years ago) parent0ff950bec1a8be
2 files changed+74−9
id.cabal+38−5 src/Data/Id.hs+36−4
id.cabal+38−5
@@ -1,9 +1,30 @@
11cabal-version: 2.4
22
33name: id
4−version: 0.1.0.0
4+version: 0.2.0.0
55license-file: LICENSE
66
7+Flag cassava
8+ Description: Enable Postgresql support
9+ Default: True
10+ Manual: False
11+
12+
13+Flag postgres
14+ Description: Enable Postgresql support
15+ Default: True
16+ Manual: False
17+
18+Flag flat
19+ Description: Enable Flat serialization support
20+ Default: True
21+ Manual: False
22+
23+Flag store
24+ Description: Enable Data.Store serialization support
25+ Default: False
26+ Manual: False
27+
728library
829 exposed-modules:
930 Data.Id
@@ -14,7 +35,6 @@ library
1435 base >=4.12 && <5,
1536 binary,
1637 deepseq,
17− flat,
1838 hashable,
1939 http-api-data,
2040 lens,
@@ -23,10 +43,23 @@ library
2343 text,
2444 QuickCheck,
2545 uuid,
26− if !impl(ghcjs)
46+ if flag(postgres) && !impl(ghcjs)
2747 build-depends:
28− cassava,
29− postgresql-simple,
48+ postgresql-simple
49+ cpp-options: -DUSE_POSTGRES
50+ if flag(cassava) && !impl(ghcjs)
51+ build-depends:
52+ cassava
53+ cpp-options: -DUSE_CASSAVA
54+ if flag(flat)
55+ build-depends:
56+ flat,
57+ cpp-options: -DUSE_FLAT
58+ if flag(store)
59+ build-depends:
60+ store,
61+ cpp-options: -DUSE_STORE
62+
3063 hs-source-dirs:
3164 src
3265 default-language:
src/Data/Id.hs+36−4
@@ -25,23 +25,34 @@ import Data.UUID (UUID)
2525import qualified Data.UUID as UUID
2626import qualified Data.UUID.V4 as UUID.V4
2727#ifndef ghcjs_HOST_OS
28+#ifdef USE_CASSAVA
29+import Data.Csv as Csv hiding(Name)
30+#endif
31+#ifdef USE_POSTGRES
2832import Database.PostgreSQL.Simple.FromField as PG (FromField)
2933import Database.PostgreSQL.Simple.ToField as PG (ToField)
30−import Data.Csv as Csv hiding(Name)
3134#endif
32−import Flat as F
35+#endif
3336import GHC.TypeLits
3437import Test.QuickCheck
3538import Text.ParserCombinators.ReadP
3639import Web.HttpApiData
3740import Web.PathPieces (PathPiece(..))
41+#ifdef USE_FLAT
42+import Flat as F
43+#endif
44+#ifdef USE_STORE
45+import qualified Data.Store
46+#endif
3847
3948newtype Id t = Id { unId :: UUID }
4049 deriving stock Data
4150 deriving newtype
4251 ( Eq, Ord, Binary
4352#ifndef ghcjs_HOST_OS
53+#ifdef USE_POSTGRES
4454 , PG.ToField, PG.FromField
55+#endif
4556#endif
4657 , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
4758 , ToParamSchema, FromHttpApiData, ToHttpApiData )
@@ -66,10 +77,19 @@ instance KnownSymbol s => Read (Id s) where
6677 _ <- string ("Id-" <> symbolVal (Proxy @s) <> "-")
6778 mkId <$> readS_to_P reads
6879
80+#ifdef USE_FLAT
6981instance Flat (Id s) where
7082 encode = F.encode . UUID.toWords . coerce
7183 decode = coerce . (\(a,b,c,d) -> UUID.fromWords a b c d) <$> F.decode
7284 size = F.size . UUID.toWords . coerce
85+#endif
86+
87+#ifdef USE_STORE
88+instance Data.Store.Store (Id s) where
89+ poke = Data.Store.poke . UUID.toWords . coerce
90+ peek = coerce . (\(a,b,c,d) -> UUID.fromWords a b c d) <$> Data.Store.peek
91+ size = Data.Store.ConstSize 16
92+#endif
7393
7494instance Arbitrary (Id t) where
7595 arbitrary = fmap Id $ UUID.fromWords
@@ -84,12 +104,14 @@ instance PathPiece (Id t) where
84104
85105#ifndef ghcjs_HOST_OS
86106
107+#ifdef USE_CASSAVA
87108instance Csv.FromField (Id tag) where
88109 parseField = parseField
89110 >=> fmap mkId . maybe (fail "invalid UUID in CSV") pure . UUID.fromText
90111
91112instance Csv.ToField (Id tag) where
92113 toField = Csv.toField . UUID.toText . coerce
114+#endif
93115
94116#endif
95117
@@ -116,7 +138,12 @@ newtype IntId t = IntId { unIntId :: Integer }
116138 deriving newtype
117139 ( Eq, Ord, Binary
118140#ifndef ghcjs_HOST_OS
119− , PG.ToField, PG.FromField, Csv.ToField, Csv.FromField
141+#ifdef USE_POSTGRES
142+ , PG.ToField, PG.FromField
143+#endif
144+#ifdef USE_CASSAVA
145+ , Csv.ToField, Csv.FromField
146+#endif
120147#endif
121148 , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
122149 , ToParamSchema, FromHttpApiData, ToHttpApiData, PathPiece, Flat, Arbitrary
@@ -154,7 +181,12 @@ newtype Name t = Name { unName :: Text }
154181 deriving newtype
155182 ( Eq, Ord, Binary
156183#ifndef ghcjs_HOST_OS
157− , PG.ToField, PG.FromField, Csv.ToField, Csv.FromField
184+#ifdef USE_POSTGRES
185+ , PG.ToField, PG.FromField
186+#endif
187+#ifdef USE_CASSAVA
188+ , Csv.ToField, Csv.FromField
189+#endif
158190#endif
159191 , FromJSON, ToJSON, NFData, Hashable, FromJSONKey, ToJSONKey, ToSchema
160192 , ToParamSchema, FromHttpApiData, ToHttpApiData, PathPiece, Flat