git.iko.soy/typeable/http-api-data
on master help sign in
Move Web.PathPieces content to .Internal module and export only classes
a694b71Nickolay Kudasov
Web/PathPieces.hs+1−154
@@ -1,162 +1,9 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE OverloadedStrings #-}
5−{-# LANGUAGE Rank2Types #-}
6−{-# LANGUAGE ScopedTypeVariables #-}
7−{-# LANGUAGE TypeSynonymInstances #-}
8-- |
1-- |
9-- Convert Haskell values to and from route pieces.
2-- Convert Haskell values to and from route pieces.
10module Web.PathPieces (
3module Web.PathPieces (
11 PathPiece (..),
4 PathPiece (..),
12 PathMultiPiece (..),
5 PathMultiPiece (..),
13− readFromPathPiece,
14− showToPathPiece,
15− WrappedPathPiece(..),
16) where
6) where
17
7
18−import Data.Monoid
8+import Web.PathPieces.Internal
19−import Data.Int
20−import Data.Word
21−import qualified Data.Text as S
22−import qualified Data.Text.Lazy as L
23−import Data.Version (Version)
24−import Data.Time (Day)
25−import Text.Read (readMaybe)
26−
27−import Unsafe.Coerce
28−
29−#if MIN_VERSION_base(4,8,0)
30−import Data.Void (Void)
31−#endif
32−
33−import Web.HttpApiData
34−import Web.HttpApiData.Internal (parseMaybeTextData)
35−
36−-- | Convert Haskell values to and from route piece.
37−class PathPiece s where
38− -- | Convert from route piece.
39− fromPathPiece :: S.Text -> Maybe s
40− default fromPathPiece :: FromHttpApiData s => S.Text -> Maybe s
41− fromPathPiece = either (const Nothing) Just . parseUrlPiece
42−
43− -- | Convert to route piece.
44− toPathPiece :: s -> S.Text
45− default toPathPiece :: ToHttpApiData s => s -> S.Text
46− toPathPiece = toUrlPiece
47−
48−instance PathPiece ()
49−instance PathPiece Char
50−instance PathPiece Bool
51−instance PathPiece Ordering
52−instance PathPiece Double
53−instance PathPiece Float
54−instance PathPiece Int
55−instance PathPiece Int8
56−instance PathPiece Int16
57−instance PathPiece Int32
58−instance PathPiece Int64
59−instance PathPiece Integer
60−instance PathPiece Word
61−instance PathPiece Word8
62−instance PathPiece Word16
63−instance PathPiece Word32
64−instance PathPiece Word64
65−instance PathPiece String
66−instance PathPiece S.Text
67−instance PathPiece L.Text
68−instance PathPiece Day
69−instance PathPiece Version
70−
71−#if MIN_VERSION_base(4,8,0)
72−instance PathPiece Void
73−#endif
74−
75−instance PathPiece All
76−instance PathPiece Any
77−
78−instance PathPiece a => PathPiece (Dual a) where
79− toPathPiece = toPathPiece1
80− fromPathPiece = fromPathPiece1
81−
82−instance PathPiece a => PathPiece (Sum a) where
83− toPathPiece = toPathPiece1
84− fromPathPiece = fromPathPiece1
85−
86−instance PathPiece a => PathPiece (Product a) where
87− toPathPiece = toPathPiece1
88− fromPathPiece = fromPathPiece1
89−
90−instance PathPiece a => PathPiece (First a) where
91− toPathPiece = toPathPiece1
92− fromPathPiece = fromPathPiece1
93−
94−instance PathPiece a => PathPiece (Last a) where
95− toPathPiece = toPathPiece1
96− fromPathPiece = fromPathPiece1
97−
98−instance PathPiece a => PathPiece (Maybe a) where
99− toPathPiece = toPathPiece1
100− fromPathPiece = fromPathPiece1
101−
102−instance (PathPiece a, PathPiece b) => PathPiece (Either a b) where
103− toPathPiece = toPathPiece2
104− fromPathPiece = fromPathPiece2
105−
106−-- | Wrapped @'PathPiece'@ value.
107−newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
108−
109−instance PathPiece a => ToHttpApiData (WrappedPathPiece a) where
110− toUrlPiece = toPathPiece . unwrapPathPiece
111−
112−instance PathPiece a => FromHttpApiData (WrappedPathPiece a) where
113− parseUrlPiece = fmap WrappedPathPiece . parseMaybeTextData fromPathPiece
114−
115−-- | Convert value to route piece using @'PathPiece'@ instance for its parameter.
116−toPathPiece1 :: forall f a. (PathPiece a, ToHttpApiData (f (WrappedPathPiece a))) => f a -> S.Text
117−toPathPiece1 = toUrlPiece . (unsafeCoerce :: f a -> f (WrappedPathPiece a))
118−
119−-- | Parse value from route piece using @'PathPiece'@ instance for its parameter.
120−fromPathPiece1 :: forall f a. (PathPiece a, FromHttpApiData (f (WrappedPathPiece a))) => S.Text -> Maybe (f a)
121−fromPathPiece1 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) -> f a)) . parseUrlPiece
122−
123−-- | Convert value to route piece using @'PathPiece'@ instance for its parameters.
124−toPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, ToHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => f a b -> S.Text
125−toPathPiece2 = toUrlPiece . (unsafeCoerce :: f a b -> f (WrappedPathPiece a) (WrappedPathPiece b))
126−
127−-- | Parse value from route piece using @'PathPiece'@ instance for its parameters.
128−fromPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, FromHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => S.Text -> Maybe (f a b)
129−fromPathPiece2 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) (WrappedPathPiece b) -> f a b)) . parseUrlPiece
130−
131−-- | Convert Haskell values to and from sequence of route pieces.
132−class PathMultiPiece s where
133− -- | Convert from sequence of route pieces.
134− fromPathMultiPiece :: [S.Text] -> Maybe s
135− -- | Convert to sequence of route pieces.
136− toPathMultiPiece :: s -> [S.Text]
137−
138−instance PathPiece a => PathMultiPiece [a] where
139− fromPathMultiPiece = mapM fromPathPiece
140− toPathMultiPiece = map toPathPiece
141−
142−-- | A function for helping generate free 'PathPiece'
143−-- instances for enumeration data types
144−-- that have derived 'Read' and 'Show' instances.
145−-- Intended to be used like this:
146−--
147−-- > data MyData = Foo | Bar | Baz
148−-- > deriving (Read,Show)
149−-- > instance PathPiece MyData where
150−-- > fromPathPiece = readFromPathPiece
151−-- > toPathPiece = showToPathPiece
152−--
153−-- Since 0.2.1.
154−readFromPathPiece :: Read s => S.Text -> Maybe s
155−readFromPathPiece = readMaybe . S.unpack
156−
157−-- | See the documentation for 'readFromPathPiece'.
158−--
159−-- Since 0.2.1.
160−showToPathPiece :: Show s => s -> S.Text
161−showToPathPiece = S.pack . show
162
9
Web/PathPieces/Internal.hsnew file+156
@@ -0,0 +1,156 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DefaultSignatures #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE Rank2Types #-}
6+{-# LANGUAGE ScopedTypeVariables #-}
7+{-# LANGUAGE TypeSynonymInstances #-}
8+-- |
9+-- Convert Haskell values to and from route pieces.
10+module Web.PathPieces.Internal where
11+
12+import Data.Monoid
13+import Data.Int
14+import Data.Word
15+import qualified Data.Text as S
16+import qualified Data.Text.Lazy as L
17+import Data.Version (Version)
18+import Data.Time (Day)
19+import Text.Read (readMaybe)
20+
21+import Unsafe.Coerce
22+
23+#if MIN_VERSION_base(4,8,0)
24+import Data.Void (Void)
25+#endif
26+
27+import Web.HttpApiData
28+import Web.HttpApiData.Internal (parseMaybeTextData)
29+
30+-- | Convert Haskell values to and from route piece.
31+class PathPiece s where
32+ -- | Convert from route piece.
33+ fromPathPiece :: S.Text -> Maybe s
34+ default fromPathPiece :: FromHttpApiData s => S.Text -> Maybe s
35+ fromPathPiece = either (const Nothing) Just . parseUrlPiece
36+
37+ -- | Convert to route piece.
38+ toPathPiece :: s -> S.Text
39+ default toPathPiece :: ToHttpApiData s => s -> S.Text
40+ toPathPiece = toUrlPiece
41+
42+instance PathPiece ()
43+instance PathPiece Char
44+instance PathPiece Bool
45+instance PathPiece Ordering
46+instance PathPiece Double
47+instance PathPiece Float
48+instance PathPiece Int
49+instance PathPiece Int8
50+instance PathPiece Int16
51+instance PathPiece Int32
52+instance PathPiece Int64
53+instance PathPiece Integer
54+instance PathPiece Word
55+instance PathPiece Word8
56+instance PathPiece Word16
57+instance PathPiece Word32
58+instance PathPiece Word64
59+instance PathPiece String
60+instance PathPiece S.Text
61+instance PathPiece L.Text
62+instance PathPiece Day
63+instance PathPiece Version
64+
65+#if MIN_VERSION_base(4,8,0)
66+instance PathPiece Void
67+#endif
68+
69+instance PathPiece All
70+instance PathPiece Any
71+
72+instance PathPiece a => PathPiece (Dual a) where
73+ toPathPiece = toPathPiece1
74+ fromPathPiece = fromPathPiece1
75+
76+instance PathPiece a => PathPiece (Sum a) where
77+ toPathPiece = toPathPiece1
78+ fromPathPiece = fromPathPiece1
79+
80+instance PathPiece a => PathPiece (Product a) where
81+ toPathPiece = toPathPiece1
82+ fromPathPiece = fromPathPiece1
83+
84+instance PathPiece a => PathPiece (First a) where
85+ toPathPiece = toPathPiece1
86+ fromPathPiece = fromPathPiece1
87+
88+instance PathPiece a => PathPiece (Last a) where
89+ toPathPiece = toPathPiece1
90+ fromPathPiece = fromPathPiece1
91+
92+instance PathPiece a => PathPiece (Maybe a) where
93+ toPathPiece = toPathPiece1
94+ fromPathPiece = fromPathPiece1
95+
96+instance (PathPiece a, PathPiece b) => PathPiece (Either a b) where
97+ toPathPiece = toPathPiece2
98+ fromPathPiece = fromPathPiece2
99+
100+-- | Wrapped @'PathPiece'@ value.
101+newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
102+
103+instance PathPiece a => ToHttpApiData (WrappedPathPiece a) where
104+ toUrlPiece = toPathPiece . unwrapPathPiece
105+
106+instance PathPiece a => FromHttpApiData (WrappedPathPiece a) where
107+ parseUrlPiece = fmap WrappedPathPiece . parseMaybeTextData fromPathPiece
108+
109+-- | Convert value to route piece using @'PathPiece'@ instance for its parameter.
110+toPathPiece1 :: forall f a. (PathPiece a, ToHttpApiData (f (WrappedPathPiece a))) => f a -> S.Text
111+toPathPiece1 = toUrlPiece . (unsafeCoerce :: f a -> f (WrappedPathPiece a))
112+
113+-- | Parse value from route piece using @'PathPiece'@ instance for its parameter.
114+fromPathPiece1 :: forall f a. (PathPiece a, FromHttpApiData (f (WrappedPathPiece a))) => S.Text -> Maybe (f a)
115+fromPathPiece1 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) -> f a)) . parseUrlPiece
116+
117+-- | Convert value to route piece using @'PathPiece'@ instance for its parameters.
118+toPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, ToHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => f a b -> S.Text
119+toPathPiece2 = toUrlPiece . (unsafeCoerce :: f a b -> f (WrappedPathPiece a) (WrappedPathPiece b))
120+
121+-- | Parse value from route piece using @'PathPiece'@ instance for its parameters.
122+fromPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, FromHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => S.Text -> Maybe (f a b)
123+fromPathPiece2 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) (WrappedPathPiece b) -> f a b)) . parseUrlPiece
124+
125+-- | Convert Haskell values to and from sequence of route pieces.
126+class PathMultiPiece s where
127+ -- | Convert from sequence of route pieces.
128+ fromPathMultiPiece :: [S.Text] -> Maybe s
129+ -- | Convert to sequence of route pieces.
130+ toPathMultiPiece :: s -> [S.Text]
131+
132+instance PathPiece a => PathMultiPiece [a] where
133+ fromPathMultiPiece = mapM fromPathPiece
134+ toPathMultiPiece = map toPathPiece
135+
136+-- | A function for helping generate free 'PathPiece'
137+-- instances for enumeration data types
138+-- that have derived 'Read' and 'Show' instances.
139+-- Intended to be used like this:
140+--
141+-- > data MyData = Foo | Bar | Baz
142+-- > deriving (Read,Show)
143+-- > instance PathPiece MyData where
144+-- > fromPathPiece = readFromPathPiece
145+-- > toPathPiece = showToPathPiece
146+--
147+-- Since 0.2.1.
148+readFromPathPiece :: Read s => S.Text -> Maybe s
149+readFromPathPiece = readMaybe . S.unpack
150+
151+-- | See the documentation for 'readFromPathPiece'.
152+--
153+-- Since 0.2.1.
154+showToPathPiece :: Show s => s -> S.Text
155+showToPathPiece = S.pack . show
156+
http-api-data.cabal+2−1
@@ -22,9 +22,10 @@ library
22 , bytestring
22 , bytestring
23 , time
23 , time
24 exposed-modules:
24 exposed-modules:
25− Web.PathPieces
26 Web.HttpApiData
25 Web.HttpApiData
27 Web.HttpApiData.Internal
26 Web.HttpApiData.Internal
27+ Web.PathPieces
28+ Web.PathPieces.Internal
28 ghc-options: -Wall
29 ghc-options: -Wall
29
30
30test-suite test
31test-suite test