Skip to content

Commit

Permalink
🎨 - Make parser more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Peelen committed May 31, 2024
1 parent ee40afd commit 1db7cc5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
23 changes: 13 additions & 10 deletions src/SchemaParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module SchemaParser (parseTypeContainer, parseSchema) where
import Control.Monad
import Data.Set (member)
import qualified Data.Text as T
import Debug.Trace
import Text.Parsec
import Type.Reflection.Unsafe
import Types
Expand Down Expand Up @@ -36,28 +37,30 @@ parseType = do
parseTable :: Parsec T.Text () (T.Text, [(T.Text, T.Text)])
parseTable = do
optional $ string "diesel::" -- Diesel v2
string "table! {" <* try spaces
string "use diesel::sql_types::*;" <* try spaces
optional $ try $ string "use super::sql_types::" <* manyTill anyChar (try (string ";")) <* spaces
string "table! {"
optional (spaces *> string "use" <* manyTill anyChar (try $ newline <* newline))

spaces
typeName <- manyTill anyChar $ try space
spaces
try $ manyTill anyChar $ try $ string "{"
contents <- manyTill (try parseType) $ try $ spaces *> string "}"

try $ manyTill anyChar (try $ string "{")
contents <- manyTill (try parseType) (try $ spaces *> string "}")
spaces
try $ string "}"

string "}"
pure (T.pack typeName, contents)

sqlTypes = do
optional $ string "pub mod sql_types {" <* try spaces
string "pub mod sql_types {" <* try spaces
manyTill anyChar (try (string "}"))
pure ()

parseSchema :: T.Text -> [(T.Text, [(T.Text, T.Text)])]
parseSchema xs = case runParser schemaParser () "Error Parsing" xs of
Right x -> x
Left y -> []
where
schemaParser = do
optional $ string "// @generated automatically by Diesel CLI."
optional spaces <* sqlTypes <* spaces
optional $ string "// @generated automatically by Diesel CLI." <* spaces
optional sqlTypes <* spaces
manyTill (try parseTable <* spaces) $ try (optional $ string "diesel::" <* string "joinable" <|> (eof >> pure ""))
37 changes: 21 additions & 16 deletions src/SchemaPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ printModuleName :: T.Text -> T.Text
printModuleName xs = "module " <> snakeToPascal xs <> " = "

printTableName :: Configuration -> T.Text -> Visibility T.Text -> T.Text
printTableName configuration tableName (Visible types) = printModuleName tableName <> "{\n" <> types <> "\n};"
printTableName configuration tableName (Visible types) = printModuleName tableName <> "{\n" <> types <> "\n}" <> semi
where
semi = case language configuration of
Rescript -> ""
Reason -> ";"
printTableName configuration tableName Hidden = "// " <> printModuleName tableName <> "{ }" <> semi
where
semi = case language configuration of
Expand All @@ -88,21 +92,23 @@ printContainerizedPPXs :: Configuration -> T.Text
printContainerizedPPXs configuration = printPPXs 2 configuration $ containerizedPPX configuration

printContainerTypeAliases :: Configuration -> T.Text
printContainerTypeAliases configuration =
T.intercalate "\n\n" $
( \(x, y) ->
printContainerizedPPXs configuration
<> " type "
<> x
<> " = "
<> y
<> lBrack
<> "t"
<> rBrack
<> semi
)
<$> M.toList (containerized configuration)
printContainerTypeAliases configuration = case p of
[] -> T.pack ""
xs -> "\n\n" <> T.intercalate "\n\n" p
where
p =
( \(x, y) ->
printContainerizedPPXs configuration
<> " type "
<> x
<> " = "
<> y
<> lBrack
<> "t"
<> rBrack
<> semi
)
<$> M.toList (containerized configuration)
(lBrack, rBrack, semi) = case language configuration of
Rescript -> ("<", ">", "")
Reason -> ("(", ")", ";")
Expand All @@ -114,7 +120,6 @@ printTableTypes configuration tableName xs =
<> T.intercalate ",\n " (fmap (printType configuration tableName) xs)
<> ",\n }"
<> semi
<> "\n\n"
<> printContainerTypeAliases configuration
where
semi = case language configuration of
Expand Down

0 comments on commit 1db7cc5

Please sign in to comment.