on master help sign in
refactored Servant.Docs.ToSample
8d92d66Sönke Hahn
example/greet.hs+1−3
@@ -46,9 +46,7 @@ instance ToParam (QueryParam "capital" Bool) where
46 Normal
46 Normal
47
47
48instance ToSample Greet where
48instance ToSample Greet where
49− toSample Proxy = Just (encode g)
50−
51− where g = Greet "Hello, haskeller!"
49+ toSample = Just $ Greet "Hello, haskeller!"
52
50
53-- API specification
51-- API specification
54type TestApi =
52type TestApi =
src/Servant/API/Get.hs+1−1
@@ -46,5 +46,5 @@ instance ToSample a => HasDocs (Get a) where
46 single endpoint' action'
46 single endpoint' action'
47
47
48 where endpoint' = endpoint & method .~ DocGET
48 where endpoint' = endpoint & method .~ DocGET
49− action' = action & response.respBody .~ toSample p
49+ action' = action & response.respBody .~ sampleByteString p
50 p = Proxy :: Proxy a
50 p = Proxy :: Proxy a
src/Servant/API/Post.hs+1−1
@@ -50,7 +50,7 @@ instance ToSample a => HasDocs (Post a) where
50
50
51 where endpoint' = endpoint & method .~ DocPOST
51 where endpoint' = endpoint & method .~ DocPOST
52
52
53− action' = action & response.respBody .~ toSample p
53+ action' = action & response.respBody .~ sampleByteString p
54 & response.respStatus .~ 201
54 & response.respStatus .~ 201
55
55
56 p = Proxy :: Proxy a
56 p = Proxy :: Proxy a
src/Servant/API/Put.hs+1−1
@@ -49,7 +49,7 @@ instance ToSample a => HasDocs (Put a) where
49
49
50 where endpoint' = endpoint & method .~ DocPUT
50 where endpoint' = endpoint & method .~ DocPUT
51
51
52− action' = action & response.respBody .~ toSample p
52+ action' = action & response.respBody .~ sampleByteString p
53 & response.respStatus .~ 200
53 & response.respStatus .~ 200
54
54
55 p = Proxy :: Proxy a
55 p = Proxy :: Proxy a
src/Servant/API/ReqBody.hs+1−1
@@ -48,5 +48,5 @@ instance (ToSample a, HasDocs sublayout)
48
48
49 where sublayoutP = Proxy :: Proxy sublayout
49 where sublayoutP = Proxy :: Proxy sublayout
50
50
51− action' = action & rqbody .~ toSample p
51+ action' = action & rqbody .~ sampleByteString p
52 p = Proxy :: Proxy a
52 p = Proxy :: Proxy a
src/Servant/Docs.hs+10−3
@@ -1,5 +1,6 @@
1{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE TemplateHaskell #-}
4
5
5-------------------------------------------------------------------------------
6-------------------------------------------------------------------------------
@@ -88,7 +89,10 @@ module Servant.Docs
88 HasDocs(..), docs, markdown, printMarkdown
89 HasDocs(..), docs, markdown, printMarkdown
89
90
90 , -- * Classes you need to implement for your types
91 , -- * Classes you need to implement for your types
92+ ToSample(..)
93+ , sampleByteString
94+ , ToParam(..)
91− ToSample(..), ToParam(..), ToCapture(..)
95+ , ToCapture(..)
92
96
93 , -- * ADTs to represent an 'API'
97 , -- * ADTs to represent an 'API'
94 Method(..)
98 Method(..)
@@ -320,10 +324,13 @@ class HasDocs layout where
320-- >
324-- >
321-- > where g = Greet "Hello, haskeller!"
325-- > where g = Greet "Hello, haskeller!"
322class ToJSON a => ToSample a where
326class ToJSON a => ToSample a where
323− toSample :: Proxy a -> Maybe ByteString
327+ toSample :: Maybe a
324
328
325instance ToSample () where
329instance 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)
327
334
328-- | The class that helps us automatically get documentation
335-- | The class that helps us automatically get documentation
329-- for GET parameters.
336-- for GET parameters.
test/Servant/ServerSpec.hs+1−1
@@ -44,7 +44,7 @@ data Person = Person {
44instance ToJSON Person
44instance ToJSON Person
45instance FromJSON Person
45instance FromJSON Person
46instance ToSample Person where
46instance ToSample Person where
47− toSample _proxy = Just $ encode alice
47+ toSample = Just alice
48
48
49alice :: Person
49alice :: Person
50alice = Person "Alice" 42
50alice = Person "Alice" 42