git.iko.soy/typeable/servant
on master help sign in
refactored Servant.Docs.ToSample
commit8d92d66e00c41aa1240ab5190a387fb638267842 authorSönke Hahn <soenke.hahn@zalora.com> dateThu 13 Nov 2014, 15:19 (12 years ago) parent69041cc
7 files changed+16−11
example/greet.hs+1−3 src/Servant/API/Get.hs+1−1 src/Servant/API/Post.hs+1−1 src/Servant/API/Put.hs+1−1 src/Servant/API/ReqBody.hs+1−1 src/Servant/Docs.hs+10−3 test/Servant/ServerSpec.hs+1−1
example/greet.hs+1−3
@@ -46,9 +46,7 @@ instance ToParam (QueryParam "capital" Bool) where
4646 Normal
4747
4848instance ToSample Greet where
49− toSample Proxy = Just (encode g)
50−
51− where g = Greet "Hello, haskeller!"
49+ toSample = Just $ Greet "Hello, haskeller!"
5250
5351-- API specification
5452type TestApi =
src/Servant/API/Get.hs+1−1
@@ -46,5 +46,5 @@ instance ToSample a => HasDocs (Get a) where
4646 single endpoint' action'
4747
4848 where endpoint' = endpoint & method .~ DocGET
49− action' = action & response.respBody .~ toSample p
49+ action' = action & response.respBody .~ sampleByteString p
5050 p = Proxy :: Proxy a
src/Servant/API/Post.hs+1−1
@@ -50,7 +50,7 @@ instance ToSample a => HasDocs (Post a) where
5050
5151 where endpoint' = endpoint & method .~ DocPOST
5252
53− action' = action & response.respBody .~ toSample p
53+ action' = action & response.respBody .~ sampleByteString p
5454 & response.respStatus .~ 201
5555
5656 p = Proxy :: Proxy a
src/Servant/API/Put.hs+1−1
@@ -49,7 +49,7 @@ instance ToSample a => HasDocs (Put a) where
4949
5050 where endpoint' = endpoint & method .~ DocPUT
5151
52− action' = action & response.respBody .~ toSample p
52+ action' = action & response.respBody .~ sampleByteString p
5353 & response.respStatus .~ 200
5454
5555 p = Proxy :: Proxy a
src/Servant/API/ReqBody.hs+1−1
@@ -48,5 +48,5 @@ instance (ToSample a, HasDocs sublayout)
4848
4949 where sublayoutP = Proxy :: Proxy sublayout
5050
51− action' = action & rqbody .~ toSample p
51+ action' = action & rqbody .~ sampleByteString p
5252 p = Proxy :: Proxy a
src/Servant/Docs.hs+10−3
@@ -1,5 +1,6 @@
11{-# LANGUAGE TypeFamilies #-}
22{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
34{-# LANGUAGE TemplateHaskell #-}
45
56-------------------------------------------------------------------------------
@@ -88,7 +89,10 @@ module Servant.Docs
8889 HasDocs(..), docs, markdown, printMarkdown
8990
9091 , -- * Classes you need to implement for your types
91− ToSample(..), ToParam(..), ToCapture(..)
92+ ToSample(..)
93+ , sampleByteString
94+ , ToParam(..)
95+ , ToCapture(..)
9296
9397 , -- * ADTs to represent an 'API'
9498 Method(..)
@@ -320,10 +324,13 @@ class HasDocs layout where
320324-- >
321325-- > where g = Greet "Hello, haskeller!"
322326class ToJSON a => ToSample a where
323− toSample :: Proxy a -> Maybe ByteString
327+ toSample :: Maybe a
324328
325329instance ToSample () where
326− toSample Proxy = Just $ encode ()
330+ toSample = Just ()
331+
332+sampleByteString :: forall a . ToSample a => Proxy a -> Maybe ByteString
333+sampleByteString Proxy = fmap encode (toSample :: Maybe a)
327334
328335-- | The class that helps us automatically get documentation
329336-- for GET parameters.
test/Servant/ServerSpec.hs+1−1
@@ -44,7 +44,7 @@ data Person = Person {
4444instance ToJSON Person
4545instance FromJSON Person
4646instance ToSample Person where
47− toSample _proxy = Just $ encode alice
47+ toSample = Just alice
4848
4949alice :: Person
5050alice = Person "Alice" 42