git.iko.soy/typeable/swagger2
on master help sign in
Add prependPath helper
commit0368033288f591b7c32899a8e14ac4a7a2271275 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateMon 1 Feb 2016, 20:22 (11 years ago) parent005f62f
1 file changed+20−1
src/Data/Swagger/Operation.hs+20−1
src/Data/Swagger/Operation.hs+20−1
@@ -1,10 +1,12 @@
11{-# LANGUAGE RankNTypes #-}
22module Data.Swagger.Operation where
33
4+import Control.Applicative
5+import Control.Arrow
46import Control.Lens
57import Data.Data.Lens
6−
78import qualified Data.HashMap.Strict as HashMap
9+import Data.List
810import Data.Maybe (mapMaybe)
911import Data.Monoid
1012import Data.Swagger.Declare
@@ -12,6 +14,23 @@ import Data.Swagger.Internal
1214import Data.Swagger.Lens
1315import Data.Swagger.Schema
1416
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+
1534-- $setup
1635-- >>> import Data.Aeson
1736-- >>> import Data.Proxy