From f3996cd6272bfbe79c40f6b138ea25729c4489a2 Mon Sep 17 00:00:00 2001 From: Aaron Snook Date: Tue, 3 Jan 2023 16:23:24 -0600 Subject: [PATCH 1/6] Remove purty from dependencies It isn't used in this package as far as I can tell, and we're sunsetting it. --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index b7a2a77..5e16c5e 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "purescript": "0.15.2", "purescript-language-server": "^0.16.6", "purescript-psa": "^0.8.2", - "purty": "7.0.0", "rimraf": "^2.6.2", "spago": "^0.20.8", "pscid": "2.9.3" From 46c98e201d2569a7fb66f9506ffd72f15a5935ab Mon Sep 17 00:00:00 2001 From: Aaron Snook Date: Wed, 4 Jan 2023 08:09:29 -0600 Subject: [PATCH 2/6] add purs-tidy --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 5e16c5e..f95bf53 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "purescript": "0.15.2", "purescript-language-server": "^0.16.6", "purescript-psa": "^0.8.2", + "purs-tidy": "0.9.2", "rimraf": "^2.6.2", "spago": "^0.20.8", "pscid": "2.9.3" From 92280401d19e7e1bed3bc3586e95fffa934ad7ee Mon Sep 17 00:00:00 2001 From: Aaron Snook Date: Wed, 4 Jan 2023 08:17:51 -0600 Subject: [PATCH 3/6] purs-tidy GetSchema --- test/GetSchema.purs | 143 ++++++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 58 deletions(-) diff --git a/test/GetSchema.purs b/test/GetSchema.purs index c4dac84..a43db85 100644 --- a/test/GetSchema.purs +++ b/test/GetSchema.purs @@ -1,72 +1,99 @@ -module Test.GetSchema where +module GetSchema + ( main + ) where import Prelude import Ccap.Codegen.Cst as Cst -import Ccap.Codegen.Database (tableModule) -import Ccap.Codegen.PrettyPrint (prettyPrint) -import Ccap.Codegen.Util (scrubEolSpaces) -import Control.Monad.Except (ExceptT(..), runExceptT) -import Data.Either (either) -import Data.Int as Int -import Data.Maybe (Maybe(..), fromMaybe) -import Data.Tuple (Tuple(..), uncurry) +import Ccap.Codegen.Database as Database +import Ccap.Codegen.GetSchemaConfig (GetSchemaConfig, getSchemaConfig) +import Ccap.Codegen.PrettyPrint as PrettyPrint +import Ccap.Codegen.Util (liftEffectSafely, processResult, scrubEolSpaces) +import Control.Monad.Except (ExceptT, except) +import Data.Either (Either(..), note) +import Data.Filterable (filter) +import Data.Foldable (traverse_) +import Data.Int (fromString) as Int +import Data.Maybe (Maybe(..)) +import Data.String as String +import Data.Traversable (for) import Database.PostgreSQL.Pool as Pool import Effect (Effect) import Effect.Aff (Aff, launchAff_) import Effect.Class (liftEffect) -import Node.Path (FilePath) -import Node.Process (lookupEnv) -import Test.Ccap.Codegen.Util (diffByLine, sourceCstTmpl) -import Test.Spec (Spec, describe, it) -import Test.Spec.Assertions (fail) -import Test.Spec.Reporter (consoleReporter) -import Test.Spec.Runner (runSpec) +import Effect.Class.Console as Console +import Options.Applicative as OptParse -main :: Effect Unit -main = launchAff_ $ runSpec [ consoleReporter ] specs +app :: GetSchemaConfig -> Effect Unit +app config = + launchAff_ + $ processResult do + let + checkMaybeString = filter (\s -> String.length s > 0) + + table = checkMaybeString config.table + poolConfig <- except (readPoolConfig config.database) + let + c = + { table + , poolConfig + , getSchemaConfig: config + } + fromDb <- dbModules c + traverse_ writeModule fromDb + where + readPoolConfig :: String -> Either String Pool.Configuration + readPoolConfig s = fromParts parts + where + parts = String.split (String.Pattern ":") s + + poolConfig db = (Pool.defaultConfiguration db) { idleTimeoutMillis = Just 500 } + + fromParts [ host, port, db, user ] = + portFromString port + <#> \p -> + (poolConfig db) + { host = Just host + , port = Just p + , user = Just user + } -caseTmplFile :: FilePath -caseTmplFile = "./test/resources/get-schema/Case.tmpl" + fromParts [ host, port, db, user, password ] = + portFromString port + <#> \p -> + (poolConfig db) + { host = Just host + , port = Just p + , user = Just user + , password = Just password + } -poolConfig :: Effect Pool.Configuration -poolConfig = do - host <- lookupEnv "DB_HOST" - port <- lookupEnv "DB_PORT" <#> (_ >>= Int.fromString) - db <- lookupEnv "DATABASE" - user <- lookupEnv "DB_USER" - password <- lookupEnv "DB_PASSWORD" - pure - $ (Pool.defaultConfiguration $ fromMaybe "cir" db) - { host = host - , port = port - , user = user - , password = password - } + fromParts _ = Left "Config parameter must be of the form :::: (password optional)" -stripImports :: Cst.Source Cst.Module -> Cst.Source Cst.Module -stripImports source = source { contents = source.contents { imports = [] } } + portFromString = note "Database port must be an integer" <<< Int.fromString -specs :: Spec Unit -specs = - describe "Case" do - it "fetches the case data correctly" do - results <- - runExceptT do - fileSource <- ExceptT <<< liftEffect $ sourceCstTmpl caseTmplFile - let - { scalaPkg, pursPkg } = fileSource.contents.exports - pool <- ExceptT <<< liftEffect <<< map pure $ poolConfig >>= Pool.new - dbModule <- - tableModule pool - { dbManagedColumns: Nothing - , scalaPkg - , pursPkg - } - "Case" - pure $ Tuple fileSource.contents dbModule - either fail (uncurry printAndDiff) results +dbModules :: Config -> ExceptT String Aff (Maybe Cst.Module) +dbModules config@{ getSchemaConfig: { dbManagedColumns, scalaPkg, pursPkg } } = do + pool <- liftEffect $ Pool.new config.poolConfig + if config.getSchemaConfig.domains then + Just <$> Database.domainModule pool { dbManagedColumns, scalaPkg, pursPkg } + else + for config.table $ Database.tableModule pool { dbManagedColumns, scalaPkg, pursPkg } -printAndDiff :: Cst.Module -> Cst.Module -> Aff Unit -printAndDiff x y = (print x) `diffByLine` (print y) +type Config = + { table :: Maybe String + , poolConfig :: Pool.Configuration + , getSchemaConfig :: GetSchemaConfig + } + +prependNotice :: String -> String +prependNotice = ("// This file is automatically generated from DB schema. Do not edit.\n" <> _) + +writeModule :: Cst.Module -> ExceptT String Aff Unit +writeModule = liftEffectSafely <<< print where - print = scrubEolSpaces <<< prettyPrint + print = Console.info <<< prependNotice <<< scrubEolSpaces <<< PrettyPrint.prettyPrint + +main :: Effect Unit +main = do + configuration <- OptParse.execParser $ OptParse.info getSchemaConfig (OptParse.fullDesc) + app configuration From 5bfe26ec01c69994ff951e96efa8a0389c284c04 Mon Sep 17 00:00:00 2001 From: Aaron Snook Date: Wed, 4 Jan 2023 08:23:37 -0600 Subject: [PATCH 4/6] purs-tidy --- src/GetSchema.purs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/GetSchema.purs b/src/GetSchema.purs index 742efcd..a43db85 100644 --- a/src/GetSchema.purs +++ b/src/GetSchema.purs @@ -51,21 +51,21 @@ app config = fromParts [ host, port, db, user ] = portFromString port <#> \p -> - (poolConfig db) - { host = Just host - , port = Just p - , user = Just user - } + (poolConfig db) + { host = Just host + , port = Just p + , user = Just user + } fromParts [ host, port, db, user, password ] = portFromString port <#> \p -> - (poolConfig db) - { host = Just host - , port = Just p - , user = Just user - , password = Just password - } + (poolConfig db) + { host = Just host + , port = Just p + , user = Just user + , password = Just password + } fromParts _ = Left "Config parameter must be of the form :::: (password optional)" @@ -79,11 +79,11 @@ dbModules config@{ getSchemaConfig: { dbManagedColumns, scalaPkg, pursPkg } } = else for config.table $ Database.tableModule pool { dbManagedColumns, scalaPkg, pursPkg } -type Config - = { table :: Maybe String - , poolConfig :: Pool.Configuration - , getSchemaConfig :: GetSchemaConfig - } +type Config = + { table :: Maybe String + , poolConfig :: Pool.Configuration + , getSchemaConfig :: GetSchemaConfig + } prependNotice :: String -> String prependNotice = ("// This file is automatically generated from DB schema. Do not edit.\n" <> _) From 20388d465762946cf9787bd338a95b03fc0ee673 Mon Sep 17 00:00:00 2001 From: Aaron Snook Date: Wed, 4 Jan 2023 08:24:30 -0600 Subject: [PATCH 5/6] purs-tidy --- test/Support.purs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Support.purs b/test/Support.purs index 779374a..aa6220e 100644 --- a/test/Support.purs +++ b/test/Support.purs @@ -2,7 +2,6 @@ module Test.Support where import Data.Newtype -newtype Newint - = Newint Int +newtype Newint = Newint Int derive instance newtypeNewint :: Newtype Newint _ From 5f1ff55d96e6148e80ef54aa5417f902cf48fee5 Mon Sep 17 00:00:00 2001 From: Aaron Snook Date: Wed, 4 Jan 2023 08:26:25 -0600 Subject: [PATCH 6/6] purs-tidy --- test/GetSchema.purs | 143 ++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 85 deletions(-) diff --git a/test/GetSchema.purs b/test/GetSchema.purs index a43db85..c4dac84 100644 --- a/test/GetSchema.purs +++ b/test/GetSchema.purs @@ -1,99 +1,72 @@ -module GetSchema - ( main - ) where +module Test.GetSchema where import Prelude import Ccap.Codegen.Cst as Cst -import Ccap.Codegen.Database as Database -import Ccap.Codegen.GetSchemaConfig (GetSchemaConfig, getSchemaConfig) -import Ccap.Codegen.PrettyPrint as PrettyPrint -import Ccap.Codegen.Util (liftEffectSafely, processResult, scrubEolSpaces) -import Control.Monad.Except (ExceptT, except) -import Data.Either (Either(..), note) -import Data.Filterable (filter) -import Data.Foldable (traverse_) -import Data.Int (fromString) as Int -import Data.Maybe (Maybe(..)) -import Data.String as String -import Data.Traversable (for) +import Ccap.Codegen.Database (tableModule) +import Ccap.Codegen.PrettyPrint (prettyPrint) +import Ccap.Codegen.Util (scrubEolSpaces) +import Control.Monad.Except (ExceptT(..), runExceptT) +import Data.Either (either) +import Data.Int as Int +import Data.Maybe (Maybe(..), fromMaybe) +import Data.Tuple (Tuple(..), uncurry) import Database.PostgreSQL.Pool as Pool import Effect (Effect) import Effect.Aff (Aff, launchAff_) import Effect.Class (liftEffect) -import Effect.Class.Console as Console -import Options.Applicative as OptParse +import Node.Path (FilePath) +import Node.Process (lookupEnv) +import Test.Ccap.Codegen.Util (diffByLine, sourceCstTmpl) +import Test.Spec (Spec, describe, it) +import Test.Spec.Assertions (fail) +import Test.Spec.Reporter (consoleReporter) +import Test.Spec.Runner (runSpec) -app :: GetSchemaConfig -> Effect Unit -app config = - launchAff_ - $ processResult do - let - checkMaybeString = filter (\s -> String.length s > 0) - - table = checkMaybeString config.table - poolConfig <- except (readPoolConfig config.database) - let - c = - { table - , poolConfig - , getSchemaConfig: config - } - fromDb <- dbModules c - traverse_ writeModule fromDb - where - readPoolConfig :: String -> Either String Pool.Configuration - readPoolConfig s = fromParts parts - where - parts = String.split (String.Pattern ":") s - - poolConfig db = (Pool.defaultConfiguration db) { idleTimeoutMillis = Just 500 } - - fromParts [ host, port, db, user ] = - portFromString port - <#> \p -> - (poolConfig db) - { host = Just host - , port = Just p - , user = Just user - } - - fromParts [ host, port, db, user, password ] = - portFromString port - <#> \p -> - (poolConfig db) - { host = Just host - , port = Just p - , user = Just user - , password = Just password - } - - fromParts _ = Left "Config parameter must be of the form :::: (password optional)" +main :: Effect Unit +main = launchAff_ $ runSpec [ consoleReporter ] specs - portFromString = note "Database port must be an integer" <<< Int.fromString +caseTmplFile :: FilePath +caseTmplFile = "./test/resources/get-schema/Case.tmpl" -dbModules :: Config -> ExceptT String Aff (Maybe Cst.Module) -dbModules config@{ getSchemaConfig: { dbManagedColumns, scalaPkg, pursPkg } } = do - pool <- liftEffect $ Pool.new config.poolConfig - if config.getSchemaConfig.domains then - Just <$> Database.domainModule pool { dbManagedColumns, scalaPkg, pursPkg } - else - for config.table $ Database.tableModule pool { dbManagedColumns, scalaPkg, pursPkg } +poolConfig :: Effect Pool.Configuration +poolConfig = do + host <- lookupEnv "DB_HOST" + port <- lookupEnv "DB_PORT" <#> (_ >>= Int.fromString) + db <- lookupEnv "DATABASE" + user <- lookupEnv "DB_USER" + password <- lookupEnv "DB_PASSWORD" + pure + $ (Pool.defaultConfiguration $ fromMaybe "cir" db) + { host = host + , port = port + , user = user + , password = password + } -type Config = - { table :: Maybe String - , poolConfig :: Pool.Configuration - , getSchemaConfig :: GetSchemaConfig - } +stripImports :: Cst.Source Cst.Module -> Cst.Source Cst.Module +stripImports source = source { contents = source.contents { imports = [] } } -prependNotice :: String -> String -prependNotice = ("// This file is automatically generated from DB schema. Do not edit.\n" <> _) +specs :: Spec Unit +specs = + describe "Case" do + it "fetches the case data correctly" do + results <- + runExceptT do + fileSource <- ExceptT <<< liftEffect $ sourceCstTmpl caseTmplFile + let + { scalaPkg, pursPkg } = fileSource.contents.exports + pool <- ExceptT <<< liftEffect <<< map pure $ poolConfig >>= Pool.new + dbModule <- + tableModule pool + { dbManagedColumns: Nothing + , scalaPkg + , pursPkg + } + "Case" + pure $ Tuple fileSource.contents dbModule + either fail (uncurry printAndDiff) results -writeModule :: Cst.Module -> ExceptT String Aff Unit -writeModule = liftEffectSafely <<< print +printAndDiff :: Cst.Module -> Cst.Module -> Aff Unit +printAndDiff x y = (print x) `diffByLine` (print y) where - print = Console.info <<< prependNotice <<< scrubEolSpaces <<< PrettyPrint.prettyPrint - -main :: Effect Unit -main = do - configuration <- OptParse.execParser $ OptParse.info getSchemaConfig (OptParse.fullDesc) - app configuration + print = scrubEolSpaces <<< prettyPrint