git.iko.soy/typeable/swagger2
on master help sign in
Replace tuples with NamedSchema data in Data.Swagger.Internal.Schema
5beb804Catherine Galkina
src/Data/Swagger.hs+1
@@ -60,6 +60,7 @@ module Data.Swagger (
60 -- ** Schemas
60 -- ** Schemas
61 ParamSchema(..),
61 ParamSchema(..),
62 Schema(..),
62 Schema(..),
63+ NamedSchema(..),
63 SwaggerItems(..),
64 SwaggerItems(..),
64 Xml(..),
65 Xml(..),
65
66
src/Data/Swagger/Internal/Schema.hs+16−16
@@ -49,19 +49,19 @@ import Data.Swagger.SchemaOptions
49type Definitions = HashMap T.Text Schema
49type Definitions = HashMap T.Text Schema
50
50
51unnamed :: Schema -> NamedSchema
51unnamed :: Schema -> NamedSchema
52−unnamed schema = (Nothing, schema)
52+unnamed schema = NamedSchema Nothing schema
53
53
54named :: T.Text -> Schema -> NamedSchema
54named :: T.Text -> Schema -> NamedSchema
55−named name schema = (Just name, schema)
55+named name schema = NamedSchema (Just name) schema
56
56
57plain :: Schema -> Declare Definitions NamedSchema
57plain :: Schema -> Declare Definitions NamedSchema
58plain = pure . unnamed
58plain = pure . unnamed
59
59
60unname :: NamedSchema -> NamedSchema
60unname :: NamedSchema -> NamedSchema
61−unname (_, schema) = (Nothing, schema)
61+unname (NamedSchema _ schema) = unnamed schema
62
62
63rename :: Maybe T.Text -> NamedSchema -> NamedSchema
63rename :: Maybe T.Text -> NamedSchema -> NamedSchema
64−rename name (_, schema) = (name, schema)
64+rename name (NamedSchema _ schema) = NamedSchema name schema
65
65
66-- | Convert a type into @'Schema'@.
66-- | Convert a type into @'Schema'@.
67--
67--
@@ -116,7 +116,7 @@ class ToSchema a where
116
116
117-- | Convert a type into a schema and declare all used schema definitions.
117-- | Convert a type into a schema and declare all used schema definitions.
118declareSchema :: ToSchema a => proxy a -> Declare Definitions Schema
118declareSchema :: ToSchema a => proxy a -> Declare Definitions Schema
119−declareSchema = fmap snd . declareNamedSchema
119+declareSchema = fmap _namedSchemaSchema . declareNamedSchema
120
120
121-- | Convert a type into an optionally named schema.
121-- | Convert a type into an optionally named schema.
122--
122--
@@ -136,7 +136,7 @@ toNamedSchema = undeclare . declareNamedSchema
136-- >>> schemaName (Proxy :: Proxy UTCTime)
136-- >>> schemaName (Proxy :: Proxy UTCTime)
137-- Just "UTCTime"
137-- Just "UTCTime"
138schemaName :: ToSchema a => proxy a -> Maybe T.Text
138schemaName :: ToSchema a => proxy a -> Maybe T.Text
139−schemaName = fst . toNamedSchema
139+schemaName = _namedSchemaName . toNamedSchema
140
140
141-- | Convert a type into a schema.
141-- | Convert a type into a schema.
142--
142--
@@ -146,7 +146,7 @@ schemaName = fst . toNamedSchema
146-- >>> encode $ toSchema (Proxy :: Proxy [Day])
146-- >>> encode $ toSchema (Proxy :: Proxy [Day])
147-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
147-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
148toSchema :: ToSchema a => proxy a -> Schema
148toSchema :: ToSchema a => proxy a -> Schema
149−toSchema = snd . toNamedSchema
149+toSchema = _namedSchemaSchema . toNamedSchema
150
150
151-- | Convert a type into a referenced schema if possible.
151-- | Convert a type into a referenced schema if possible.
152-- Only named schemas can be referenced, nameless schemas are inlined.
152-- Only named schemas can be referenced, nameless schemas are inlined.
@@ -169,7 +169,7 @@ toSchemaRef = undeclare . declareSchemaRef
169declareSchemaRef :: ToSchema a => proxy a -> Declare Definitions (Referenced Schema)
169declareSchemaRef :: ToSchema a => proxy a -> Declare Definitions (Referenced Schema)
170declareSchemaRef proxy = do
170declareSchemaRef proxy = do
171 case toNamedSchema proxy of
171 case toNamedSchema proxy of
172− (Just name, schema) -> do
172+ NamedSchema (Just name) schema -> do
173 -- This check is very important as it allows generically
173 -- This check is very important as it allows generically
174 -- derive used definitions for recursive schemas.
174 -- derive used definitions for recursive schemas.
175 -- Lazy Declare monad allows toNamedSchema to ignore
175 -- Lazy Declare monad allows toNamedSchema to ignore
@@ -289,7 +289,7 @@ instance ToSchema a => ToSchema (Maybe a) where
289instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
289instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
290
290
291instance ToSchema () where
291instance ToSchema () where
292− declareNamedSchema _ = pure (Nothing, nullarySchema)
292+ declareNamedSchema _ = pure (NamedSchema Nothing nullarySchema)
293
293
294instance (ToSchema a, ToSchema b) => ToSchema (a, b)
294instance (ToSchema a, ToSchema b) => ToSchema (a, b)
295instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
295instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
@@ -382,11 +382,11 @@ genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
382 , Generic a, Rep a ~ D1 d f, Datatype d)
382 , Generic a, Rep a ~ D1 d f, Datatype d)
383 => SchemaOptions -> proxy a -> NamedSchema
383 => SchemaOptions -> proxy a -> NamedSchema
384genericToNamedSchemaBoundedIntegral opts proxy
384genericToNamedSchemaBoundedIntegral opts proxy
385− = (gdatatypeSchemaName opts (Proxy :: Proxy d), toSchemaBoundedIntegral proxy)
385+ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
386
386
387-- | A configurable generic @'Schema'@ creator.
387-- | A configurable generic @'Schema'@ creator.
388genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare Definitions Schema
388genericDeclareSchema :: (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
390
390
391-- | A configurable generic @'NamedSchema'@ creator.
391-- | A configurable generic @'NamedSchema'@ creator.
392-- This function applied to @'defaultSchemaOptions'@
392-- This function applied to @'defaultSchemaOptions'@
@@ -406,7 +406,7 @@ gdatatypeSchemaName opts _ = case name of
406paramSchemaToNamedSchema :: forall a d f proxy.
406paramSchemaToNamedSchema :: forall a d f proxy.
407 (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
407 (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
408 => SchemaOptions -> proxy a -> NamedSchema
408 => 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)
410
410
411-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
411-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
412paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
412paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
@@ -422,11 +422,11 @@ gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
422gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
422gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
423
423
424gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare Definitions Schema
424gdeclareSchema :: 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
426
426
427instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
427instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
428 gdeclareNamedSchema opts _ schema = do
428 gdeclareNamedSchema opts _ schema = do
429− (_, gschema) <- gdeclareNamedSchema opts (Proxy :: Proxy g) schema
429+ NamedSchema _ gschema <- gdeclareNamedSchema opts (Proxy :: Proxy g) schema
430 gdeclareNamedSchema opts (Proxy :: Proxy f) gschema
430 gdeclareNamedSchema opts (Proxy :: Proxy f) gschema
431
431
432instance (Datatype d, GToSchema f) => GToSchema (D1 d f) where
432instance (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
451 declare defs
451 declare defs
452 return (unnamed schema)
452 return (unnamed schema)
453 where
453 where
454− (defs, (_, schema)) = runDeclare recordSchema mempty
454+ (defs, NamedSchema _ schema) = runDeclare recordSchema mempty
455 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
455 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
456 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
456 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
457
457
458gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definitions (Referenced Schema)
458gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definitions (Referenced Schema)
459gdeclareSchemaRef opts proxy = do
459gdeclareSchemaRef opts proxy = do
460 case gtoNamedSchema opts proxy of
460 case gtoNamedSchema opts proxy of
461− (Just name, schema) -> do
461+ NamedSchema (Just name) schema -> do
462 -- This check is very important as it allows generically
462 -- This check is very important as it allows generically
463 -- derive used definitions for recursive schemas.
463 -- derive used definitions for recursive schemas.
464 -- Lazy Declare monad allows toNamedSchema to ignore
464 -- Lazy Declare monad allows toNamedSchema to ignore
src/Data/Swagger/Schema.hs−1
@@ -10,7 +10,6 @@ module Data.Swagger.Schema (
10 -- * Encoding
10 -- * Encoding
11 ToSchema(..),
11 ToSchema(..),
12 Definitions,
12 Definitions,
13− NamedSchema,
14 declareSchema,
13 declareSchema,
15 declareSchemaRef,
14 declareSchemaRef,
16 toSchema,
15 toSchema,