From 217f64c21dd5f12f7a662584950dd9e363b65f34 Mon Sep 17 00:00:00 2001 From: Kei Hibino Date: Fri, 19 Sep 2014 18:12:57 +0900 Subject: [PATCH] Export template of record width expression. --- persistable-record/src/Database/Record/TH.hs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/persistable-record/src/Database/Record/TH.hs b/persistable-record/src/Database/Record/TH.hs index f001d4c1..a665772e 100644 --- a/persistable-record/src/Database/Record/TH.hs +++ b/persistable-record/src/Database/Record/TH.hs @@ -41,6 +41,8 @@ module Database.Record.TH ( makeRecordPersistableWithSqlTypeDefaultFromDefined, definePersistableWidthInstance, + recordWidthTemplate, + defineRecordParser, defineRecordPrinter, @@ -173,16 +175,21 @@ derivingData = conCamelcaseName "Data" derivingTypable :: ConName derivingTypable = conCamelcaseName "Typable" +-- | Record type width expression template. +recordWidthTemplate :: TypeQ -- ^ Record type constructor. + -> ExpQ -- ^ Expression to get record width. +recordWidthTemplate ty = + [| runPersistableRecordWidth + $(sigE [| persistableWidth |] [t| PersistableRecordWidth $(ty) |]) + |] + -- | 'PersistableWidth' instance declaration. definePersistableWidthInstance :: TypeQ -- ^ Record type constructor. -> [TypeQ] -- ^ Types of record columns. -> Q [Dec] -- ^ Declaration of 'PersistableWidth' instance. definePersistableWidthInstance typeCon tys = do - let wdt ty = [| runPersistableRecordWidth - $(sigE [| persistableWidth |] [t| PersistableRecordWidth $(ty) |]) - |] [d| instance PersistableWidth $typeCon where - persistableWidth = unsafePersistableRecordWidth $ foldl' (+) 0 $(listE $ map wdt tys) + persistableWidth = unsafePersistableRecordWidth $ foldl' (+) 0 $(listE $ map recordWidthTemplate tys) |] -- | Record type declaration template.