refactored Servant.Docs.ToSample

This commit is contained in:
Sönke Hahn 2014-11-13 15:19:14 +08:00
parent 69041cc39e
commit 8d92d66e00
7 changed files with 16 additions and 11 deletions

View File

@ -46,9 +46,7 @@ instance ToParam (QueryParam "capital" Bool) where
Normal Normal
instance ToSample Greet where instance ToSample Greet where
toSample Proxy = Just (encode g) toSample = Just $ Greet "Hello, haskeller!"
where g = Greet "Hello, haskeller!"
-- API specification -- API specification
type TestApi = type TestApi =

View File

@ -46,5 +46,5 @@ instance ToSample a => HasDocs (Get a) where
single endpoint' action' single endpoint' action'
where endpoint' = endpoint & method .~ DocGET where endpoint' = endpoint & method .~ DocGET
action' = action & response.respBody .~ toSample p action' = action & response.respBody .~ sampleByteString p
p = Proxy :: Proxy a p = Proxy :: Proxy a

View File

@ -50,7 +50,7 @@ instance ToSample a => HasDocs (Post a) where
where endpoint' = endpoint & method .~ DocPOST where endpoint' = endpoint & method .~ DocPOST
action' = action & response.respBody .~ toSample p action' = action & response.respBody .~ sampleByteString p
& response.respStatus .~ 201 & response.respStatus .~ 201
p = Proxy :: Proxy a p = Proxy :: Proxy a

View File

@ -49,7 +49,7 @@ instance ToSample a => HasDocs (Put a) where
where endpoint' = endpoint & method .~ DocPUT where endpoint' = endpoint & method .~ DocPUT
action' = action & response.respBody .~ toSample p action' = action & response.respBody .~ sampleByteString p
& response.respStatus .~ 200 & response.respStatus .~ 200
p = Proxy :: Proxy a p = Proxy :: Proxy a

View File

@ -48,5 +48,5 @@ instance (ToSample a, HasDocs sublayout)
where sublayoutP = Proxy :: Proxy sublayout where sublayoutP = Proxy :: Proxy sublayout
action' = action & rqbody .~ toSample p action' = action & rqbody .~ sampleByteString p
p = Proxy :: Proxy a p = Proxy :: Proxy a

View File

@ -1,5 +1,6 @@
{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -88,7 +89,10 @@ module Servant.Docs
HasDocs(..), docs, markdown, printMarkdown HasDocs(..), docs, markdown, printMarkdown
, -- * Classes you need to implement for your types , -- * Classes you need to implement for your types
ToSample(..), ToParam(..), ToCapture(..) ToSample(..)
, sampleByteString
, ToParam(..)
, ToCapture(..)
, -- * ADTs to represent an 'API' , -- * ADTs to represent an 'API'
Method(..) Method(..)
@ -320,10 +324,13 @@ class HasDocs layout where
-- > -- >
-- > where g = Greet "Hello, haskeller!" -- > where g = Greet "Hello, haskeller!"
class ToJSON a => ToSample a where class ToJSON a => ToSample a where
toSample :: Proxy a -> Maybe ByteString toSample :: Maybe a
instance ToSample () where instance ToSample () where
toSample Proxy = Just $ encode () toSample = Just ()
sampleByteString :: forall a . ToSample a => Proxy a -> Maybe ByteString
sampleByteString Proxy = fmap encode (toSample :: Maybe a)
-- | The class that helps us automatically get documentation -- | The class that helps us automatically get documentation
-- for GET parameters. -- for GET parameters.

View File

@ -44,7 +44,7 @@ data Person = Person {
instance ToJSON Person instance ToJSON Person
instance FromJSON Person instance FromJSON Person
instance ToSample Person where instance ToSample Person where
toSample _proxy = Just $ encode alice toSample = Just alice
alice :: Person alice :: Person
alice = Person "Alice" 42 alice = Person "Alice" 42