git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #47 from GetShopTV/isstring-#43

IsString instances

commit104bce684df33c840971a31a0c143c48fa24cc28 authorNickolay Kudasov <fizruk@users.noreply.github.com> dateMon 1 Feb 2016, 17:58 (11 years ago) parent87906b0ee1ebb7
4 files changed+30−10
src/Data/Swagger.hs+4−3 src/Data/Swagger/Internal.hs+18−2 src/Data/Swagger/Lens.hs+2 test/Data/SwaggerSpec.hs+6−5
src/Data/Swagger.hs+4−3
@@ -168,9 +168,10 @@ import Data.Swagger.Internal
168168-- & paths .~
169169-- [ ("/user", mempty & get ?~ (mempty
170170-- & produces ?~ MimeList ["application/json"]
171−-- & at 200 ?~ Inline (mempty & schema ?~ Ref (Reference "User")))) ]
171+-- & at 200 ?~ ("OK" & _Inline.schema ?~ Ref (Reference "User"))
172+-- & at 404 ?~ "User info not found")) ]
172173-- :}
173−-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"User\":{\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}}"
174+-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"User\":{\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"404\":{\"description\":\"User info not found\"},\"200\":{\"schema\":{\"$ref\":\"#/definitions/User\"},\"description\":\"OK\"}},\"produces\":[\"application/json\"]}}}}"
174175--
175176-- In the snippet above we declare API with a single path @/user@ providing method @GET@
176177-- which produces @application/json@ output and should respond with code @200@ and body specified
@@ -202,7 +203,7 @@ import Data.Swagger.Internal
202203--
203204-- >>> :{
204205-- encode $ (mempty :: Operation)
205−-- & at 404 ?~ Inline (mempty & description .~ "Not found")
206+-- & at 404 ?~ "Not found"
206207-- :}
207208-- "{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
208209--
src/Data/Swagger/Internal.hs+18−2
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE DeriveDataTypeable #-}
3+{-# LANGUAGE DeriveFunctor #-}
34{-# LANGUAGE DeriveGeneric #-}
45{-# LANGUAGE DeriveTraversable #-}
56{-# LANGUAGE FlexibleContexts #-}
@@ -28,7 +29,7 @@ import Data.Map (Map)
2829import qualified Data.Map as Map
2930import Data.Monoid
3031import Data.Scientific (Scientific)
31−import Data.String (fromString)
32+import Data.String (IsString(..))
3233import Data.Text (Text)
3334import qualified Data.Text as Text
3435import GHC.Generics (Generic)
@@ -150,12 +151,18 @@ data License = License
150151 , _licenseUrl :: Maybe URL
151152 } deriving (Eq, Show, Generic, Data, Typeable)
152153
154+instance IsString License where
155+ fromString s = License (fromString s) Nothing
156+
153157-- | The host (name or ip) serving the API. It MAY include a port.
154158data Host = Host
155159 { _hostName :: HostName -- ^ Host name.
156160 , _hostPort :: Maybe PortNumber -- ^ Optional port.
157161 } deriving (Eq, Show, Generic, Typeable)
158162
163+instance IsString Host where
164+ fromString s = Host s Nothing
165+
159166hostConstr :: Constr
160167hostConstr = mkConstr hostDataType "Host" [] Prefix
161168
@@ -591,6 +598,9 @@ data Response = Response
591598 , _responseExamples :: Maybe Example
592599 } deriving (Eq, Show, Generic, Data, Typeable)
593600
601+instance IsString Response where
602+ fromString s = Response (fromString s) Nothing mempty Nothing
603+
594604type HeaderName = Text
595605
596606data Header = Header
@@ -689,6 +699,9 @@ data Tag = Tag
689699 , _tagExternalDocs :: Maybe ExternalDocs
690700 } deriving (Eq, Show, Generic, Data, Typeable)
691701
702+instance IsString Tag where
703+ fromString s = Tag (fromString s) Nothing Nothing
704+
692705-- | Allows referencing an external resource for extended documentation.
693706data ExternalDocs = ExternalDocs
694707 { -- | A short description of the target documentation.
@@ -707,7 +720,10 @@ newtype Reference = Reference { getReference :: Text }
707720data Referenced a
708721 = Ref Reference
709722 | Inline a
710− deriving (Eq, Show, Data, Typeable)
723+ deriving (Eq, Show, Functor, Data, Typeable)
724+
725+instance IsString a => IsString (Referenced a) where
726+ fromString = Inline . fromString
711727
712728newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
713729
src/Data/Swagger/Lens.hs+2
@@ -48,6 +48,8 @@ makeFields ''ExternalDocs
4848makePrisms ''ParamAnySchema
4949-- ** 'SecuritySchemeType' prisms
5050makePrisms ''SecuritySchemeType
51+-- ** 'Referenced' prisms
52+makePrisms ''Referenced
5153
5254-- ** 'SwaggerItems' prisms
5355
test/Data/SwaggerSpec.hs+6−5
@@ -98,7 +98,8 @@ contactExampleJSON = [aesonQQ|
9898-- =======================================================================
9999
100100licenseExample :: License
101−licenseExample = License "Apache 2.0" (Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html"))
101+licenseExample = "Apache 2.0"
102+ & url ?~ URL "http://www.apache.org/licenses/LICENSE-2.0.html"
102103
103104licenseExampleJSON :: Value
104105licenseExampleJSON = [aesonQQ|
@@ -139,9 +140,8 @@ operationExample = mempty
139140 & schema .~ ParamOther (stringSchema ParamFormData)
140141 ]
141142
142− & responses .~ (mempty & responses .~
143− [ (200, Inline (mempty & description .~ "Pet updated."))
144− , (405, Inline (mempty & description .~ "Invalid input")) ])
143+ & at 200 ?~ "Pet updated."
144+ & at 405 ?~ "Invalid input"
145145 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
146146 where
147147 stringSchema :: ParamLocation -> ParamOtherSchema
@@ -479,7 +479,8 @@ swaggerExample = mempty
479479 & info .~ (mempty
480480 & version .~ "1.0"
481481 & title .~ "Todo API"
482− & license ?~ License "MIT" (Just (URL "http://mit.com"))
482+ & license ?~ "MIT"
483+ & license._Just.url ?~ URL "http://mit.com"
483484 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
484485 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
485486 & at 200 ?~ Inline (mempty