Merge remote-tracking branch 'upstream/master' into wip-notes

Conflicts:
	src/Servant/Docs.hs
This commit is contained in:
Christian Marie 2015-01-30 15:57:56 +11:00
commit dd34ba0d92
4 changed files with 121 additions and 28 deletions

18
.gitignore vendored
View file

@ -1,3 +1,17 @@
.cabal-sandbox
cabal.sandbox.config
dist dist
cabal-dev
*.o
*.hi
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.virtualenv
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
cabal.config
*.prof
*.aux
*.hp

View file

@ -37,6 +37,13 @@ instance ToParam (QueryParam "capital" Bool) where
\Default is false." \Default is false."
Normal Normal
instance ToParam (MatrixParam "lang" String) where
toParam _ =
DocQueryParam "lang"
["en", "sv", "fr"]
"Get the greeting message selected language. Default is en."
Normal
instance ToSample Greet where instance ToSample Greet where
toSample = Just $ Greet "Hello, haskeller!" toSample = Just $ Greet "Hello, haskeller!"
@ -59,7 +66,7 @@ intro2 = DocIntro "This title is below the last"
-- API specification -- API specification
type TestApi = type TestApi =
-- GET /hello/:name?capital={true, false} returns a Greet as JSON -- GET /hello/:name?capital={true, false} returns a Greet as JSON
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
-- POST /greet with a Greet as JSON in the request body, -- POST /greet with a Greet as JSON in the request body,
-- returns a Greet as JSON -- returns a Greet as JSON

View file

@ -13,9 +13,7 @@ You'll also note that multiple intros are possible.
#### Request Body: #### Request Body:
``` javascript ``` javascript
{ "Hello, haskeller!"
"msg": "Hello, haskeller!"
}
``` ```
#### Response: #### Response:
@ -24,30 +22,37 @@ You'll also note that multiple intros are possible.
- If you use ?capital=true - If you use ?capital=true
``` javascript ``` javascript
{ "HELLO, HASKELLER"
"msg": "HELLO, HASKELLER"
}
``` ```
- If you use ?capital=false - If you use ?capital=false
``` javascript ``` javascript
{ "Hello, haskeller"
"msg": "Hello, haskeller"
}
``` ```
## GET /hello/:name ## GET /hello;lang=<value>/:name
#### Captures: #### Captures:
- *name*: name of the person to greet - *name*: name of the person to greet
#### Matrix Parameters**:
**hello**:
- lang
- **Values**: *en, sv, fr*
- **Description**: Get the greeting message selected language. Default is en.
#### GET Parameters: #### GET Parameters:
- capital - capital
- **Values**: *true, false* - **Values**: *true, false*
- **Description**: Get the greeting message in uppercase (true) or not (false). Default is false. - **Description**: Get the greeting message in uppercase (true) or not (false).Default is false.
#### Response: #### Response:
@ -56,17 +61,13 @@ You'll also note that multiple intros are possible.
- If you use ?capital=true - If you use ?capital=true
``` javascript ``` javascript
{ "HELLO, HASKELLER"
"msg": "HELLO, HASKELLER"
}
``` ```
- If you use ?capital=false - If you use ?capital=false
``` javascript ``` javascript
{ "Hello, haskeller"
"msg": "Hello, haskeller"
}
``` ```
## DELETE /greet/:greetid ## DELETE /greet/:greetid

View file

@ -73,6 +73,13 @@
-- > \Default is false." -- > \Default is false."
-- > Normal -- > Normal
-- > -- >
-- > instance ToParam (MatrixParam "lang" String) where
-- > toParam _ =
-- > DocQueryParam "lang"
-- > ["en", "sv", "fr"]
-- > "Get the greeting message selected language. Default is en."
-- > Normal
-- >
-- > instance ToSample Greet where -- > instance ToSample Greet where
-- > toSample = Just $ Greet "Hello, haskeller!" -- > toSample = Just $ Greet "Hello, haskeller!"
-- > -- >
@ -95,7 +102,7 @@
-- > -- API specification -- > -- API specification
-- > type TestApi = -- > type TestApi =
-- > -- GET /hello/:name?capital={true, false} returns a Greet as JSON -- > -- GET /hello/:name?capital={true, false} returns a Greet as JSON
-- > "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet -- > "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
-- > -- >
-- > -- POST /greet with a Greet as JSON in the request body, -- > -- POST /greet with a Greet as JSON in the request body,
-- > -- returns a Greet as JSON -- > -- returns a Greet as JSON
@ -116,6 +123,7 @@
-- > -- > docs testAPI -- > -- > docs testAPI
-- > docsGreet :: API -- > docsGreet :: API
-- > docsGreet = docsWithIntros [intro1, intro2] testApi -- > docsGreet = docsWithIntros [intro1, intro2] testApi
-- >
-- > main :: IO () -- > main :: IO ()
-- > main = putStrLn $ markdown docsGreet -- > main = putStrLn $ markdown docsGreet
module Servant.Docs module Servant.Docs
@ -336,6 +344,7 @@ data Action = Action
, _headers :: [Text] -- type collected , _headers :: [Text] -- type collected
, _params :: [DocQueryParam] -- type collected + user supplied info , _params :: [DocQueryParam] -- type collected + user supplied info
, _notes :: [DocNote] -- user supplied , _notes :: [DocNote] -- user supplied
, _mxParams :: [(String, [DocQueryParam])] -- type collected + user supplied info
, _rqbody :: Maybe ByteString -- user supplied , _rqbody :: Maybe ByteString -- user supplied
, _response :: Response -- user supplied , _response :: Response -- user supplied
} deriving (Eq, Show) } deriving (Eq, Show)
@ -346,15 +355,16 @@ data Action = Action
-- Tweakable with lenses. -- Tweakable with lenses.
-- --
-- > λ> defAction -- > λ> defAction
-- > Action {_captures = [], _params = [], _rqbody = Nothing, _response = Response {_respStatus = 200, _respBody = Nothing}} -- > Action {_captures = [], _headers = [], _params = [], _mxParams = [], _rqbody = Nothing, _response = Response {_respStatus = 200, _respBody = Nothing}}
-- > λ> defAction & response.respStatus .~ 201 -- > λ> defAction & response.respStatus .~ 201
-- > Action {_captures = [], _params = [], _rqbody = Nothing, _response = Response {_respStatus = 201, _respBody = Nothing}} -- > Action {_captures = [], _headers = [], _params = [], _mxParams = [], _rqbody = Nothing, _response = Response {_respStatus = 201, _respBody = Nothing}}
defAction :: Action defAction :: Action
defAction = defAction =
Action [] Action []
[] []
[] []
[] []
[]
Nothing Nothing
defResponse defResponse
@ -473,6 +483,7 @@ markdown api = unlines $
"" : "" :
notesStr (action ^. notes) ++ notesStr (action ^. notes) ++
capturesStr (action ^. captures) ++ capturesStr (action ^. captures) ++
mxParamsStr (action ^. mxParams) ++
headersStr (action ^. headers) ++ headersStr (action ^. headers) ++
paramsStr (action ^. params) ++ paramsStr (action ^. params) ++
rqbodyStr (action ^. rqbody) ++ rqbodyStr (action ^. rqbody) ++
@ -515,6 +526,22 @@ markdown api = unlines $
captureStr cap = captureStr cap =
"- *" ++ (cap ^. capSymbol) ++ "*: " ++ (cap ^. capDesc) "- *" ++ (cap ^. capSymbol) ++ "*: " ++ (cap ^. capDesc)
mxParamsStr :: [(String, [DocQueryParam])] -> [String]
mxParamsStr [] = []
mxParamsStr l =
"#### Matrix Parameters:" :
"" :
map segmentStr l ++
"" :
[]
segmentStr :: (String, [DocQueryParam]) -> String
segmentStr (segment, l) = unlines $
("**" ++ segment ++ "**:") :
"" :
map paramStr l ++
"" :
[]
headersStr :: [Text] -> [String] headersStr :: [Text] -> [String]
headersStr [] = [] headersStr [] = []
headersStr l = [""] ++ map headerStr l ++ [""] headersStr l = [""] ++ map headerStr l ++ [""]
@ -685,6 +712,50 @@ instance (KnownSymbol sym, ToParam (QueryFlag sym), HasDocs sublayout)
paramP = Proxy :: Proxy (QueryFlag sym) paramP = Proxy :: Proxy (QueryFlag sym)
action' = over params (|> toParam paramP) action action' = over params (|> toParam paramP) action
instance (KnownSymbol sym, ToParam (MatrixParam sym a), HasDocs sublayout)
=> HasDocs (MatrixParam sym a :> sublayout) where
docsFor Proxy (endpoint, action) =
docsFor sublayoutP (endpoint', action')
where sublayoutP = Proxy :: Proxy sublayout
paramP = Proxy :: Proxy (MatrixParam sym a)
segment = endpoint ^. (path._last)
segment' = action ^. (mxParams._last._1)
endpoint' = over (path._last) (\p -> p ++ ";" ++ symbolVal symP ++ "=<value>") endpoint
action' = if segment' /= segment
-- This is the first matrix parameter for this segment, insert a new entry into the mxParams list
then over mxParams (|> (segment, [toParam paramP])) action
-- We've already inserted a matrix parameter for this segment, append to the existing list
else action & mxParams._last._2 <>~ [toParam paramP]
symP = Proxy :: Proxy sym
instance (KnownSymbol sym, {- ToParam (MatrixParams sym a), -} HasDocs sublayout)
=> HasDocs (MatrixParams sym a :> sublayout) where
docsFor Proxy (endpoint, action) =
docsFor sublayoutP (endpoint', action)
where sublayoutP = Proxy :: Proxy sublayout
endpoint' = over path (\p -> p ++ [";" ++ symbolVal symP ++ "=<value>"]) endpoint
symP = Proxy :: Proxy sym
instance (KnownSymbol sym, {- ToParam (MatrixFlag sym), -} HasDocs sublayout)
=> HasDocs (MatrixFlag sym :> sublayout) where
docsFor Proxy (endpoint, action) =
docsFor sublayoutP (endpoint', action)
where sublayoutP = Proxy :: Proxy sublayout
endpoint' = over path (\p -> p ++ [";" ++ symbolVal symP]) endpoint
symP = Proxy :: Proxy sym
instance HasDocs Raw where instance HasDocs Raw where
docsFor _proxy (endpoint, action) = docsFor _proxy (endpoint, action) =
single endpoint action single endpoint action