git.iko.soy/typeable/swagger2
on master help sign in
Use a single DataType for all Data SwaggerType instances

This is necessary to forbid (at runtime) using file as null: >>> fromConstr (toConstr (SwaggerFile :: SwaggerType ParamOtherSchema)) :: SwaggerType Schema SwaggerNull It also simplifies Data instances a bit.

commitdd64c84716a8c26fcda8937ca6c91f5462025c5d authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateFri 18 Dec 2015, 23:56 (11 years ago) parent7e335bd
2 files changed+16−13
src/Data/Swagger/Internal.hs+14−11 src/Data/Swagger/Internal/Utils.hs+2−2
src/Data/Swagger/Internal.hs+14−11
@@ -344,8 +344,8 @@ deriving instance Typeable (SwaggerType t)
344344swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
345345swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
346346
347−swaggerTypeDataType :: Data (SwaggerType t) => [SwaggerType t] -> SwaggerType t -> DataType
348−swaggerTypeDataType cons _ = mkDataType "Data.Swagger.SwaggerType" $ map swaggerTypeConstr cons
347+swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
348+swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
349349
350350swaggerCommonTypes :: [SwaggerType t]
351351swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
@@ -354,23 +354,26 @@ swaggerParamTypes :: [SwaggerType ParamOtherSchema]
354354swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
355355
356356swaggerSchemaTypes :: [SwaggerType Schema]
357−swaggerSchemaTypes = swaggerCommonTypes ++ [SwaggerNull, SwaggerObject]
357+swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
358+
359+swaggerTypeConstrs :: [Constr]
360+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
361+ ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
358362
359363instance {-# OVERLAPPABLE #-} Typeable t => Data (SwaggerType t) where
360− gunfold = gunfoldEnum "SwaggerType" $ zip [1..] swaggerCommonTypes
364+ gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
361365 toConstr = swaggerTypeConstr
362− dataTypeOf = swaggerTypeDataType swaggerCommonTypes
366+ dataTypeOf = swaggerTypeDataType
363367
364368instance {-# OVERLAPPING #-} Data (SwaggerType ParamOtherSchema) where
365− gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" $ zip [1..] swaggerParamTypes
369+ gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
366370 toConstr = swaggerTypeConstr
367− dataTypeOf = swaggerTypeDataType swaggerParamTypes
371+ dataTypeOf = swaggerTypeDataType
368372
369373instance {-# OVERLAPPING #-} Data (SwaggerType Schema) where
370− gunfold = gunfoldEnum "SwaggerType Schema" $ zip ns swaggerSchemaTypes
371− where ns = [1, 2, 3, 4, 5, 7, 8] -- skipping constructor #6 here
374+ gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
372375 toConstr = swaggerTypeConstr
373− dataTypeOf = swaggerTypeDataType swaggerSchemaTypes
376+ dataTypeOf = swaggerTypeDataType
374377
375378data ParamLocation
376379 = -- | Parameters that are appended to the URL.
@@ -424,7 +427,7 @@ collectionCommonFormats :: [CollectionFormat t]
424427collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
425428
426429instance {-# OVERLAPPABLE #-} Data t => Data (CollectionFormat t) where
427− gunfold = gunfoldEnum "CollectionFormat" $ zip [1..] collectionCommonFormats
430+ gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
428431 toConstr = collectionFormatConstr
429432 dataTypeOf _ = collectionFormatDataType
430433
src/Data/Swagger/Internal/Utils.hs+2−2
@@ -21,8 +21,8 @@ import GHC.Generics
2121import Language.Haskell.TH
2222import Text.Read (readMaybe)
2323
24−gunfoldEnum :: String -> [(ConIndex, a)] -> (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c a
25−gunfoldEnum tname xs _k z c = case lookup (constrIndex c) xs of
24+gunfoldEnum :: String -> [a] -> (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c a
25+gunfoldEnum tname xs _k z c = case lookup (constrIndex c) (zip [1..] xs) of
2626 Just x -> z x
2727 Nothing -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type " ++ tname ++ "."
2828