git.iko.soy/typeable/swagger2
on master help sign in
Add prependPath helper
0368033Nickolay Kudasov
src/Data/Swagger/Operation.hs+20−1
@@ -1,10 +1,12 @@
1{-# LANGUAGE RankNTypes #-}
1{-# LANGUAGE RankNTypes #-}
2module Data.Swagger.Operation where
2module Data.Swagger.Operation where
3
3
4+import Control.Applicative
5+import Control.Arrow
4import Control.Lens
6import Control.Lens
5import Data.Data.Lens
7import Data.Data.Lens
6−
7import qualified Data.HashMap.Strict as HashMap
8import qualified Data.HashMap.Strict as HashMap
9+import Data.List
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
14
16
17+-- | Prepend path piece to all operations of the spec.
18+-- Leading and trailing slashes are trimmed/added automatically.
19+--
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 </>)
25+ where
26+ mapKeys f = HashMap.fromList . map (first f) . HashMap.toList
27+
28+ x </> y = case trim y of
29+ "" -> "/" <> trim x
30+ y' -> "/" <> trim x <> "/" <> y'
31+
32+ trim = dropWhile (== '/') . dropWhileEnd (== '/')
33+
15-- $setup
34-- $setup
16-- >>> import Data.Aeson
35-- >>> import Data.Aeson
17-- >>> import Data.Proxy
36-- >>> import Data.Proxy