@@ -1,10 +1,12 @@
1{-# LANGUAGE RankNTypes #-}
1{-# LANGUAGE RankNTypes #-}
2module Data.Swagger.Operation where
2module Data.Swagger.Operation where
4+import Control.Applicative
4import Control.Lens
6import Control.Lens
5import Data.Data.Lens
7import Data.Data.Lens
7import qualified Data.HashMap.Strict as HashMap
8import qualified Data.HashMap.Strict as HashMap
8import Data.Maybe (mapMaybe)
10import Data.Maybe (mapMaybe)
9import Data.Monoid
11import Data.Monoid
10import Data.Swagger.Declare
12import Data.Swagger.Declare
@@ -12,6 +14,23 @@ import Data.Swagger.Internal
12import Data.Swagger.Lens
14import Data.Swagger.Lens
13import Data.Swagger.Schema
15import Data.Swagger.Schema
17+-- | Prepend path piece to all operations of the spec.
18+-- Leading and trailing slashes are trimmed/added automatically.
20+-- >>> let api = (mempty :: Swagger) & paths .~ [("/info", mempty)]
21+-- >>> encode $ prependPath "user/{user_id}" api ^. paths
22+-- "{\"/user/{user_id}/info\":{}}"
23+prependPath :: FilePath -> Swagger -> Swagger
24+prependPath path = paths %~ mapKeys (path </>)
26+ mapKeys f = HashMap.fromList . map (first f) . HashMap.toList
28+ x </> y = case trim y of
30+ y' -> "/" <> trim x <> "/" <> y'
32+ trim = dropWhile (== '/') . dropWhileEnd (== '/')
16-- >>> import Data.Aeson
35-- >>> import Data.Aeson
17-- >>> import Data.Proxy
36-- >>> import Data.Proxy