diff --git a/exe-rel8-import/Main.hs b/exe-rel8-import/Main.hs index f1fb0961..b32b67e1 100644 --- a/exe-rel8-import/Main.hs +++ b/exe-rel8-import/Main.hs @@ -4,7 +4,6 @@ {-# language DeriveGeneric #-} {-# language DerivingStrategies #-} {-# language DuplicateRecordFields #-} -{-# language DisambiguateRecordFields #-} {-# language GeneralizedNewtypeDeriving #-} {-# language LambdaCase #-} {-# language NamedFieldPuns #-} @@ -179,11 +178,11 @@ main = do , .. } - putStrLn $ HS.prettyPrint $ tablesToModule tables + putStrLn $ HS.prettyPrint $ tablesToModule schema tables -tablesToModule :: [Table Result] -> HS.Module () -tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls +tablesToModule :: Text -> [Table Result] -> HS.Module () +tablesToModule nameOfDatabaseSchema tables = HS.Module () Nothing pragmas imports allTableDecls where pragmas = [ deriveGeneric, deriveAnyClass, derivingStrategies, overloadedStrings ] where @@ -211,6 +210,27 @@ tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls where pascalName = HS.Ident () $ pascal $ unpack name + field Attribute{ attribute, typ } = (fieldDecl, fieldColumnMapping) + where + fieldDecl = HS.FieldDecl () [fieldName] $ columnF columnType + + fieldColumnMapping = HS.FieldUpdate () (HS.UnQual () fieldName) columnName + + fieldName = HS.Ident () $ camel $ unpack $ attname attribute + + columnType = + HS.TyCon () $ HS.UnQual () $ HS.Ident () $ + maybe (pascal $ unpack $ typname typ) unpack (lookup (typname typ) typeMapping) + + columnF = HS.TyApp () (HS.TyApp () _Column f) + where + _Column = HS.TyCon () $ HS.UnQual () $ HS.Ident () "Column" + f = HS.TyVar () $ HS.Ident () "f" + + columnName = HS.Lit () $ HS.String () str str + where + str = unpack $ attname attribute + rel8able = HS.DataDecl () (HS.DataType ()) Nothing declHead [constructor] [derivingGeneric, derivingRel8able] where declHead = HS.DHApp () tyName f @@ -220,19 +240,7 @@ tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls constructor = HS.QualConDecl () Nothing Nothing conDecl where - conDecl = HS.RecDecl () pascalName $ map field columns - field Attribute{ attribute, typ } = HS.FieldDecl () [fieldName] $ columnF columnType - where - fieldName = HS.Ident () $ camel $ unpack $ attname attribute - - columnType = - HS.TyCon () $ HS.UnQual () $ HS.Ident () $ - maybe (pascal $ unpack $ typname typ) unpack (lookup (typname typ) typeMapping) - - columnF = HS.TyApp () (HS.TyApp () _Column f) - where - _Column = HS.TyCon () $ HS.UnQual () $ HS.Ident () "Column" - f = HS.TyVar () $ HS.Ident () "f" + conDecl = HS.RecDecl () pascalName $ map (fst . field) columns derivingGeneric = HS.Deriving () (Just (HS.DerivStock ())) [rule] where @@ -270,19 +278,12 @@ tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls HS.App () (HS.Con () (HS.UnQual () (HS.Ident () "Just"))) $ HS.Lit () $ HS.String () str str where - str = unpack name + str = unpack nameOfDatabaseSchema columnsField = HS.FieldUpdate () columnsName columnsRecord where columnsName = HS.UnQual () (HS.Ident () "columns") - columnsRecord = HS.RecConstr () (HS.UnQual () pascalName) $ map field columns - - field Attribute{ attribute, typ } = HS.FieldUpdate () (HS.UnQual () fieldName) columnName - where - fieldName = HS.Ident () $ camel $ unpack $ attname attribute - columnName = HS.Lit () $ HS.String () str str - where - str = unpack $ attname attribute + columnsRecord = HS.RecConstr () (HS.UnQual () pascalName) $ map (snd . field) columns typeMapping :: [(Text, Text)]