on master help sign in
Merge pull request #3 from typeable/ville/add-read-instance
676e18eVille Tirronen
src/Data/Id.hs+16
@@ -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
@@ -122,6 +128,11 @@ mkIntId = coerce
122instance KnownSymbol s => Show (IntId s) where
128instance KnownSymbol s => Show (IntId s) where
123 show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> show v
129 show (IntId v) = "IntId-" <> symbolVal (Proxy @s) <> "-" <> show v
124
130
131+instance KnownSymbol s => Read (IntId s) where
132+ readsPrec _ = readP_to_S $ do
133+ _ <- string ("IntId-" <> symbolVal (Proxy @s) <> "-")
134+ mkIntId <$> readS_to_P reads
135+
125-- | This is a more \"explicit\" 'coerce' specifically for 'IntId'.
136-- | This is a more \"explicit\" 'coerce' specifically for 'IntId'.
126-- You are forced to explicitly specify the phantom types you are converting
137-- You are forced to explicitly specify the phantom types you are converting
127-- via the @TypeApplications@ compiler extension.
138-- via the @TypeApplications@ compiler extension.
@@ -155,6 +166,11 @@ mkName = coerce
155instance KnownSymbol s => Show (Name s) where
166instance KnownSymbol s => Show (Name s) where
156 show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> show v
167 show (Name v) = "Name-" <> symbolVal (Proxy @s) <> "-" <> show v
157
168
169+instance KnownSymbol s => Read (Name s) where
170+ readsPrec _ = readP_to_S $ do
171+ _ <- string ("Name-" <> symbolVal (Proxy @s) <> "-")
172+ mkName <$> readS_to_P reads
173+
158instance Arbitrary (Name s) where
174instance Arbitrary (Name s) where
159 arbitrary = coerce . T.pack . getPrintableString <$> arbitrary
175 arbitrary = coerce . T.pack . getPrintableString <$> arbitrary
160
176