on master help sign in
Add a more descriptive error in deriveSerializeWith
98a6b06Adithya Kumar
core/src/Streamly/Internal/Data/Serialize/TH.hs+26−9
@@ -402,7 +402,7 @@ deriveSerializeWith conf mDecs = do
402 dec <- mDecs
402 dec <- mDecs
403 case dec of
403 case dec of
404 [inst@(InstanceD _ _ headTy [])] -> do
404 [inst@(InstanceD _ _ headTy [])] -> do
405− dt <- reifyDataType (getMainTypeName headTy)
405+ dt <- reifyDataType (getMainTypeName dec headTy)
406 let cons = dtCons dt
406 let cons = dtCons dt
407 deriveSerializeInternal conf headTy cons (next inst)
407 deriveSerializeInternal conf headTy cons (next inst)
408 _ -> errorUnsupported
408 _ -> errorUnsupported
@@ -412,14 +412,31 @@ deriveSerializeWith conf mDecs = do
412 pure [InstanceD mo preds headTy methods]
412 pure [InstanceD mo preds headTy methods]
413 next _ _ = errorUnsupported
413 next _ _ = errorUnsupported
414
414
415+ errorMessage dec =
416+ error $ unlines
417+ [ "Error: deriveSerializeWith:"
418+ , ""
419+ , ">> " ++ pprint dec
420+ , ""
421+ , "The above is not a valid instance declaration."
422+ , "Any haskell instance declaration without a body is valid."
423+ , ""
424+ , "Examples:"
425+ , "instance Serialize (Proxy a)"
426+ , "instance Serialize a => Serialize (Identity a)"
427+ , "instance Serialize (TableT Identity)"
428+ ]
429+
415− getMainTypeName = go . unwrap
430+ getMainTypeName dec = go . unwrap
416−
431+
432+ where
433+
417− unwrap (AppT (ConT _) r) = r
434+ unwrap (AppT (ConT _) r) = r
418− unwrap _ = error "getMainTypeName.unwrap: Undefined instance declaration."
435+ unwrap _ = errorMessage dec
419−
436+
420− go (ConT nm) = nm
437+ go (ConT nm) = nm
421− go (AppT l _) = go l
438+ go (AppT l _) = go l
422− go _ = error "getMainTypeName.go: Undefined instance declaration."
439+ go _ = errorMessage dec
423
440
424
441
425-- | Template haskell helper to create instances of 'Serialize' automatically.
442-- | Template haskell helper to create instances of 'Serialize' automatically.