@@ -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