git.iko.soy/typeable/swagger2
on master help sign in
Simplify Data instances for SwaggerType and CollectionFormat
7d7bb50Nickolay Kudasov
src/Data/Swagger/Internal.hs+32−76
@@ -341,73 +341,41 @@ deriving instance Eq (SwaggerType t)
341deriving instance Show (SwaggerType t)
341deriving instance Show (SwaggerType t)
342deriving instance Typeable (SwaggerType t)
342deriving instance Typeable (SwaggerType t)
343
343
344−swaggerStringConstr :: DataType -> Constr
344+swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
345−swaggerStringConstr dt = mkConstr dt "SwaggerString" [] Prefix
345+swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
346
346
347−swaggerNumberConstr :: DataType -> Constr
347+swaggerTypeDataType :: Data (SwaggerType t) => [SwaggerType t] -> SwaggerType t -> DataType
348−swaggerNumberConstr dt = mkConstr dt "SwaggerNumber" [] Prefix
348+swaggerTypeDataType cons _ = mkDataType "Data.Swagger.SwaggerType" $ map swaggerTypeConstr cons
349
349
350−swaggerIntegerConstr :: DataType -> Constr
350+swaggerCommonTypes :: [SwaggerType t]
351−swaggerIntegerConstr dt = mkConstr dt "SwaggerInteger" [] Prefix
351+swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
352
352
353−swaggerBooleanConstr :: DataType -> Constr
353+swaggerParamTypes :: [SwaggerType ParamOtherSchema]
354−swaggerBooleanConstr dt = mkConstr dt "SwaggerBoolean" [] Prefix
354+swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
355
355
356−swaggerArrayConstr :: DataType -> Constr
356+swaggerSchemaTypes :: [SwaggerType Schema]
357−swaggerArrayConstr dt = mkConstr dt "SwaggerArray" [] Prefix
357+swaggerSchemaTypes = swaggerCommonTypes ++ [SwaggerNull, SwaggerObject]
358−
359−swaggerFileConstr :: DataType -> Constr
360−swaggerFileConstr dt = mkConstr dt "SwaggerFile" [] Prefix
361−
362−swaggerNullConstr :: DataType -> Constr
363−swaggerNullConstr dt = mkConstr dt "SwaggerNull" [] Prefix
364−
365−swaggerObjectConstr :: DataType -> Constr
366−swaggerObjectConstr dt = mkConstr dt "SwaggerObject" [] Prefix
367−
368−swaggerTypeDataType :: [DataType -> Constr] -> DataType
369−swaggerTypeDataType extra = dt
370− where
371− dt = mkDataType "Data.Swagger.SwaggerType" $ map ($ dt) $
372− [ swaggerStringConstr, swaggerNumberConstr, swaggerIntegerConstr, swaggerBooleanConstr , swaggerArrayConstr ]
373− ++ extra
374−
375−swaggerTypeToConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
376−swaggerTypeToConstr t = case t of
377− SwaggerString -> swaggerStringConstr dt
378− SwaggerNumber -> swaggerNumberConstr dt
379− SwaggerInteger -> swaggerIntegerConstr dt
380− SwaggerBoolean -> swaggerBooleanConstr dt
381− SwaggerArray -> swaggerArrayConstr dt
382− SwaggerFile -> swaggerFileConstr dt
383− SwaggerNull -> swaggerNullConstr dt
384− SwaggerObject -> swaggerObjectConstr dt
385− where
386− dt = dataTypeOf t
387
358
388instance Data (SwaggerType Items) where
359instance Data (SwaggerType Items) where
389− gunfold = gunfoldEnum "SwaggerType Items" $ zip [1..]
360+ gunfold = gunfoldEnum "SwaggerType Items" $ zip [1..] swaggerCommonTypes
390− [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
361+ toConstr = swaggerTypeConstr
391− toConstr = swaggerTypeToConstr
392− dataTypeOf _ = swaggerTypeDataType []
362+ dataTypeOf = swaggerTypeDataType swaggerCommonTypes
393
363
394instance Data (SwaggerType Header) where
364instance Data (SwaggerType Header) where
395− gunfold = gunfoldEnum "SwaggerType Header" $ zip [1..]
365+ gunfold = gunfoldEnum "SwaggerType Header" $ zip [1..] swaggerCommonTypes
396− [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
366+ toConstr = swaggerTypeConstr
397− toConstr = swaggerTypeToConstr
398− dataTypeOf _ = swaggerTypeDataType []
367+ dataTypeOf = swaggerTypeDataType swaggerCommonTypes
399
368
400instance Data (SwaggerType ParamOtherSchema) where
369instance Data (SwaggerType ParamOtherSchema) where
401− gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" $ zip [1..]
370+ gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" $ zip [1..] swaggerParamTypes
402− [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray, SwaggerFile]
371+ toConstr = swaggerTypeConstr
403− toConstr = swaggerTypeToConstr
404− dataTypeOf _ = swaggerTypeDataType [swaggerFileConstr]
372+ dataTypeOf = swaggerTypeDataType swaggerParamTypes
405
373
406instance Data (SwaggerType Schema) where
374instance Data (SwaggerType Schema) where
407− gunfold = gunfoldEnum "SwaggerType Schema" $ zip [1, 2, 3, 4, 5, 7, 8]
375+ gunfold = gunfoldEnum "SwaggerType Schema" $ zip ns swaggerSchemaTypes
408− [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
376+ where ns = [1, 2, 3, 4, 5, 7, 8] -- skipping constructor #6 here
409− toConstr = swaggerTypeToConstr
377+ toConstr = swaggerTypeConstr
410− dataTypeOf _ = swaggerTypeDataType [swaggerNullConstr, swaggerObjectConstr]
378+ dataTypeOf = swaggerTypeDataType swaggerSchemaTypes
411
379
412data ParamLocation
380data ParamLocation
413 = -- | Parameters that are appended to the URL.
381 = -- | Parameters that are appended to the URL.
@@ -450,31 +418,19 @@ deriving instance Eq (CollectionFormat t)
450deriving instance Show (CollectionFormat t)
418deriving instance Show (CollectionFormat t)
451deriving instance Typeable (CollectionFormat t)
419deriving instance Typeable (CollectionFormat t)
452
420
453−collectionCsvConstr :: Constr
421+collectionFormatConstr :: CollectionFormat t -> Constr
454−collectionCsvConstr = mkConstr collectionFormatDataType "CollectionCSV" [] Prefix
455−
456−collectionSsvConstr :: Constr
457−collectionSsvConstr = mkConstr collectionFormatDataType "CollectionSSV" [] Prefix
458−
459−collectionTsvConstr :: Constr
460−collectionTsvConstr = mkConstr collectionFormatDataType "CollectionTSV" [] Prefix
422+collectionFormatConstr cf = mkConstr collectionFormatDataType (show cf) [] Prefix
461−
462−collectionPipesConstr :: Constr
463−collectionPipesConstr = mkConstr collectionFormatDataType "CollectionPipes" [] Prefix
464
423
465collectionFormatDataType :: DataType
424collectionFormatDataType :: DataType
466−collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat"
425+collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
467− [ collectionCsvConstr, collectionSsvConstr, collectionTsvConstr, collectionPipesConstr ]
426+ map collectionFormatConstr collectionCommonFormats
468
427
469−instance {-# OVERLAPPABLE #-} Data t => Data (CollectionFormat t) where
428+collectionCommonFormats :: [CollectionFormat t]
470− gunfold = gunfoldEnum "CollectionFormat" $ zip [1..]
471− [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
429+collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
472−
473− toConstr CollectionCSV = collectionCsvConstr
474− toConstr CollectionSSV = collectionSsvConstr
475− toConstr CollectionTSV = collectionTsvConstr
476− toConstr CollectionPipes = collectionPipesConstr
477
430
431+instance {-# OVERLAPPABLE #-} Data t => Data (CollectionFormat t) where
432+ gunfold = gunfoldEnum "CollectionFormat" $ zip [1..] collectionCommonFormats
433+ toConstr = collectionFormatConstr
478 dataTypeOf _ = collectionFormatDataType
434 dataTypeOf _ = collectionFormatDataType
479
435
480deriving instance {-# OVERLAPPING #-} Data (CollectionFormat Param)
436deriving instance {-# OVERLAPPING #-} Data (CollectionFormat Param)