on master help sign in
Add read instance for benefit of yesod in contractor
19de896Ville Tirronen
src/Data/Id.hs+6
@@ -31,6 +31,7 @@ import Data.Csv as Csv hiding(Name)
31import Flat as F
31import Flat as F
32import GHC.TypeLits
32import GHC.TypeLits
33import Test.QuickCheck
33import Test.QuickCheck
34+import Text.ParserCombinators.ReadP
34import Web.HttpApiData
35import Web.HttpApiData
35import Web.PathPieces (PathPiece(..))
36import Web.PathPieces (PathPiece(..))
36
37
@@ -55,6 +56,11 @@ mkId = coerce
55instance KnownSymbol s => Show (Id s) where
56instance KnownSymbol s => Show (Id s) where
56 show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> show v
57 show (Id v) = "Id-" <> symbolVal (Proxy @s) <> "-" <> show v
57
58
59+instance KnownSymbol s => Read (Id s) where
60+ readsPrec _ = readP_to_S $ do
61+ _ <- string ("Id-" <> symbolVal (Proxy @s) <> "-")
62+ mkId <$> readS_to_P reads
63+
58instance Flat (Id s) where
64instance Flat (Id s) where
59 encode = F.encode . UUID.toWords . coerce
65 encode = F.encode . UUID.toWords . coerce
60 decode = coerce . (\(a,b,c,d) -> UUID.fromWords a b c d) <$> F.decode
66 decode = coerce . (\(a,b,c,d) -> UUID.fromWords a b c d) <$> F.decode