git.iko.soy/typeable/openapi3
on master help sign in
Replace tuples with NamedSchema data in Data.Swagger.Internal.Schema
commit5beb804b9035638a8df091ed831025ba1d0103bd authorCatherine Galkina <catherine@getshoptv.com> dateThu 21 Jan 2016, 14:31 (11 years ago) parentacee6c7
3 files changed+17−17
src/Data/Swagger.hs+1 src/Data/Swagger/Internal/Schema.hs+16−16 src/Data/Swagger/Schema.hs−1
src/Data/Swagger.hs+1
@@ -60,6 +60,7 @@ module Data.Swagger (
6060 -- ** Schemas
6161 ParamSchema(..),
6262 Schema(..),
63+ NamedSchema(..),
6364 SwaggerItems(..),
6465 Xml(..),
6566
src/Data/Swagger/Internal/Schema.hs+16−16
@@ -49,19 +49,19 @@ import Data.Swagger.SchemaOptions
4949type Definitions = HashMap T.Text Schema
5050
5151unnamed :: Schema -> NamedSchema
52−unnamed schema = (Nothing, schema)
52+unnamed schema = NamedSchema Nothing schema
5353
5454named :: T.Text -> Schema -> NamedSchema
55−named name schema = (Just name, schema)
55+named name schema = NamedSchema (Just name) schema
5656
5757plain :: Schema -> Declare Definitions NamedSchema
5858plain = pure . unnamed
5959
6060unname :: NamedSchema -> NamedSchema
61−unname (_, schema) = (Nothing, schema)
61+unname (NamedSchema _ schema) = unnamed schema
6262
6363rename :: Maybe T.Text -> NamedSchema -> NamedSchema
64−rename name (_, schema) = (name, schema)
64+rename name (NamedSchema _ schema) = NamedSchema name schema
6565
6666-- | Convert a type into @'Schema'@.
6767--
@@ -116,7 +116,7 @@ class ToSchema a where
116116
117117-- | Convert a type into a schema and declare all used schema definitions.
118118declareSchema :: ToSchema a => proxy a -> Declare Definitions Schema
119−declareSchema = fmap snd . declareNamedSchema
119+declareSchema = fmap _namedSchemaSchema . declareNamedSchema
120120
121121-- | Convert a type into an optionally named schema.
122122--
@@ -136,7 +136,7 @@ toNamedSchema = undeclare . declareNamedSchema
136136-- >>> schemaName (Proxy :: Proxy UTCTime)
137137-- Just "UTCTime"
138138schemaName :: ToSchema a => proxy a -> Maybe T.Text
139−schemaName = fst . toNamedSchema
139+schemaName = _namedSchemaName . toNamedSchema
140140
141141-- | Convert a type into a schema.
142142--
@@ -146,7 +146,7 @@ schemaName = fst . toNamedSchema
146146-- >>> encode $ toSchema (Proxy :: Proxy [Day])
147147-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
148148toSchema :: ToSchema a => proxy a -> Schema
149−toSchema = snd . toNamedSchema
149+toSchema = _namedSchemaSchema . toNamedSchema
150150
151151-- | Convert a type into a referenced schema if possible.
152152-- Only named schemas can be referenced, nameless schemas are inlined.
@@ -169,7 +169,7 @@ toSchemaRef = undeclare . declareSchemaRef
169169declareSchemaRef :: ToSchema a => proxy a -> Declare Definitions (Referenced Schema)
170170declareSchemaRef proxy = do
171171 case toNamedSchema proxy of
172− (Just name, schema) -> do
172+ NamedSchema (Just name) schema -> do
173173 -- This check is very important as it allows generically
174174 -- derive used definitions for recursive schemas.
175175 -- Lazy Declare monad allows toNamedSchema to ignore
@@ -289,7 +289,7 @@ instance ToSchema a => ToSchema (Maybe a) where
289289instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
290290
291291instance ToSchema () where
292− declareNamedSchema _ = pure (Nothing, nullarySchema)
292+ declareNamedSchema _ = pure (NamedSchema Nothing nullarySchema)
293293
294294instance (ToSchema a, ToSchema b) => ToSchema (a, b)
295295instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
@@ -382,11 +382,11 @@ genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
382382 , Generic a, Rep a ~ D1 d f, Datatype d)
383383 => SchemaOptions -> proxy a -> NamedSchema
384384genericToNamedSchemaBoundedIntegral opts proxy
385− = (gdatatypeSchemaName opts (Proxy :: Proxy d), toSchemaBoundedIntegral proxy)
385+ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
386386
387387-- | A configurable generic @'Schema'@ creator.
388388genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare Definitions Schema
389−genericDeclareSchema opts proxy = snd <$> genericDeclareNamedSchema opts proxy
389+genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
390390
391391-- | A configurable generic @'NamedSchema'@ creator.
392392-- This function applied to @'defaultSchemaOptions'@
@@ -406,7 +406,7 @@ gdatatypeSchemaName opts _ = case name of
406406paramSchemaToNamedSchema :: forall a d f proxy.
407407 (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
408408 => SchemaOptions -> proxy a -> NamedSchema
409−paramSchemaToNamedSchema opts proxy = (gdatatypeSchemaName opts (Proxy :: Proxy d), paramSchemaToSchema proxy)
409+paramSchemaToNamedSchema opts proxy = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (paramSchemaToSchema proxy)
410410
411411-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
412412paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
@@ -422,11 +422,11 @@ gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
422422gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
423423
424424gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare Definitions Schema
425−gdeclareSchema opts proxy = snd <$> gdeclareNamedSchema opts proxy mempty
425+gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
426426
427427instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
428428 gdeclareNamedSchema opts _ schema = do
429− (_, gschema) <- gdeclareNamedSchema opts (Proxy :: Proxy g) schema
429+ NamedSchema _ gschema <- gdeclareNamedSchema opts (Proxy :: Proxy g) schema
430430 gdeclareNamedSchema opts (Proxy :: Proxy f) gschema
431431
432432instance (Datatype d, GToSchema f) => GToSchema (D1 d f) where
@@ -451,14 +451,14 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
451451 declare defs
452452 return (unnamed schema)
453453 where
454− (defs, (_, schema)) = runDeclare recordSchema mempty
454+ (defs, NamedSchema _ schema) = runDeclare recordSchema mempty
455455 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
456456 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
457457
458458gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definitions (Referenced Schema)
459459gdeclareSchemaRef opts proxy = do
460460 case gtoNamedSchema opts proxy of
461− (Just name, schema) -> do
461+ NamedSchema (Just name) schema -> do
462462 -- This check is very important as it allows generically
463463 -- derive used definitions for recursive schemas.
464464 -- Lazy Declare monad allows toNamedSchema to ignore
src/Data/Swagger/Schema.hs−1
@@ -10,7 +10,6 @@ module Data.Swagger.Schema (
1010 -- * Encoding
1111 ToSchema(..),
1212 Definitions,
13− NamedSchema,
1413 declareSchema,
1514 declareSchemaRef,
1615 toSchema,