git.iko.soy/typeable/servant
on master help sign in
Review fixes
commit5e4297a1358b7a7aa09e13e717ad6fb082be7b38 authorJulian K. Arni <jkarni@gmail.com> dateThu 19 Feb 2015, 10:29 (12 years ago) parent0658ab6
8 files changed+32−42
servant.cabal+1−1 src/Servant/API/ContentTypes.hs+22−33 src/Servant/API/Get.hs+1−1 src/Servant/API/Patch.hs+5−3 src/Servant/API/Post.hs+1−1 src/Servant/API/Put.hs+1−1 src/Servant/API/ReqBody.hs+1−1 src/Servant/Utils/Links.hs−1
servant.cabal+1−1
@@ -46,7 +46,7 @@ library
4646 build-depends:
4747 base >=4.7 && <5
4848 , bytestring == 0.10.*
49− , http-media >= 0.4 && < 0.5
49+ , http-media >= 0.4 && < 0.6
5050 , http-types == 0.8.*
5151 , text >= 1 && < 2
5252 , template-haskell >= 2.7 && < 2.10
src/Servant/API/ContentTypes.hs+22−33
@@ -11,7 +11,6 @@
1111{-# LANGUAGE UndecidableInstances #-}
1212module Servant.API.ContentTypes where
1313
14−import Control.Monad (join)
1514import qualified Data.ByteString as BS
1615import Data.ByteString.Lazy (ByteString)
1716import Data.String.Conversions (cs)
@@ -102,27 +101,25 @@ instance ( AllMimeRender ctyps a, IsNonEmpty ctyps
102101 ) => AllCTRender ctyps a where
103102 handleAcceptH _ (AcceptHeader accept) val = M.mapAcceptMedia lkup accept
104103 where pctyps = Proxy :: Proxy ctyps
105− amrs = amr pctyps val
106− lkup = zip (map fst amrs) $ map (\(a,b) -> (cs $ show a, b)) amrs
107−
108−
104+ amrs = allMimeRender pctyps val
105+ lkup = fmap (\(a,b) -> (a, (cs $ show a, b))) amrs
109106
110107
111108--------------------------------------------------------------------------
112109-- * Unrender
113110class Accept ctype => MimeUnrender ctype a where
114− fromByteString :: Proxy ctype -> ByteString -> Maybe a
111+ fromByteString :: Proxy ctype -> ByteString -> Either String a
115112
116113class AllCTUnrender list a where
117114 handleCTypeH :: Proxy list
118115 -> ByteString -- Content-Type header
119116 -> ByteString -- Request body
120− -> Maybe a
117+ -> Maybe (Either String a)
121118
122119instance ( AllMimeUnrender ctyps a, IsNonEmpty ctyps
123120 ) => AllCTUnrender ctyps a where
124− handleCTypeH _ ctypeH body = join $ M.mapContentMedia lkup (cs ctypeH)
125− where lkup = amu (Proxy :: Proxy ctyps) body
121+ handleCTypeH _ ctypeH body = M.mapContentMedia lkup (cs ctypeH)
122+ where lkup = allMimeUnrender (Proxy :: Proxy ctyps) body
126123
127124--------------------------------------------------------------------------
128125-- * Utils (Internal)
@@ -132,50 +129,42 @@ instance ( AllMimeUnrender ctyps a, IsNonEmpty ctyps
132129-- Check that all elements of list are instances of MimeRender
133130--------------------------------------------------------------------------
134131class AllMimeRender ls a where
135− amr :: Proxy ls
136− -> a -- value to serialize
137− -> [(M.MediaType, ByteString)] -- content-types/response pairs
132+ allMimeRender :: Proxy ls
133+ -> a -- value to serialize
134+ -> [(M.MediaType, ByteString)] -- content-types/response pairs
138135
139136instance ( MimeRender ctyp a ) => AllMimeRender '[ctyp] a where
140− amr _ a = [(contentType pctyp, toByteString pctyp a)]
137+ allMimeRender _ a = [(contentType pctyp, toByteString pctyp a)]
141138 where pctyp = Proxy :: Proxy ctyp
142139
143140instance ( MimeRender ctyp a
144− , MimeRender ctyp' a -- at least two elems to avoid overlap
145− , AllMimeRender ctyps a
141+ , AllMimeRender (ctyp' ': ctyps) a
146142 ) => AllMimeRender (ctyp ': ctyp' ': ctyps) a where
147− amr _ a = (contentType pctyp, toByteString pctyp a)
148− :(contentType pctyp', toByteString pctyp' a)
149− :(amr pctyps a)
143+ allMimeRender _ a = (contentType pctyp, toByteString pctyp a)
144+ :(allMimeRender pctyps a)
150145 where pctyp = Proxy :: Proxy ctyp
151− pctyps = Proxy :: Proxy ctyps
152− pctyp' = Proxy :: Proxy ctyp'
146+ pctyps = Proxy :: Proxy (ctyp' ': ctyps)
153147
154148
155149instance AllMimeRender '[] a where
156− amr _ _ = []
150+ allMimeRender _ _ = []
157151
158152--------------------------------------------------------------------------
159153-- Check that all elements of list are instances of MimeUnrender
160154--------------------------------------------------------------------------
161155class AllMimeUnrender ls a where
162− amu :: Proxy ls -> ByteString -> [(M.MediaType, Maybe a)]
156+ allMimeUnrender :: Proxy ls -> ByteString -> [(M.MediaType, Either String a)]
163157
164−instance ( MimeUnrender ctyp a ) => AllMimeUnrender '[ctyp] a where
165− amu _ val = [(contentType pctyp, fromByteString pctyp val)]
166− where pctyp = Proxy :: Proxy ctyp
158+instance AllMimeUnrender '[] a where
159+ allMimeUnrender _ _ = []
167160
168161instance ( MimeUnrender ctyp a
169− , MimeUnrender ctyp' a
170162 , AllMimeUnrender ctyps a
171− ) => AllMimeUnrender (ctyp ': ctyp' ': ctyps) a where
172− amu _ val = (contentType pctyp, fromByteString pctyp val)
173− :(contentType pctyp', fromByteString pctyp' val)
174− :(amu pctyps val)
163+ ) => AllMimeUnrender (ctyp ': ctyps) a where
164+ allMimeUnrender _ val = (contentType pctyp, fromByteString pctyp val)
165+ :(allMimeUnrender pctyps val)
175166 where pctyp = Proxy :: Proxy ctyp
176167 pctyps = Proxy :: Proxy ctyps
177− pctyp' = Proxy :: Proxy ctyp'
178168
179169type family IsNonEmpty (ls::[*]) :: Constraint where
180− IsNonEmpty '[] = 'False ~ 'True
181− IsNonEmpty x = ()
170+ IsNonEmpty (x ': xs) = ()
src/Servant/API/Get.hs+1−1
@@ -10,5 +10,5 @@ import Data.Typeable ( Typeable )
1010-- Example:
1111--
1212-- > type MyApi = "books" :> Get '[JSON] [Book]
13−data Get (cts::[*]) a
13+data Get (contentTypes::[*]) a
1414 deriving Typeable
src/Servant/API/Patch.hs+5−3
@@ -1,4 +1,6 @@
11{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE KindSignatures #-}
24module Servant.API.Patch where
35
46import Data.Typeable ( Typeable )
@@ -11,9 +13,9 @@ import Data.Typeable ( Typeable )
1113--
1214-- Example:
1315--
14−-- > -- POST /books
16+-- > -- PATCH /books
1517-- > -- with a JSON encoded Book as the request body
1618-- > -- returning the just-created Book
17−-- > type MyApi = "books" :> ReqBody Book :> Post Book
18−data Patch a
19+-- > type MyApi = "books" :> ReqBody Book :> Patch '[JSON] Book
20+data Patch (contentTypes::[*]) a
1921 deriving Typeable
src/Servant/API/Post.hs+1−1
@@ -15,5 +15,5 @@ import Data.Typeable ( Typeable )
1515-- > -- with a JSON encoded Book as the request body
1616-- > -- returning the just-created Book
1717-- > type MyApi = "books" :> ReqBody Book :> Post '[JSON] Book
18−data Post (cts::[*]) a
18+data Post (contentTypes::[*]) a
1919 deriving Typeable
src/Servant/API/Put.hs+1−1
@@ -13,5 +13,5 @@ import Data.Typeable ( Typeable )
1313-- > -- PUT /books/:isbn
1414-- > -- with a Book as request body, returning the updated Book
1515-- > type MyApi = "books" :> Capture "isbn" Text :> ReqBody Book :> Put '[JSON] Book
16−data Put (cts::[*]) a
16+data Put (contentTypes::[*]) a
1717 deriving Typeable
src/Servant/API/ReqBody.hs+1−1
@@ -8,4 +8,4 @@ module Servant.API.ReqBody where
88--
99-- > -- POST /books
1010-- > type MyApi = "books" :> ReqBody '[JSON] Book :> Post Book
11−data ReqBody (ls::[*]) a
11+data ReqBody (contentTypes::[*]) a
src/Servant/Utils/Links.hs−1
@@ -180,7 +180,6 @@ type family IsSubList a b :: Constraint where
180180 IsSubList '[x] (x ': xs) = ()
181181 IsSubList '[x] (y ': ys) = IsSubList '[x] ys
182182 IsSubList (x ': xs) y = IsSubList '[x] y `And` IsSubList xs y
183− IsSubList a b = 'True ~ 'False
184183
185184-- Phantom types for Param
186185data Matrix