git.iko.soy/typeable/swagger2
on master help sign in
use MonadDeclare in place of Declare

This makes the functions that generate schemas useful in more contexts. This could break clients that implements ToSchema if they somehow use Declare specifically, however most implementations should simply continue to compile (as all the instances here do).

commite9fcd21bf606430218a661daa70063eceb37d258 authormoss <moss@tvisioninsights.com> dateMon 24 Apr 2017, 11:42 (9 years ago) parent1516a07
1 file changed+17−17
src/Data/Swagger/Internal/Schema.hs+17−17
src/Data/Swagger/Internal/Schema.hs+17−17
@@ -82,7 +82,7 @@ unnamed schema = NamedSchema Nothing schema
8282named :: T.Text -> Schema -> NamedSchema
8383named name schema = NamedSchema (Just name) schema
8484
85−plain :: Schema -> Declare (Definitions Schema) NamedSchema
85+plain :: MonadDeclare (Definitions Schema) m => Schema -> m NamedSchema
8686plain = pure . unnamed
8787
8888unname :: NamedSchema -> NamedSchema
@@ -138,12 +138,12 @@ class ToSchema a where
138138 -- together with all used definitions.
139139 -- Note that the schema itself is included in definitions
140140 -- only if it is recursive (and thus needs its definition in scope).
141− declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
142− default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
141+ declareNamedSchema :: MonadDeclare (Definitions Schema) m => proxy a -> m NamedSchema
142+ default declareNamedSchema :: (Generic a, GToSchema (Rep a), MonadDeclare (Definitions Schema) m) => proxy a -> m NamedSchema
143143 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
144144
145145-- | Convert a type into a schema and declare all used schema definitions.
146−declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
146+declareSchema :: (ToSchema a, MonadDeclare (Definitions Schema) m) => proxy a -> m Schema
147147declareSchema = fmap _namedSchemaSchema . declareNamedSchema
148148
149149-- | Convert a type into an optionally named schema.
@@ -198,7 +198,7 @@ toSchemaRef = undeclare . declareSchemaRef
198198-- Schema definitions are typically declared for every referenced schema.
199199-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
200200-- will be declared (regardless of whether it is recusive or not).
201−declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
201+declareSchemaRef :: (ToSchema a, MonadDeclare (Definitions Schema) m) => proxy a -> m (Referenced Schema)
202202declareSchemaRef proxy = do
203203 case toNamedSchema proxy of
204204 NamedSchema (Just name) schema -> do
@@ -405,7 +405,7 @@ sketchStrictSchema = go . toJSON
405405 names = HashMap.keys o
406406
407407class GToSchema (f :: * -> *) where
408− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
408+ gdeclareNamedSchema :: (MonadDeclare (Definitions Schema) m) => SchemaOptions -> proxy f -> Schema -> m NamedSchema
409409
410410instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
411411 declareNamedSchema _ = do
@@ -583,14 +583,14 @@ genericToNamedSchemaBoundedIntegral opts proxy
583583 = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
584584
585585-- | A configurable generic @'Schema'@ creator.
586−genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
586+genericDeclareSchema :: (Generic a, GToSchema (Rep a), MonadDeclare (Definitions Schema) m) => SchemaOptions -> proxy a -> m Schema
587587genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
588588
589589-- | A configurable generic @'NamedSchema'@ creator.
590590-- This function applied to @'defaultSchemaOptions'@
591591-- is used as the default for @'declareNamedSchema'@
592592-- when the type is an instance of @'Generic'@.
593−genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
593+genericDeclareNamedSchema :: forall a m proxy. (Generic a, GToSchema (Rep a), MonadDeclare (Definitions Schema) m) => SchemaOptions -> proxy a -> m NamedSchema
594594genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
595595
596596gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
@@ -618,7 +618,7 @@ nullarySchema = mempty
618618gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
619619gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
620620
621−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
621+gdeclareSchema :: (GToSchema f, MonadDeclare (Definitions Schema) m) => SchemaOptions -> proxy f -> m Schema
622622gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
623623
624624instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -652,7 +652,7 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
652652 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
653653 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
654654
655−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
655+gdeclareSchemaRef :: (GToSchema a, MonadDeclare (Definitions Schema) m) => SchemaOptions -> proxy a -> m (Referenced Schema)
656656gdeclareSchemaRef opts proxy = do
657657 case gtoNamedSchema opts proxy of
658658 NamedSchema (Just name) schema -> do
@@ -676,8 +676,8 @@ appendItem x Nothing = Just (SwaggerItemsArray [x])
676676appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (xs ++ [x]))
677677appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
678678
679−withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) =>
680− SchemaOptions -> proxy s f -> Bool -> Schema -> Declare (Definitions Schema) Schema
679+withFieldSchema :: forall proxy s f m. (Selector s, GToSchema f, MonadDeclare (Definitions Schema) m) =>
680+ SchemaOptions -> proxy s f -> Bool -> Schema -> m Schema
681681withFieldSchema opts _ isRequiredField schema = do
682682 ref <- gdeclareSchemaRef opts (Proxy :: Proxy f)
683683 return $
@@ -713,11 +713,12 @@ instance OVERLAPPABLE_ ToSchema c => GToSchema (K1 i c) where
713713instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
714714 gdeclareNamedSchema = gdeclareNamedSumSchema
715715
716−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
716+gdeclareNamedSumSchema :: (GSumToSchema f, MonadDeclare (Definitions Schema) m) => SchemaOptions -> proxy f -> Schema -> m NamedSchema
717717gdeclareNamedSumSchema opts proxy s
718718 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
719719 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
720720 where
721+ declareSumSchema :: MonadDeclare (Definitions Schema) m => WriterT AllNullary m Schema
721722 declareSumSchema = gsumToSchema opts proxy s
722723 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
723724
@@ -728,7 +729,7 @@ gdeclareNamedSumSchema opts proxy s
728729type AllNullary = All
729730
730731class GSumToSchema f where
731− gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
732+ gsumToSchema :: MonadDeclare (Definitions Schema) m => SchemaOptions -> proxy f -> Schema -> WriterT AllNullary m Schema
732733
733734instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
734735 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
@@ -743,8 +744,8 @@ gsumConToSchemaWith ref opts _ schema = schema
743744 where
744745 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
745746
746−gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
747− SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
747+gsumConToSchema :: forall c f m proxy. (GToSchema (C1 c f), Constructor c, MonadDeclare (Definitions Schema) m) =>
748+ SchemaOptions -> proxy (C1 c f) -> Schema -> m Schema
748749gsumConToSchema opts proxy schema = do
749750 ref <- gdeclareSchemaRef opts proxy
750751 return $ gsumConToSchemaWith ref opts proxy schema
@@ -765,4 +766,3 @@ instance Constructor c => GSumToSchema (C1 c U1) where
765766data Proxy2 a b = Proxy2
766767
767768data Proxy3 a b c = Proxy3
768−