Render entpoints in canonical order.

This commit is contained in:
Matthias Fischmann 2015-02-23 10:53:18 +01:00
parent 795a770780
commit 1fef813a3b

View file

@ -11,6 +11,7 @@
{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- | This module lets you get API docs for free. It lets generate -- | This module lets you get API docs for free. It lets generate
@ -190,6 +191,7 @@ import Data.ByteString.Lazy.Char8 (ByteString)
import Data.Hashable import Data.Hashable
import Data.HashMap.Strict (HashMap) import Data.HashMap.Strict (HashMap)
import Data.List import Data.List
import Data.Function (on)
import Data.Maybe import Data.Maybe
import Data.Monoid import Data.Monoid
import Data.Ord (comparing) import Data.Ord (comparing)
@ -207,12 +209,18 @@ import qualified Data.HashMap.Strict as HM
import qualified Data.Text as T import qualified Data.Text as T
import qualified Network.HTTP.Media as M import qualified Network.HTTP.Media as M
-- | Temporary orphan. Can be eliminated as soon as
-- https://github.com/zmthy/http-media/pull/12 has been released.
-- (Also consider removing StandaloneDeriving language ext above.)
instance Ord M.MediaType where
compare t t' = compare (show t) (show t')
-- | Supported HTTP request methods -- | Supported HTTP request methods
data Method = DocDELETE -- ^ the DELETE method data Method = DocDELETE -- ^ the DELETE method
| DocGET -- ^ the GET method | DocGET -- ^ the GET method
| DocPOST -- ^ the POST method | DocPOST -- ^ the POST method
| DocPUT -- ^ the PUT method | DocPUT -- ^ the PUT method
deriving (Eq, Generic) deriving (Eq, Ord, Generic)
instance Show Method where instance Show Method where
show DocGET = "GET" show DocGET = "GET"
@ -239,7 +247,7 @@ instance Hashable Method
data Endpoint = Endpoint data Endpoint = Endpoint
{ _path :: [String] -- type collected { _path :: [String] -- type collected
, _method :: Method -- type collected , _method :: Method -- type collected
} deriving (Eq, Generic) } deriving (Eq, Ord, Generic)
instance Show Endpoint where instance Show Endpoint where
show (Endpoint p m) = show (Endpoint p m) =
@ -291,7 +299,7 @@ emptyAPI = mempty
data DocCapture = DocCapture data DocCapture = DocCapture
{ _capSymbol :: String -- type supplied { _capSymbol :: String -- type supplied
, _capDesc :: String -- user supplied , _capDesc :: String -- user supplied
} deriving (Eq, Show) } deriving (Eq, Ord, Show)
-- | A type to represent a /GET/ parameter from the Query String. Holds its name, -- | A type to represent a /GET/ parameter from the Query String. Holds its name,
-- the possible values (leave empty if there isn't a finite number of them), -- the possible values (leave empty if there isn't a finite number of them),
@ -303,7 +311,7 @@ data DocQueryParam = DocQueryParam
, _paramValues :: [String] -- user supplied , _paramValues :: [String] -- user supplied
, _paramDesc :: String -- user supplied , _paramDesc :: String -- user supplied
, _paramKind :: ParamKind , _paramKind :: ParamKind
} deriving (Eq, Show) } deriving (Eq, Ord, Show)
-- | An introductory paragraph for your documentation. You can pass these to -- | An introductory paragraph for your documentation. You can pass these to
-- 'docsWithIntros'. -- 'docsWithIntros'.
@ -322,7 +330,7 @@ instance Ord DocIntro where
data DocNote = DocNote data DocNote = DocNote
{ _noteTitle :: String { _noteTitle :: String
, _noteBody :: [String] , _noteBody :: [String]
} deriving (Eq, Show) } deriving (Eq, Ord, Show)
-- | Type of extra information that a user may wish to "union" with their -- | Type of extra information that a user may wish to "union" with their
-- documentation. -- documentation.
@ -341,7 +349,7 @@ instance Monoid (ExtraInfo a) where
-- - List corresponds to @QueryParams@, i.e GET parameters with multiple values -- - List corresponds to @QueryParams@, i.e GET parameters with multiple values
-- - Flag corresponds to @QueryFlag@, i.e a value-less GET parameter -- - Flag corresponds to @QueryFlag@, i.e a value-less GET parameter
data ParamKind = Normal | List | Flag data ParamKind = Normal | List | Flag
deriving (Eq, Show) deriving (Eq, Ord, Show)
-- | A type to represent an HTTP response. Has an 'Int' status, a list of -- | A type to represent an HTTP response. Has an 'Int' status, a list of
-- possible 'MediaType's, and a list of example 'ByteString' response bodies. -- possible 'MediaType's, and a list of example 'ByteString' response bodies.
@ -362,7 +370,7 @@ data Response = Response
{ _respStatus :: Int { _respStatus :: Int
, _respTypes :: [M.MediaType] , _respTypes :: [M.MediaType]
, _respBody :: [(Text, M.MediaType, ByteString)] , _respBody :: [(Text, M.MediaType, ByteString)]
} deriving (Eq, Show) } deriving (Eq, Ord, Show)
-- | Default response: status code 200, no response body. -- | Default response: status code 200, no response body.
-- --
@ -394,7 +402,7 @@ data Action = Action
, _rqtypes :: [M.MediaType] -- type collected , _rqtypes :: [M.MediaType] -- type collected
, _rqbody :: [(M.MediaType, ByteString)] -- user supplied , _rqbody :: [(M.MediaType, ByteString)] -- user supplied
, _response :: Response -- user supplied , _response :: Response -- user supplied
} deriving (Eq, Show) } deriving (Eq, Ord, Show)
-- | Combine two Actions, we can't make a monoid as merging Response breaks the -- | Combine two Actions, we can't make a monoid as merging Response breaks the
-- laws. -- laws.
@ -611,7 +619,7 @@ class ToCapture c where
markdown :: API -> String markdown :: API -> String
markdown api = unlines $ markdown api = unlines $
introsStr (api ^. apiIntros) introsStr (api ^. apiIntros)
++ (concatMap (uncurry printEndpoint) . HM.toList $ api ^. apiEndpoints) ++ (concatMap (uncurry printEndpoint) . sort . HM.toList $ api ^. apiEndpoints)
where printEndpoint :: Endpoint -> Action -> [String] where printEndpoint :: Endpoint -> Action -> [String]
printEndpoint endpoint action = printEndpoint endpoint action =