diff --git a/README.md b/README.md index 0fbf839..99ecc56 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Describe datatypes in one or more ".type" file, then create appropriate datatype Still a work-in-progress, but there's (very early) results! This is open to collaboration, so if you would like to see `{{insert_language+version_here}}` faster than I can get around to it, feel free to take a swing at it. Once we get to a more feature-complete state and actually make this open to distribution, I'll keep track of actual changes with a CHANGELOG.md and versioning. -Inside `testProject/` (an example project root directory), there's a `datatypetool.toml` file. There are also test `.type` files inside `testProject/types/`. Some basic language scripts are there to do basic testing on types generated from `testProject/types/Test_File.type`, based on the JSON in `testProject/test.json` +Inside `exampleProject/` (an example project root directory), there's a `datatypetool.toml` file. There are also test `.type` files inside `exampleProject/types/`. Some basic language scripts are there to do basic testing on types generated from `exampleProject/types/Test_File.type`, based on the JSON in `exampleProject/test.json` There's also some basic (and hopefully undestandable) documentation in `docs/`. @@ -18,12 +18,12 @@ More languages and functionality to come soon! - If non-nix, GHC: 9.8.2, HLS: 2.7.0.0, cabal 3.10.2.1 from GHCup is what I have locally. Cabal will fetch the dependencies when it builds. - Do regular git-clone+cd stuff, then: - `just shell` (aka `nix-shell --pure --show-trace`) - - `just run --toml testProject/datatypetool.toml` (to generate files based on whats in `testProject/types/`) + - `just run --toml exampleProject/datatypetool.toml` (to generate files based on whats in `exampleProject/types/`) - `just format` (to format files for keeping things tidy) - `just` with no arguments or `just help` will list the available commands. Right now there's only 4, and this runs one of them. The other three are listed above. - Language based stuff (Toml configs, language writers, etc.) are found in `app/Lang/`. Just follow whats there, keep things organized. - `*.type` file parser stuff is found in `app/Parsing/` - `datatypetool.toml` parser stuff is found in `app/Config`, except dedicated language settings, which are found in `app/Lang/**/{{Config / Toml}}.hs` - `app/Cli.hs` has the command line parser (as one might expect) - - `app/Instances` is for doing funky Haskell instance declarations on special cases. + - `app/Instances.hs` is for doing funky Haskell instance declarations on special cases. - `app/Common/` is for general-purpose functions that could be used anywhere. Files in this directory should try to _not_ import files outside of here, to prevent import loops. \ No newline at end of file diff --git a/app/Cli.hs b/app/Cli.hs index 70cae0e..88754da 100644 --- a/app/Cli.hs +++ b/app/Cli.hs @@ -1,14 +1,12 @@ module Cli where -import Control.Applicative ((<|>)) -import Data.Function ((&)) -import Data.String (fromString) +import qualified Antelude.List as List +import Antelude.String (fromString) import Data.Version (showVersion) import Options.Applicative ((<**>)) import qualified Options.Applicative as Opts import Paths_datatypetool (version) import qualified System.Directory as Dir -import System.IO (FilePath) data Flags = Flags @@ -34,11 +32,11 @@ opts cwd = let <> Opts.metavar "FILE" <> Opts.help "A datatypetool.toml file path, if not within the current working directory" ) - & Opts.liftA (\a -> fromString a :: FilePath) + |> Opts.liftA (\a -> fromString a :: FilePath) ) <|> pure (cwd <> fromString "/datatypetool.toml" :: FilePath) ) - <*> ( ( length + <*> ( ( List.length <$> Opts.many (Opts.flag' Basic @@ -47,7 +45,7 @@ opts cwd = let ) ) ) - & Opts.liftA + |> Opts.liftA (\case 0 -> Basic 1 -> Extra @@ -61,8 +59,7 @@ opts cwd = let (parseFlags <**> Opts.simpleVersioner (showVersion version) <**> Opts.helper) ( Opts.fullDesc <> Opts.progDesc - ( "Create types that work across your entire stack.\n\ + "Create types that work across your entire stack.\n\ \ This CLI reads a datatypetool.toml file and creates types for\ \ all specified languages from a set of *.type files." - ) ) diff --git a/app/Common/Utils.hs b/app/Common/Utils.hs index e4a1b51..36a27c9 100644 --- a/app/Common/Utils.hs +++ b/app/Common/Utils.hs @@ -1,18 +1,20 @@ module Common.Utils where -import Control.Monad (mapM) -import qualified Data.Char as Char -import Data.Function ((&)) -import qualified Data.List as List -import qualified Data.Map.Strict as Map -import qualified Data.Maybe as Maybe -import qualified Data.Text as Text -import Data.Traversable (mapAccumM) -import qualified System.Directory as Dir -import System.FilePath (pathSeparator) -import System.IO (FilePath, readFile') -import qualified Text.Casing as Case +import qualified Antelude.Function as Function +import qualified Antelude.List as List +import qualified Antelude.Maybe as Maybe +import qualified Antelude.String.Case as Case +import qualified Antelude.Tuple.Pair as Pair +import Control.Monad (mapM) +import qualified Data.Char as Char +import qualified Data.Map.Strict as Map +import qualified Data.Text as Text +import Data.Traversable (mapAccumM) +import qualified System.Directory as Dir +import System.FilePath (pathSeparator) +import System.IO (readFile') +import qualified Text.Casing as Casing data CaseType @@ -28,26 +30,26 @@ typeFilePathToLangFilePath :: CaseType -> FilePath -> FilePath -> FilePath typeFilePathToLangFilePath caseType newExtension typeRelPath = let caseFn :: (String -> String) caseFn = case caseType of - PascalCase -> Case.pascal - CamelCase -> Case.camel + PascalCase -> Casing.pascal + CamelCase -> Casing.camel ScreamingCase -> List.map Char.toUpper - CapitalizedSnakeCase -> id - QuietSnakeCase -> Case.quietSnake - ScreamingSnakeCase -> Case.screamingSnake + CapitalizedSnakeCase -> Function.identity + QuietSnakeCase -> Casing.quietSnake + ScreamingSnakeCase -> Casing.screamingSnake in typeRelPath - & List.reverse -- Drop .type suffix - & List.stripPrefix (List.reverse ".type") - & Maybe.fromMaybe + |> List.reverse -- Drop .type suffix + |> List.stripPrefix (List.reverse ".type") + |> Maybe.withDefault (List.reverse typeRelPath) - & List.reverse - & List.drop 1 -- Remove prefixed '/' or '\\' - & split pathSeparator -- Split at '/' or '\\' - & List.map caseFn -- Case-ify - & List.intersperse [pathSeparator] -- Reinsert '/' or '\\' - & ([pathSeparator] :) -- Readd prefixed '/' or '\\' - & List.concat -- Merge - & (++ newExtension) -- New Extension + |> List.reverse + |> List.drop 1 -- Remove prefixed '/' or '\\' + |> split pathSeparator -- Split at '/' or '\\' + |> List.map caseFn -- Case-ify + |> List.intersperse [pathSeparator] -- Reinsert '/' or '\\' + |> ([pathSeparator] :) -- Readd prefixed '/' or '\\' + |> List.concat -- Merge + |> (<> newExtension) -- New Extension -- The same as above, but without the actual file. -- Used with System.Directory.createDirectoryIfMissing to ensure we can write. @@ -55,51 +57,51 @@ typeFilePathToLangDirPath :: CaseType -> FilePath -> FilePath typeFilePathToLangDirPath caseType typeRelPath = let caseFn :: (String -> String) caseFn = case caseType of - PascalCase -> Case.pascal - CamelCase -> Case.camel + PascalCase -> Casing.pascal + CamelCase -> Casing.camel ScreamingCase -> List.map Char.toUpper - CapitalizedSnakeCase -> id - QuietSnakeCase -> Case.quietSnake - ScreamingSnakeCase -> Case.screamingSnake + CapitalizedSnakeCase -> Function.identity + QuietSnakeCase -> Casing.quietSnake + ScreamingSnakeCase -> Casing.screamingSnake in typeRelPath - & List.reverse -- Drop .type suffix - & List.dropWhile (/= pathSeparator) - & List.reverse - & List.drop 1 -- Remove prefixed '/' or '\\' - & split pathSeparator -- Split at '/' or '\\' - & List.map caseFn -- Case-ify - & List.intersperse [pathSeparator] -- Reinsert '/' or '\\' - & ([pathSeparator] :) -- Readd prefixed '/' or '\\' - & List.concat -- Merge + |> List.reverse -- Drop .type suffix + |> List.dropWhile (/= pathSeparator) + |> List.reverse + |> List.drop 1 -- Remove prefixed '/' or '\\' + |> split pathSeparator -- Split at '/' or '\\' + |> List.map caseFn -- Case-ify + |> List.intersperse [pathSeparator] -- Reinsert '/' or '\\' + |> ([pathSeparator] :) -- Readd prefixed '/' or '\\' + |> List.concat -- Merge typeFilePathToLangCodePath :: CaseType -> Char -> FilePath -> String typeFilePathToLangCodePath caseType separator typeRelPath = let caseFn :: (String -> String) caseFn = case caseType of - PascalCase -> Case.pascal - CamelCase -> Case.camel + PascalCase -> Casing.pascal + CamelCase -> Casing.camel ScreamingCase -> List.map Char.toUpper - CapitalizedSnakeCase -> id - QuietSnakeCase -> Case.quietSnake - ScreamingSnakeCase -> Case.screamingSnake + CapitalizedSnakeCase -> Function.identity + QuietSnakeCase -> Casing.quietSnake + ScreamingSnakeCase -> Casing.screamingSnake in typeRelPath - & List.reverse -- Drop .type suffix - & List.stripPrefix (List.reverse ".type") - & Maybe.fromMaybe + |> List.reverse -- Drop .type suffix + |> List.stripPrefix (List.reverse ".type") + |> Maybe.withDefault (List.reverse typeRelPath) - & List.reverse - & List.dropWhile (== pathSeparator) -- Remove prefixed '/' or '\\' - & split pathSeparator -- Split at '/' or '\\' - & List.map caseFn -- Case-ify - & List.intersperse [separator] -- Insert langage's file separator - & List.concat -- Merge + |> List.reverse + |> List.dropWhile (== pathSeparator) -- Remove prefixed '/' or '\\' + |> split pathSeparator -- Split at '/' or '\\' + |> List.map caseFn -- Case-ify + |> List.intersperse [separator] -- Insert langage's file separator + |> List.concat -- Merge split :: Char -> String -> [String] -split chr str = case break (==chr) str of +split chr str = case List.break (==chr) str of (a, chr:b) -> a : split chr b (a, "") -> [a] @@ -114,45 +116,39 @@ recurseDirectory dp = traverseDirs state pth = let fullPth :: FilePath fullPth = dp - ++ [pathSeparator] - ++ pth + <> [pathSeparator] + <> pth in Dir.doesDirectoryExist fullPth >>= \case True -> recurseDirectory fullPth - >>= \dirsResult -> return (state ++ dirsResult, dirsResult) + >>= \dirsResult -> return (state <> dirsResult, dirsResult) False -> Dir.doesFileExist fullPth >>= \case - True -> if (List.isSuffixOf ".type" fullPth) + True -> if List.isSuffixOf ".type" fullPth then readFile' fullPth >>= \result -> return - ( state ++ [(fullPth, result)] + ( state <> [(fullPth, result)] , [(fullPth, result)] ) else return (state, []) False -> return (state, []) in Dir.listDirectory dp >>= mapAccumM traverseDirs [] - >>= return . fst + >>= return <. Pair.first createWritePath :: FilePath -> IO () -createWritePath path = - Dir.createDirectoryIfMissing True path +createWritePath = + Dir.createDirectoryIfMissing True between :: Ord a => a -> a -> a -> Bool between input lower upper = - if lower < input && input < upper - then True - else False + lower < input && input < upper betweenOrEqualLower :: Ord a => a -> a -> a -> Bool betweenOrEqualLower input lower upper = - if lower <= input && input < upper - then True - else False + lower <= input && input < upper betweenOrEqualUpper :: Ord a => a -> a -> a -> Bool betweenOrEqualUpper input lower upper = - if lower < input && input <= upper - then True - else False + lower < input && input <= upper diff --git a/app/Config/Config.hs b/app/Config/Config.hs index 4561252..41d63fe 100644 --- a/app/Config/Config.hs +++ b/app/Config/Config.hs @@ -1,27 +1,26 @@ {-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE TupleSections #-} module Config.Config where +import qualified Antelude.List as List +import qualified Antelude.List.NonEmpty as NE +import qualified Antelude.Maybe as Maybe +import qualified Antelude.Tuple.Pair as Pair import Control.Applicative -import Data.Function ((&)) -import qualified Data.List as List -import qualified Data.List.NonEmpty as NE -import qualified Data.Map.Strict as Map -import qualified Data.Maybe as Maybe -import Data.Text (Text) -import qualified Data.Text as Text -import qualified Lang.Elm.Config as ElmConfig -import qualified Lang.Elm.Toml as ElmToml -import qualified Lang.Gleam.Config as GleamConfig -import qualified Lang.Gleam.Toml as GleamToml -import qualified Lang.Haskell.Config as HaskellConfig -import qualified Lang.Haskell.Toml as HaskellToml -import qualified Lang.Python.Config as PythonConfig -import qualified Lang.Python.Toml as PythonToml -import qualified Parsing.Types as PT -import System.IO (FilePath) +import qualified Data.Map.Strict as Map +import qualified Data.Text as Text +import qualified Data.Version as Version +import qualified Lang.Elm.Config as ElmConfig +import qualified Lang.Elm.Toml as ElmToml +import qualified Lang.Gleam.Config as GleamConfig +import qualified Lang.Gleam.Toml as GleamToml +import qualified Lang.Haskell.Config as HaskellConfig +import qualified Lang.Haskell.Toml as HaskellToml +import qualified Lang.Python.Config as PythonConfig +import qualified Lang.Python.Toml as PythonToml +import qualified Lang.Python.Writer as PythonWrite +import qualified Parsing.Types as PT import qualified Toml -import Toml ((.=)) +import Toml ((.=)) data Config = @@ -30,36 +29,36 @@ data Config = , spacing :: Spacing -- This comment is for testing with regular lists -- , targets :: [LangConfiguration] - , targets :: NE.NonEmpty LangConfiguration + , targets :: NonEmpty LangConfiguration } datatypetoolCodec :: Toml.TomlCodec Config datatypetoolCodec = Config - <$> (Toml.table + <$> Toml.table (Toml.string "sources") "datatypetool" - ) + .= sources - <*> (Toml.table + <*> Toml.table ( Toml.dioptional (Toml.dimap fromSpacing toSpacing (Toml.string "spacing") ) - & Toml.dimap - (Just) - (Maybe.fromMaybe Spaces4) + |> Toml.dimap + Just + (Maybe.withDefault Spaces4) ) "datatypetool" - ) + .= spacing <*> (Toml.tableMap Toml._KeyText indvLangCodec "targets" - & parseTableAsNEList + |> parseTableAsNEList ) .= targets @@ -73,16 +72,16 @@ data Spacing toSpacing :: String -> Spacing -toSpacing ("tabs") = Tabs -toSpacing ("spaces2") = Spaces2 -toSpacing ("spaces3") = Spaces3 -toSpacing ("spaces4") = Spaces4 +toSpacing "tabs" = Tabs +toSpacing "spaces2" = Spaces2 +toSpacing "spaces3" = Spaces3 +toSpacing "spaces4" = Spaces4 fromSpacing :: Spacing -> String -fromSpacing (Tabs) = "tabs" -fromSpacing (Spaces2) = "spaces2" -fromSpacing (Spaces3) = "spaces3" -fromSpacing (Spaces4) = "spaces4" +fromSpacing Tabs = "tabs" +fromSpacing Spaces2 = "spaces2" +fromSpacing Spaces3 = "spaces3" +fromSpacing Spaces4 = "spaces4" data LangConfiguration @@ -100,10 +99,35 @@ parseTableAsNEList parseTableAsNEList = Toml.dimap -- These comments are for testing with regular lists - -- (\ls -> List.map (\a -> (langNameString a, a)) ls & Map.fromList) - -- (\mp -> Map.toList mp & List.map snd) - (\ne -> NE.toList ne & List.map (\a -> (langNameString a, a)) & Map.fromList) - (\mp -> Map.toList mp & List.map snd & NE.fromList) + -- (\ls -> + -- List.map + -- (\a -> (langNameString a, a)) ls + -- |> Map.fromList + -- ) + -- (\mp -> + -- Map.toList mp + -- |> List.map Pair.second + -- ) + (\ne -> + NE.toList ne + |> List.map + (\a -> (langNameString a, a)) + |> Map.fromList + ) + (\mp -> + Map.toList mp + |> List.map Pair.second + |> NE.fromList + |> Maybe.withDefault + (NE.singleton + <| Python + <| PythonConfig.LangConf + { PythonConfig.langWriter = PythonWrite.pythonWriter + , PythonConfig.outputLocation = "/dev/null" + , PythonConfig.langVersion = Version.Version {Version.versionBranch = [0]} + } + ) + ) langNameString :: LangConfiguration -> Text @@ -113,44 +137,36 @@ langNameString (Elm _) = "elm" langNameString (Gleam _) = "gleam" matchPython :: LangConfiguration -> Maybe PythonConfig.LangConf -matchPython (Python cnf) = Just (cnf) +matchPython (Python cnf) = Just cnf matchPython _ = Nothing matchHaskell :: LangConfiguration -> Maybe HaskellConfig.LangConf -matchHaskell (Haskell cnf) = Just (cnf) +matchHaskell (Haskell cnf) = Just cnf matchHaskell _ = Nothing matchElm :: LangConfiguration -> Maybe ElmConfig.LangConf -matchElm (Elm cnf) = Just (cnf) +matchElm (Elm cnf) = Just cnf matchElm _ = Nothing matchGleam :: LangConfiguration -> Maybe GleamConfig.LangConf -matchGleam (Gleam cnf) = Just (cnf) +matchGleam (Gleam cnf) = Just cnf matchGleam _ = Nothing indvLangCodec :: Toml.Key -> Toml.TomlCodec LangConfiguration -indvLangCodec ("python") = +indvLangCodec "python" = Toml.dimatch matchPython Python (Toml.table PythonToml.confCodec "python") -indvLangCodec ("haskell") = +indvLangCodec "haskell" = Toml.dimatch matchHaskell Haskell (Toml.table HaskellToml.confCodec "haskell") -indvLangCodec ("gleam") = +indvLangCodec "gleam" = Toml.dimatch matchGleam Gleam (Toml.table GleamToml.confCodec "gleam") -indvLangCodec ("elm") = +indvLangCodec "elm" = Toml.dimatch matchElm Elm (Toml.table ElmToml.confCodec "elm") runLangWriter :: LangConfiguration -> (FilePath -> [(FilePath, [PT.DataTypes])] -> IO ()) -runLangWriter (Python cnf) = (PythonConfig.langWriter cnf) cnf -runLangWriter (Haskell cnf) = (HaskellConfig.langWriter cnf) cnf -runLangWriter (Elm cnf) = (ElmConfig.langWriter cnf) cnf -runLangWriter (Gleam cnf) = (GleamConfig.langWriter cnf) cnf - - --- data BasicLangConf = --- LangConf --- { langWriter :: IO () --- , outputLocation :: String --- , langVersion :: String --- } --- deriving (Show, Eq, Ord) +runLangWriter (Python cnf) = PythonConfig.langWriter cnf cnf +runLangWriter (Haskell cnf) = HaskellConfig.langWriter cnf cnf +runLangWriter (Elm cnf) = ElmConfig.langWriter cnf cnf +runLangWriter (Gleam cnf) = GleamConfig.langWriter cnf cnf + diff --git a/app/Config/Extras.hs b/app/Config/Extras.hs index 12271d4..74fb480 100644 --- a/app/Config/Extras.hs +++ b/app/Config/Extras.hs @@ -1,10 +1,8 @@ {-# LANGUAGE OverloadedStrings #-} module Config.Extras where -import Data.Function ((&)) -import qualified Data.List as List +import qualified Antelude.List as List import qualified Data.Map.Strict as Map -import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Version as Version import Text.Read (readMaybe) @@ -14,23 +12,23 @@ import Toml ((.=)) versionCodec :: Toml.Key -> Toml.TomlCodec Version.Version versionCodec key = let splitByDot :: String -> [String] - splitByDot str = case dropWhile (=='.') str of + splitByDot str = case List.dropWhile (=='.') str of "" -> [] str' -> w : splitByDot str'' - where (w, str'') = break (=='.') str' + where (w, str'') = List.break (=='.') str' strToVersion :: String -> Version.Version strToVersion str = str - & splitByDot - & List.map + |> splitByDot + |> List.map (\s -> (readMaybe s :: Maybe Int) - & \case + |> \case Just a -> a Nothing -> 0 ) - & Version.makeVersion + |> Version.makeVersion in Toml.dimap Version.showVersion @@ -40,7 +38,7 @@ versionCodec key = let optionalTextConsiderBlanks :: Toml.Key -> Toml.TomlCodec (Maybe Text) -optionalTextConsiderBlanks key = Toml.dioptional (Toml.text key) & considerBlankAsNothing +optionalTextConsiderBlanks key = Toml.dioptional (Toml.text key) |> considerBlankAsNothing where considerJustBlank :: Maybe Text -> Maybe Text considerJustBlank (Nothing) = Nothing @@ -52,4 +50,3 @@ optionalTextConsiderBlanks key = Toml.dioptional (Toml.text key) & considerBlank Toml.dimap considerJustBlank considerJustBlank - diff --git a/app/Instances.hs b/app/Instances.hs index 3f3a873..28ac305 100644 --- a/app/Instances.hs +++ b/app/Instances.hs @@ -1,5 +1,8 @@ module Instances where +import Data.Eq (Eq (..)) +import Data.Ord (Ord (..)) + instance Eq (a -> b) where (==) _ _ = True (/=) _ _ = False diff --git a/app/Lang/Elm/Config.hs b/app/Lang/Elm/Config.hs index b302e48..9ebd3d5 100644 --- a/app/Lang/Elm/Config.hs +++ b/app/Lang/Elm/Config.hs @@ -4,7 +4,6 @@ module Lang.Elm.Config where import Data.Version (Version) import Instances import Parsing.Types (DataTypes) -import System.IO (FilePath) import Text.Show.Functions diff --git a/app/Lang/Elm/Toml.hs b/app/Lang/Elm/Toml.hs index da26110..13901d3 100644 --- a/app/Lang/Elm/Toml.hs +++ b/app/Lang/Elm/Toml.hs @@ -4,7 +4,6 @@ module Lang.Elm.Toml where import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) import Instances import Lang.Elm.Config (LangConf (..)) import Lang.Elm.Writer (elmWriter) diff --git a/app/Lang/Elm/Versions/ElmZeroNineteenOne.hs b/app/Lang/Elm/Versions/ElmZeroNineteenOne.hs index ff49286..8d308de 100644 --- a/app/Lang/Elm/Versions/ElmZeroNineteenOne.hs +++ b/app/Lang/Elm/Versions/ElmZeroNineteenOne.hs @@ -1,16 +1,17 @@ module Lang.Elm.Versions.ElmZeroNineteenOne (writer) where +import qualified Antelude.File as File +import qualified Antelude.List as List +import qualified Antelude.Maybe as Maybe import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) -import qualified Data.List as List +import qualified Control.Monad as Monad import qualified Data.Map.Strict as Map import qualified Data.Set as Set import qualified Data.Text as Text import Lang.Elm.Config (LangConf (..)) import qualified Parsing.Types as PT import qualified System.Directory as Dir -import System.IO (FilePath) import qualified Text.Casing as Case @@ -18,19 +19,19 @@ import qualified Text.Casing as Case writer :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () writer elmConf baseDir = let path :: FilePath -> FilePath - path pth = (baseDir <> (outputLocation elmConf) <> elmDirPath pth) + path pth = (baseDir <> outputLocation elmConf <> elmDirPath pth) in - mapM_ + Monad.mapM_ (\(pth, lstType) -> Com.createWritePath (baseDir - <> (outputLocation elmConf) + <> outputLocation elmConf <> "/DTT/" <> elmDirPath pth ) - >> writeFile + >> File.writeFile (baseDir - <> (outputLocation elmConf) + <> outputLocation elmConf <> "/DTT/" <> elmFilePath pth ) @@ -43,17 +44,17 @@ writer elmConf baseDir = let ) elmDirPath :: FilePath -> FilePath -elmDirPath fp = +elmDirPath = Com.typeFilePathToLangDirPath Com.PascalCase - fp + elmFilePath :: FilePath -> FilePath -elmFilePath fp = +elmFilePath = Com.typeFilePathToLangFilePath Com.PascalCase ".elm" - fp + elmCodePath :: FilePath -> String elmCodePath = Com.typeFilePathToLangCodePath @@ -63,22 +64,22 @@ elmCodePath = Com.typeFilePathToLangCodePath header :: FilePath -> String header fp = - ( "module DTT." - <> elmCodePath fp - <> " exposing (..)" - <> "\n\n{- This file was generated by DataTypeTool -}\n\n" - ) + "module DTT." + <> elmCodePath fp + <> " exposing (..)" + <> "\n\n{- This file was generated by DataTypeTool -}\n\n" + staticImports :: String staticImports = - ( "import Dict exposing (Dict)\n" + "import Dict exposing (Dict)\n" <> "import Set exposing (Set)\n" <> "import Json.Decode as JD\n" <> "import Json.Encode as JE\n" <> "import Json.Decode.Extra as JDE\n" <> "import Json.Encode.Extra as JEE\n" <> "import Json.Decode.Pipeline as JDP\n" - ) + mapReferencedImports :: (FilePath, [PT.DataTypes]) -> String mapReferencedImports (_, lstDtaTypes) = let @@ -90,29 +91,29 @@ mapReferencedImports (_, lstDtaTypes) = let <> elmCodePath fp <> " exposing (" <> ( lstTypes - & List.filter + |> List.filter (\case PT.DefinedDataTypes _ -> True _ -> False ) - & List.map + |> List.map (\a -> typesMap a -- TODO, need to adjust how "DefinedDataTypes" are stored -- So I can disambiguate between needing (..) or not here. <> ", " - <> (serializerMap a) + <> serializerMap a <> ", " - <> (deserializerMap a) + <> deserializerMap a ) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> ")" ) metaStuff - & List.intersperse "\n" - & List.concat + |> List.intersperse "\n" + |> List.concat ) <> "\n" in @@ -123,231 +124,229 @@ mapReferencedImports (_, lstDtaTypes) = let PT.Enum _ meta _ -> meta PT.Model _ meta _ -> meta PT.Constant _ meta _ _ -> meta - & \case + |> \case Just m -> - case PT.metaRef m of - Just ref -> referencedImports ref - Nothing -> "" + Maybe.map referencedImports (PT.metaRef m) + |> Maybe.withDefault "" Nothing -> "" ) lstDtaTypes - & List.filter (/= "") - & List.intersperse "\n" - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n" + |> List.concat docComments :: Maybe PT.MetaField -> String docComments = \case Just meta -> - case (PT.metaDoc meta) of + case PT.metaDoc meta of Just doc -> - ( "{-\n " + "{-\n " <> ( List.lines (Text.unpack doc) - & List.filter (/= "") - & List.intersperse "\n " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n " + |> List.concat ) <> "\n-}\n" - ) Nothing -> "" Nothing -> "" -serializer :: PT.DataTypes -> String -serializer (PT.Constant name meta type_ value) = "" -serializer (PT.Alias name meta type_) = - ( (Text.unpack name & Case.camel) - <> "Encoder : " - <> (Text.unpack name & Case.pascal) - <> " -> JE.Value\n" - <> (Text.unpack name & Case.camel) - <> "Encoder val =\n " - <> (serializerMap type_) - <> " val" - ) -serializer (PT.Enum name meta members) = - ( (Text.unpack name & Case.camel) - <> "Encoder : " - <> (Text.unpack name & Case.pascal) - <> " -> JE.Value\n" - <> (Text.unpack name & Case.camel) - <> "Encoder val =\n " - <> "case val of\n " - <> ( List.map - (\case - PT.OtherValue txt -> - ( (Text.unpack txt) - <> " -> JE.string \"" - <> (Case.quietSnake $ Text.unpack txt) - <> "\"" - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat - ) - ) -serializer (PT.Model name meta fields) = - ( (Text.unpack name & Case.camel) - <> "Encoder : " - <> (Text.unpack name & Case.pascal) - <> " -> JE.Value\n" - <> (Text.unpack name & Case.camel) - <> "Encoder val =" - <> "\n " - <> "JE.object\n [ " - <> ( List.map - (\field -> - ( "( \"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "\", " - <> (PT.fieldType field & serializerMap) - <> " val." - <> (PT.fieldName field & Text.unpack & Case.camel) - <> ")" - ) - ) - fields - & List.intersperse "\n , " - & List.concat - ) - <> "\n ]" - ) +-- serializer :: PT.DataTypes -> String +-- serializer (PT.Constant name meta type_ value) = "" +-- serializer (PT.Alias name meta type_) = +-- ( (Text.unpack name |> Case.camel) +-- <> "Encoder : " +-- <> (Text.unpack name |> Case.pascal) +-- <> " -> JE.Value\n" +-- <> (Text.unpack name |> Case.camel) +-- <> "Encoder val =\n " +-- <> (serializerMap type_) +-- <> " val" +-- ) +-- serializer (PT.Enum name meta members) = +-- ( (Text.unpack name |> Case.camel) +-- <> "Encoder : " +-- <> (Text.unpack name |> Case.pascal) +-- <> " -> JE.Value\n" +-- <> (Text.unpack name |> Case.camel) +-- <> "Encoder val =\n " +-- <> "case val of\n " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( (Text.unpack txt) +-- <> " -> JE.string \"" +-- <> (Case.quietSnake <| Text.unpack txt) +-- <> "\"" +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- ) +-- serializer (PT.Model name meta fields) = +-- ( (Text.unpack name |> Case.camel) +-- <> "Encoder : " +-- <> (Text.unpack name |> Case.pascal) +-- <> " -> JE.Value\n" +-- <> (Text.unpack name |> Case.camel) +-- <> "Encoder val =" +-- <> "\n " +-- <> "JE.object\n [ " +-- <> ( List.map +-- (\field -> +-- ( "( \"" +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "\", " +-- <> (PT.fieldType field |> serializerMap) +-- <> " val." +-- <> (PT.fieldName field |> Text.unpack |> Case.camel) +-- <> ")" +-- ) +-- ) +-- fields +-- |> List.intersperse "\n , " +-- |> List.concat +-- ) +-- <> "\n ]" +-- ) -deserializer :: PT.DataTypes -> String -deserializer (PT.Constant name meta type_ value) = "" -deserializer (PT.Alias name meta type_) = - ( (Text.unpack name & Case.camel) - <> "Decoder : JD.Decoder " - <> (Text.unpack name & Case.pascal) - <> "\n" - <> (Text.unpack name & Case.camel) - <> "Decoder = " - <> (deserializerMap type_) - ) -deserializer (PT.Enum name meta members) = - ( (Text.unpack name & Case.camel) - <> "Decoder : JD.Decoder " - <> (Text.unpack name & Case.pascal) - <> "\n" - <> (Text.unpack name & Case.camel) - <> "Decoder = JD.string" - <> "\n |> JD.andThen" - <> "\n (\\strEnum -> case strEnum of" - <> "\n " - <> ( List.map - (\case - PT.OtherValue txt -> - ( "\"" - <> (Case.quietSnake $ Text.unpack txt) - <> "\" -> JD.succeed " - <> (Text.unpack txt) - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat - ) - <> "\n _ -> JD.fail \"Could not find matching string for type " - <> (Text.unpack name & Case.pascal) - <> "\"\n )" - ) -deserializer (PT.Model name meta fields) = - ( (Text.unpack name & Case.camel) - <> "Decoder : JD.Decoder " - <> (Text.unpack name & Case.pascal) - <> "\n" - <> (Text.unpack name & Case.camel) - <> "Decoder =\n JD.succeed " - <> (Text.unpack name & Case.pascal) - <> "\n " - <> ( List.map - (\field -> - ( "|> JDP.required \"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "\" (" - <> (PT.fieldType field & deserializerMap) - <> ")" - ) - ) - fields - & List.intersperse "\n " - & List.concat - ) - ) +-- deserializer :: PT.DataTypes -> String +-- deserializer (PT.Constant name meta type_ value) = "" +-- deserializer (PT.Alias name meta type_) = +-- ( (Text.unpack name |> Case.camel) +-- <> "Decoder : JD.Decoder " +-- <> (Text.unpack name |> Case.pascal) +-- <> "\n" +-- <> (Text.unpack name |> Case.camel) +-- <> "Decoder = " +-- <> (deserializerMap type_) +-- ) +-- deserializer (PT.Enum name meta members) = +-- ( (Text.unpack name |> Case.camel) +-- <> "Decoder : JD.Decoder " +-- <> (Text.unpack name |> Case.pascal) +-- <> "\n" +-- <> (Text.unpack name |> Case.camel) +-- <> "Decoder = JD.string" +-- <> "\n |> JD.andThen" +-- <> "\n (\\strEnum -> case strEnum of" +-- <> "\n " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( "\"" +-- <> (Case.quietSnake <| Text.unpack txt) +-- <> "\" -> JD.succeed " +-- <> (Text.unpack txt) +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- <> "\n _ -> JD.fail \"Could not find matching string for type " +-- <> (Text.unpack name |> Case.pascal) +-- <> "\"\n )" +-- ) +-- deserializer (PT.Model name meta fields) = +-- ( (Text.unpack name |> Case.camel) +-- <> "Decoder : JD.Decoder " +-- <> (Text.unpack name |> Case.pascal) +-- <> "\n" +-- <> (Text.unpack name |> Case.camel) +-- <> "Decoder =\n JD.succeed " +-- <> (Text.unpack name |> Case.pascal) +-- <> "\n " +-- <> ( List.map +-- (\field -> +-- ( "|> JDP.required \"" +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "\" (" +-- <> (PT.fieldType field |> deserializerMap) +-- <> ")" +-- ) +-- ) +-- fields +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- ) invTypeWriter :: PT.DataTypes -> String invTypeWriter (PT.Constant name meta type_ value) = - ( docComments meta - <> (Text.unpack name & Case.camel) + docComments meta + <> (Text.unpack name |> Case.camel) <> " : " - <> (typesMap type_) + <> typesMap type_ <> "\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> " = " - <> (valuesMap value) - ) + <> valuesMap value + invTypeWriter (PT.Alias name meta type_) = - ( docComments meta + docComments meta <> "type alias " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> " = " - <> (typesMap type_) + <> typesMap type_ <> "\n\n\n" - <> serializer (PT.Alias name meta type_) + -- <> serializer (PT.Alias name meta type_) <> "\n\n\n" - <> deserializer (PT.Alias name meta type_) - ) + -- <> deserializer (PT.Alias name meta type_) + invTypeWriter (PT.Enum name meta members) = - ( docComments meta + docComments meta <> "type " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> "\n = " <> ( members - & List.map (\m -> valuesMap m & Case.pascal) - & List.intersperse "\n | " - & List.concat + |> List.map (\m -> valuesMap m |> Case.pascal) + |> List.intersperse "\n | " + |> List.concat ) <> "\n\n\n" - <> serializer (PT.Enum name meta members) + -- <> serializer (PT.Enum name meta members) <> "\n\n\n" - <> deserializer (PT.Enum name meta members) - ) + -- <> deserializer (PT.Enum name meta members) + invTypeWriter (PT.Model name meta fields) = - ( docComments meta + docComments meta <> "type alias " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> " =\n" <> " { " <> ( fields - & List.map + |> List.map (\f -> - (Text.unpack (PT.fieldName f) & Case.camel) + (Text.unpack (PT.fieldName f) |> Case.camel) <> " : " <> typesMap (PT.fieldType f) ) - & List.intersperse "\n , " - & List.concat + |> List.intersperse "\n , " + |> List.concat ) <> "\n }" <> "\n\n\n" - <> serializer (PT.Model name meta fields) + -- <> serializer (PT.Model name meta fields) <> "\n\n\n" - <> deserializer (PT.Model name meta fields) - ) + -- <> deserializer (PT.Model name meta fields) + typesWriter :: [PT.DataTypes] -> String typesWriter types = List.map invTypeWriter types - & List.intersperse "\n\n\n" - & List.concat + |> List.intersperse "\n\n\n" + |> List.concat @@ -397,7 +396,7 @@ typesMap = \case <> ", " <> typesMap e <> ")" - PT.DefinedDataTypes t -> Text.unpack t & Case.pascal + PT.DefinedDataTypes t -> Text.unpack t |> Case.pascal valuesMap :: PT.Values -> String @@ -409,8 +408,8 @@ valuesMap = \case PT.VList lst -> "[" <> ( List.map valuesMap lst - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VDict map -> @@ -424,8 +423,8 @@ valuesMap = \case <> ")" ) (Map.toList map) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VNullable nll -> case nll of @@ -434,8 +433,8 @@ valuesMap = \case PT.VSet set -> "Set.fromList [" <> ( List.map valuesMap (Set.toList set) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VTuple2 (a, b) -> @@ -474,7 +473,7 @@ valuesMap = \case <> ", " <> valuesMap e <> ")" - PT.OtherValue t -> Text.unpack t & Case.pascal + PT.OtherValue t -> Text.unpack t |> Case.pascal serializerMap :: PT.Types -> String @@ -526,7 +525,7 @@ serializerMap = \case <> " d), (\"fifth\", " <> serializerMap e <> " e)])" - PT.DefinedDataTypes txt -> Text.unpack txt & Case.camel & (<> "Encoder") + PT.DefinedDataTypes txt -> Text.unpack txt |> Case.camel |> (<> "Encoder") deserializerMap :: PT.Types -> String @@ -583,5 +582,4 @@ deserializerMap = \case <> ")) (JD.field \"fifth\" (" <> deserializerMap e <>")))" - PT.DefinedDataTypes txt -> Text.unpack txt & Case.camel & (<> "Decoder") - + PT.DefinedDataTypes txt -> Text.unpack txt |> Case.camel |> (<> "Decoder") diff --git a/app/Lang/Elm/Writer.hs b/app/Lang/Elm/Writer.hs index 8243749..c38aa34 100644 --- a/app/Lang/Elm/Writer.hs +++ b/app/Lang/Elm/Writer.hs @@ -7,7 +7,6 @@ import Data.Version (Version, makeVersion, import Lang.Elm.Config (LangConf (..)) import qualified Lang.Elm.Versions.ElmZeroNineteenOne as E0_19_1 import qualified Parsing.Types as PT -import System.IO (FilePath) elmWriter :: Lang.Elm.Config.LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () elmWriter langConf path typeData = let diff --git a/app/Lang/Gleam/Config.hs b/app/Lang/Gleam/Config.hs index 67ac0c2..e23d6d1 100644 --- a/app/Lang/Gleam/Config.hs +++ b/app/Lang/Gleam/Config.hs @@ -3,7 +3,6 @@ module Lang.Gleam.Config where import Data.Version (Version) import Instances import Parsing.Types (DataTypes) -import System.IO (FilePath) import Text.Show.Functions data GleamBackend diff --git a/app/Lang/Gleam/Toml.hs b/app/Lang/Gleam/Toml.hs index b419091..2b97d13 100644 --- a/app/Lang/Gleam/Toml.hs +++ b/app/Lang/Gleam/Toml.hs @@ -4,7 +4,6 @@ module Lang.Gleam.Toml where import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) import Instances import Lang.Gleam.Config (GleamBackend (..), LangConf (..)) import Lang.Gleam.Writer (gleamWriter) diff --git a/app/Lang/Gleam/Versions/GleamOneZeroZero.hs b/app/Lang/Gleam/Versions/GleamOneZeroZero.hs index 26b3d0f..f1e1a3e 100644 --- a/app/Lang/Gleam/Versions/GleamOneZeroZero.hs +++ b/app/Lang/Gleam/Versions/GleamOneZeroZero.hs @@ -1,16 +1,17 @@ module Lang.Gleam.Versions.GleamOneZeroZero (writer) where +import qualified Antelude.File as File +import qualified Antelude.List as List +import qualified Antelude.Maybe as Maybe import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) -import qualified Data.List as List +import qualified Control.Monad as Monad import qualified Data.Map.Strict as Map import qualified Data.Set as Set import qualified Data.Text as Text import Lang.Gleam.Config (LangConf (..)) import qualified Parsing.Types as PT import qualified System.Directory as Dir -import System.IO (FilePath) import qualified Text.Casing as Case @@ -18,9 +19,9 @@ import qualified Text.Casing as Case writer :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () writer gleamConf baseDir = let path :: FilePath -> FilePath - path pth = (baseDir <> (outputLocation gleamConf) <> gleamDirPath pth) + path pth = (baseDir <> outputLocation gleamConf <> gleamDirPath pth) in - mapM_ + Monad.mapM_ (\(pth, lstType) -> Dir.createDirectoryIfMissing True @@ -29,7 +30,7 @@ writer gleamConf baseDir = let <> "/dtt/" <> gleamDirPath pth ) - >> writeFile + >> File.writeFile ( baseDir <> outputLocation gleamConf <> "/dtt/" @@ -42,24 +43,20 @@ writer gleamConf baseDir = let <> ( List.map invTypeWriter lstType - & List.intersperse "\n\n\n" - & List.concat + |> List.intersperse "\n\n\n" + |> List.concat ) ) ) gleamDirPath :: FilePath -> FilePath -gleamDirPath fp = - Com.typeFilePathToLangDirPath +gleamDirPath = Com.typeFilePathToLangDirPath Com.QuietSnakeCase - fp gleamFilePath :: FilePath -> FilePath -gleamFilePath fp = - Com.typeFilePathToLangFilePath +gleamFilePath = Com.typeFilePathToLangFilePath Com.QuietSnakeCase ".gleam" - fp -- for the header of files, imports, etc gleamCodePath :: FilePath -> String @@ -75,13 +72,12 @@ header = staticImports :: String staticImports = - ( "import gleam/dict.{type Dict}\n" + "import gleam/dict.{type Dict}\n" <> "import gleam/set.{type Set}\n" <> "import gleam/dynamic.{type Dynamic, type DecodeError}\n" <> "import gleam/json.{type Json}\n" <> "import gleam/option.{type Option}\n" <> "import gleam/result.{try}\n" - ) mapReferencedImports :: (FilePath, [PT.DataTypes]) -> String mapReferencedImports (_, lstDtaTypes) = let @@ -93,26 +89,26 @@ mapReferencedImports (_, lstDtaTypes) = let <> gleamCodePath fp <> ".{" <> ( lstTypes - & List.map + |> List.map (\case PT.DefinedDataTypes txt -> "type " - <> (typesMap $ PT.DefinedDataTypes txt) + <> (typesMap <| PT.DefinedDataTypes txt) <> ", " - <> (txt & Text.unpack & Case.quietSnake) + <> (txt |> Text.unpack |> Case.quietSnake) <> "_encoder, " - <> (deserializerMap $ PT.DefinedDataTypes txt) + <> (deserializerMap <| PT.DefinedDataTypes txt) _ -> "" ) - & List.filter (/= "") - & List.intersperse ", " - & List.concat + |> List.filter (/= "") + |> List.intersperse ", " + |> List.concat ) <> "}" ) metaStuff - & List.intersperse "\n" - & List.concat + |> List.intersperse "\n" + |> List.concat ) <> "\n" in @@ -123,288 +119,275 @@ mapReferencedImports (_, lstDtaTypes) = let PT.Enum _ meta _ -> meta PT.Model _ meta _ -> meta PT.Constant _ meta _ _ -> meta - & \case + |> \case Just m -> - case PT.metaRef m of - Just ref -> referencedImports ref - Nothing -> "" + Maybe.map referencedImports (PT.metaRef m) + |> Maybe.withDefault "" Nothing -> "" ) lstDtaTypes - & List.filter (/= "") - & List.intersperse "\n" - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n" + |> List.concat docComments :: Maybe PT.MetaField -> String docComments = \case Just meta -> - case (PT.metaDoc meta) of + case PT.metaDoc meta of Just doc -> - ( "/// " + "/// " <> (List.lines (Text.unpack doc) - & List.filter (/= "") - & List.intersperse "\n/// " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n/// " + |> List.concat ) <> "\n" - ) Nothing -> "" Nothing -> "" -serializer :: PT.DataTypes -> String -serializer (PT.Constant name meta type_ value) = "" -serializer (PT.Alias name meta type_) = - ( "pub fn " - <> (Text.unpack name & Case.quietSnake) - <> "_encoder(" - <> (Text.unpack name & Case.quietSnake) - <> ": " - <> (Text.unpack name & Case.pascal) - <> ") -> Json {" - <> "\n " - <> (serializerMap type_) - <> "(" - <> (Text.unpack name & Case.quietSnake) - <> ")" - <> "\n}" - ) -serializer (PT.Enum name meta members) = - ( "pub fn " - <> (Text.unpack name & Case.quietSnake) - <> "_encoder(" - <> (Text.unpack name & Case.quietSnake) - <> ": " - <> (Text.unpack name & Case.pascal) - <> ") -> Json {" - <> "\n case " - <> (Text.unpack name & Case.quietSnake) - <> " {\n " - <> ( List.map - (\case - PT.OtherValue txt -> - ( (Text.unpack txt & Case.pascal) - <> " -> json.string(\"" - <> (Text.unpack txt & Case.quietSnake) - <> "\")" - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat - ) - <> "\n }" - <> "\n}" - ) -serializer (PT.Model name meta fields) = - ( "pub fn " - <> (Text.unpack name & Case.quietSnake) - <> "_encoder(" - <> (Text.unpack name & Case.quietSnake) - <> ": " - <> (Text.unpack name & Case.pascal) - <> ") -> Json {" - <> "\n json.object([" - <> "\n " - <> ( List.map - (\field -> - ( "#(\"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "\", " - <> (PT.fieldType field & serializerMap) - <> "(" - <> (Text.unpack name & Case.quietSnake) - <> "." - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "))" - ) - ) - fields - & List.intersperse ",\n " - & List.concat - ) - <> "\n ])" - <> "\n}" - - ) +-- serializer :: PT.DataTypes -> String +-- serializer (PT.Constant name meta type_ value) = "" +-- serializer (PT.Alias name meta type_) = +-- "pub fn " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_encoder(" +-- <> (Text.unpack name |> Case.quietSnake) +-- <> ": " +-- <> (Text.unpack name |> Case.pascal) +-- <> ") -> Json {" +-- <> "\n " +-- <> (serializerMap type_) +-- <> "(" +-- <> (Text.unpack name |> Case.quietSnake) +-- <> ")" +-- <> "\n}" +-- serializer (PT.Enum name meta members) = +-- "pub fn " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_encoder(" +-- <> (Text.unpack name |> Case.quietSnake) +-- <> ": " +-- <> (Text.unpack name |> Case.pascal) +-- <> ") -> Json {" +-- <> "\n case " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> " {\n " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( (Text.unpack txt |> Case.pascal) +-- <> " -> json.string(\"" +-- <> (Text.unpack txt |> Case.quietSnake) +-- <> "\")" +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- <> "\n }" +-- <> "\n}" +-- serializer (PT.Model name meta fields) = +-- "pub fn " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_encoder(" +-- <> (Text.unpack name |> Case.quietSnake) +-- <> ": " +-- <> (Text.unpack name |> Case.pascal) +-- <> ") -> Json {" +-- <> "\n json.object([" +-- <> "\n " +-- <> ( List.map +-- (\field -> +-- ( "#(\"" +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "\", " +-- <> (PT.fieldType field |> serializerMap) +-- <> "(" +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "." +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "))" +-- ) +-- ) +-- fields +-- |> List.intersperse ",\n " +-- |> List.concat +-- ) +-- <> "\n ])" +-- <> "\n}" -deserializer :: PT.DataTypes -> String -deserializer (PT.Constant name meta type_ value) = "" -deserializer (PT.Alias name meta type_) = - ( "pub fn " - <> (Text.unpack name & Case.quietSnake) - <> "_decoder(json: Dynamic) -> Result(" - <> (Text.unpack name & Case.pascal) - <> ", List(DecodeError)) {" - <> "\n " - <> (deserializerMap type_) - <> "(json)\n}" - ) -deserializer (PT.Enum name meta members) = - ( "pub fn " - <> (Text.unpack name & Case.quietSnake) - <> "_decoder(json: Dynamic) -> Result(" - <> (Text.unpack name & Case.pascal) - <> ", List(DecodeError)) {" - <> "\n use enum_str <- result.try(dynamic.string(json))" - <> "\n case enum_str {" - <> "\n " - <> ( List.map - (\case - PT.OtherValue txt -> - ( "\"" - <> (Text.unpack txt & Case.quietSnake) - <> "\" -> Ok(" - <> (Text.unpack txt & Case.pascal) - <> ")" - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat - ) - <> "\n err -> Error(" - <> "\n [dynamic.DecodeError(" - <> "\n \"Expected one of: " - <> ( List.map - (\case - PT.OtherValue txt -> - ( "\\\"" - <> (Text.unpack txt & Case.quietSnake) - <> "\\\"" - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse ", " - & List.concat - ) - <> "\"," - <> "\n \"Found: \" <> err," - <> "\n []" - <> "\n )]" - <> "\n )" - <> "\n }" - <> "\n}" - ) -deserializer (PT.Model name meta fields) = - ( "pub fn " - <> (Text.unpack name & Case.quietSnake) - <> "_decoder(json: Dynamic) -> Result(" - <> (Text.unpack name & Case.pascal) - <> ", List(DecodeError)) {" - <> ( List.map - (\field -> - ( "\n use " - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> " <- try(dynamic.field(\"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "\", " - <> (PT.fieldType field & deserializerMap) - <> ")(json))" - ) - ) - fields - & List.concat - ) - <> "\n Ok(" - <> (Text.unpack name & Case.pascal) - <> "(" - <> ( List.map - (\field -> - (PT.fieldName field & Text.unpack & Case.quietSnake) - ) - fields - & List.intersperse ", " - & List.concat - ) - <> "))\n}" - ) +-- deserializer :: PT.DataTypes -> String +-- deserializer (PT.Constant name meta type_ value) = "" +-- deserializer (PT.Alias name meta type_) = +-- "pub fn " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_decoder(json: Dynamic) -> Result(" +-- <> (Text.unpack name |> Case.pascal) +-- <> ", List(DecodeError)) {" +-- <> "\n " +-- <> (deserializerMap type_) +-- <> "(json)\n}" +-- deserializer (PT.Enum name meta members) = +-- "pub fn " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_decoder(json: Dynamic) -> Result(" +-- <> (Text.unpack name |> Case.pascal) +-- <> ", List(DecodeError)) {" +-- <> "\n use enum_str <- result.try(dynamic.string(json))" +-- <> "\n case enum_str {" +-- <> "\n " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( "\"" +-- <> (Text.unpack txt |> Case.quietSnake) +-- <> "\" -> Ok(" +-- <> (Text.unpack txt |> Case.pascal) +-- <> ")" +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- <> "\n err -> Error(" +-- <> "\n [dynamic.DecodeError(" +-- <> "\n \"Expected one of: " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( "\\\"" +-- <> (Text.unpack txt |> Case.quietSnake) +-- <> "\\\"" +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse ", " +-- |> List.concat +-- ) +-- <> "\"," +-- <> "\n \"Found: \" <> err," +-- <> "\n []" +-- <> "\n )]" +-- <> "\n )" +-- <> "\n }" +-- <> "\n}" +-- deserializer (PT.Model name meta fields) = +-- "pub fn " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_decoder(json: Dynamic) -> Result(" +-- <> (Text.unpack name |> Case.pascal) +-- <> ", List(DecodeError)) {" +-- <> ( List.map +-- (\field -> +-- ( "\n use " +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> " <- try(dynamic.field(\"" +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "\", " +-- <> (PT.fieldType field |> deserializerMap) +-- <> ")(json))" +-- ) +-- ) +-- fields +-- |> List.concat +-- ) +-- <> "\n Ok(" +-- <> (Text.unpack name |> Case.pascal) +-- <> "(" +-- <> ( List.map +-- (\field -> +-- (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- ) +-- fields +-- |> List.intersperse ", " +-- |> List.concat +-- ) +-- <> "))\n}" invTypeWriter :: PT.DataTypes -> String invTypeWriter (PT.Constant name meta type_ value) = - ( docComments meta + docComments meta <> "/// Temporarily a function solely because Dict's cant be constructed free-hand.\n" <> "pub fn " - <> (Text.unpack name & Case.quietSnake) + <> (Text.unpack name |> Case.quietSnake) <> "() -> " - <> (typesMap type_) + <> typesMap type_ <> " {" - <> (valuesMap value) + <> valuesMap value <> "}" - ) -- ( docComments meta -- <> "pub const " - -- <> (Text.unpack name & Case.quietSnake) + -- <> (Text.unpack name |> Case.quietSnake) -- <> ": " -- <> (typesMap type_) -- <> " = " -- <> (valuesMap value) -- ) invTypeWriter (PT.Alias name meta type_) = - ( docComments meta + docComments meta <> "pub type " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> " = " - <> (typesMap type_) + <> typesMap type_ <> "\n\n\n" - <> serializer (PT.Alias name meta type_) + -- <> serializer (PT.Alias name meta type_) <> "\n\n\n" - <> deserializer (PT.Alias name meta type_) - ) + -- <> deserializer (PT.Alias name meta type_) invTypeWriter (PT.Enum name meta members) = - ( "pub type " - <> (Text.unpack name & Case.pascal) + "pub type " + <> (Text.unpack name |> Case.pascal) <> " {\n " <> ( members - & List.map (\m -> valuesMap m & Case.pascal) - & List.intersperse "\n " - & List.concat + |> List.map (\m -> valuesMap m |> Case.pascal) + |> List.intersperse "\n " + |> List.concat ) <> "\n}" <> "\n\n\n" - <> serializer (PT.Enum name meta members) + -- <> serializer (PT.Enum name meta members) <> "\n\n\n" - <> deserializer (PT.Enum name meta members) - ) + -- <> deserializer (PT.Enum name meta members) invTypeWriter (PT.Model name meta fields) = - ( docComments meta + docComments meta <> "pub type " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> " {\n " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> "(\n " <> ( fields - & List.map + |> List.map (\f -> - (Text.unpack (PT.fieldName f) & Case.quietSnake) + (Text.unpack (PT.fieldName f) |> Case.quietSnake) <> ": " <> typesMap (PT.fieldType f) ) - & List.intersperse ",\n " - & List.concat + |> List.intersperse ",\n " + |> List.concat ) <> "\n )\n}" <> "\n\n\n" - <> serializer (PT.Model name meta fields) + -- <> serializer (PT.Model name meta fields) <> "\n\n\n" - <> deserializer (PT.Model name meta fields) - ) + -- <> deserializer (PT.Model name meta fields) typesWriter :: [PT.DataTypes] -> String typesWriter types = List.map invTypeWriter types - & List.intersperse "\n\n\n" - & List.concat + |> List.intersperse "\n\n\n" + |> List.concat typesMap :: PT.Types -> String @@ -453,7 +436,7 @@ typesMap = \case <> ", " <> typesMap e <> ")" - PT.DefinedDataTypes t -> Text.unpack t & Case.pascal + PT.DefinedDataTypes t -> Text.unpack t |> Case.pascal valuesMap :: PT.Values -> String valuesMap = \case @@ -464,8 +447,8 @@ valuesMap = \case PT.VList lst -> "[" <> ( List.map valuesMap lst - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VDict map -> @@ -480,8 +463,8 @@ valuesMap = \case <> ")" ) (Map.toList map) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "])" PT.VNullable nll -> case nll of @@ -490,8 +473,8 @@ valuesMap = \case PT.VSet set -> "Set.fromList [" <> ( List.map valuesMap (Set.toList set) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VTuple2 (a, b) -> @@ -530,7 +513,7 @@ valuesMap = \case <> ", " <> valuesMap e <> ")" - PT.OtherValue t -> Text.unpack t & Case.pascal + PT.OtherValue t -> Text.unpack t |> Case.pascal serializerMap :: PT.Types -> String @@ -586,7 +569,7 @@ serializerMap = \case <> "(d)), #(\"fifth\", " <> serializerMap e <> "(e))])}" - PT.DefinedDataTypes txt -> (Text.unpack txt & Case.quietSnake) <> "_encoder" + PT.DefinedDataTypes txt -> (Text.unpack txt |> Case.quietSnake) <> "_encoder" deserializerMap :: PT.Types -> String @@ -648,4 +631,4 @@ deserializerMap = \case <> " use e <- try(dynamic.field(\"fifth\", " <> deserializerMap e <> ")(t5json)) Ok(#(a, b, c, d, e))}" - PT.DefinedDataTypes txt -> Text.unpack txt & Case.quietSnake & (<> "_decoder") + PT.DefinedDataTypes txt -> Text.unpack txt |> Case.quietSnake |> (<> "_decoder") diff --git a/app/Lang/Gleam/Writer.hs b/app/Lang/Gleam/Writer.hs index e59d978..b6a9a59 100644 --- a/app/Lang/Gleam/Writer.hs +++ b/app/Lang/Gleam/Writer.hs @@ -7,7 +7,6 @@ import Data.Version (Version, makeVersion, import Lang.Gleam.Config (LangConf (..)) import Lang.Gleam.Versions.GleamOneZeroZero as G1_0_0 import qualified Parsing.Types as PT -import System.IO (FilePath) gleamWriter :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () gleamWriter langConf path typeData = let diff --git a/app/Lang/Haskell/Config.hs b/app/Lang/Haskell/Config.hs index 8f94693..aecf1ea 100644 --- a/app/Lang/Haskell/Config.hs +++ b/app/Lang/Haskell/Config.hs @@ -3,7 +3,6 @@ module Lang.Haskell.Config where import Data.Version (Version) import Instances import Parsing.Types (DataTypes) -import System.IO (FilePath) import Text.Show.Functions data LangConf = diff --git a/app/Lang/Haskell/Toml.hs b/app/Lang/Haskell/Toml.hs index acc8670..7d9f1e6 100644 --- a/app/Lang/Haskell/Toml.hs +++ b/app/Lang/Haskell/Toml.hs @@ -3,7 +3,6 @@ module Lang.Haskell.Toml where import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) import qualified Data.Maybe as Maybe import Instances import Lang.Haskell.Config (LangConf (..)) @@ -18,7 +17,7 @@ confCodec = LangConf <*> Toml.string "output_location" .= outputLocation <*> Extras.versionCodec "lang_version" .= langVersion <*> ( Toml.dioptional (Toml.bool "use_text_over_string") - & Toml.dimap + |> Toml.dimap (Just) (Maybe.fromMaybe False) ) .= useTextOverString diff --git a/app/Lang/Haskell/Versions/GHCNineEightOne.hs b/app/Lang/Haskell/Versions/GHCNineEightOne.hs index b1efe01..f4af871 100644 --- a/app/Lang/Haskell/Versions/GHCNineEightOne.hs +++ b/app/Lang/Haskell/Versions/GHCNineEightOne.hs @@ -1,16 +1,17 @@ module Lang.Haskell.Versions.GHCNineEightOne (writer) where +import qualified Antelude.File as File +import qualified Antelude.List as List +import qualified Antelude.Maybe as Maybe import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) -import qualified Data.List as List +import qualified Control.Monad as Monad import qualified Data.Map.Strict as Map import qualified Data.Set as Set import qualified Data.Text as Text import Lang.Haskell.Config (LangConf (..)) import qualified Parsing.Types as PT import qualified System.Directory as Dir -import System.IO (FilePath) import qualified Text.Casing as Case @@ -18,19 +19,19 @@ import qualified Text.Casing as Case writer :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () writer haskellConf baseDir = let path :: FilePath -> FilePath - path pth = (baseDir <> (outputLocation haskellConf) <> haskellDirPath pth) + path pth = (baseDir <> outputLocation haskellConf <> haskellDirPath pth) in - mapM_ + Monad.mapM_ (\(pth, lstType) -> Com.createWritePath (baseDir - <> (outputLocation haskellConf) + <> outputLocation haskellConf <> "/DTT/" <> haskellDirPath pth ) - >> writeFile + >> File.writeFile (baseDir - <> (outputLocation haskellConf) + <> outputLocation haskellConf <> "/DTT/" <> haskellFilePath pth ) @@ -43,17 +44,13 @@ writer haskellConf baseDir = let ) haskellDirPath :: FilePath -> FilePath -haskellDirPath fp = - Com.typeFilePathToLangDirPath +haskellDirPath = Com.typeFilePathToLangDirPath Com.PascalCase - fp haskellFilePath :: FilePath -> FilePath -haskellFilePath fp = - Com.typeFilePathToLangFilePath +haskellFilePath = Com.typeFilePathToLangFilePath Com.PascalCase ".hs" - fp haskellCodePath :: FilePath -> String haskellCodePath = Com.typeFilePathToLangCodePath @@ -63,15 +60,14 @@ haskellCodePath = Com.typeFilePathToLangCodePath header :: FilePath -> String header fp = - ( "module DTT." + "module DTT." <> haskellCodePath fp <> " where" <> "\n\n{- This file was generated by DataTypeTool -}\n\n" - ) staticImports :: String staticImports = - ( "import Data.Map.Strict (Map)\n" + "import Data.Map.Strict (Map)\n" <> "import qualified Data.Map.Strict as Map\n" <> "import Data.Set (Set)\n" <> "import qualified Data.Set as Set\n" @@ -83,7 +79,6 @@ staticImports = <> "import Data.Function ((&))\n" <> "import Control.Applicative ((<|>))\n" <> "import Control.Monad.Fail (fail)\n" - ) mapReferencedImports :: (FilePath, [PT.DataTypes]) -> String mapReferencedImports (_, lstDtaTypes) = let @@ -95,8 +90,8 @@ mapReferencedImports (_, lstDtaTypes) = let <> haskellCodePath fp ) metaStuff - & List.intersperse "\n" - & List.concat + |> List.intersperse "\n" + |> List.concat ) <> "\n" in @@ -107,32 +102,30 @@ mapReferencedImports (_, lstDtaTypes) = let PT.Enum _ meta _ -> meta PT.Model _ meta _ -> meta PT.Constant _ meta _ _ -> meta - & \case + |> \case Just m -> - case PT.metaRef m of - Just ref -> referencedImports ref - Nothing -> "" + Maybe.map referencedImports (PT.metaRef m) + |> Maybe.withDefault "" Nothing -> "" ) lstDtaTypes - & List.filter (/= "") - & List.intersperse "\n" - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n" + |> List.concat docComments :: Maybe PT.MetaField -> String docComments = \case Just meta -> - case (PT.metaDoc meta) of + case PT.metaDoc meta of Just doc -> - ( "{- |\n " + "{- |\n " <> ( List.lines (Text.unpack doc) - & List.filter (/= "") - & List.intersperse "\n " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n " + |> List.concat ) <> "\n-}\n" - ) Nothing -> "" Nothing -> "" @@ -140,204 +133,192 @@ docComments = \case serializer :: PT.DataTypes -> String serializer (PT.Constant name meta type_ value) = "" serializer (PT.Alias name meta type_) = - ( (Text.unpack name & Case.camel) + (Text.unpack name |> Case.camel) <> "Encoder :: " - <>(Text.unpack name & Case.pascal) + <>(Text.unpack name |> Case.pascal) <> " -> AesonT.Value\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> "Encoder = " - <> (serializerMap type_) - ) + <> serializerMap type_ serializer (PT.Enum name meta members) = - ( (Text.unpack name & Case.camel) + (Text.unpack name |> Case.camel) <> "Encoder :: " - <>(Text.unpack name & Case.pascal) + <>(Text.unpack name |> Case.pascal) <> " -> AesonT.Value\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> "Encoder t = case t of\n " <> ( List.map (\case PT.OtherValue txt -> - ( (Text.unpack txt) + ( Text.unpack txt <> " -> Aeson.toJSON \"" - <> (Case.quietSnake $ Text.unpack txt) + <> (Case.quietSnake <| Text.unpack txt) <> "\"" ) _ -> "" ) members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n " + |> List.concat ) - ) serializer (PT.Model name meta fields) = - ( (Text.unpack name & Case.camel) + (Text.unpack name |> Case.camel) <> "Encoder :: " - <>(Text.unpack name & Case.pascal) + <>(Text.unpack name |> Case.pascal) <> " -> AesonT.Value\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> "Encoder val = Aeson.object" <> "\n [ " <> ( List.map (\field -> - ( "(fromString \"" - <> (Case.quietSnake $ Text.unpack $ PT.fieldName field) + "(fromString \"" + <> (Case.quietSnake <| Text.unpack <| PT.fieldName field) <> "\" :: Aeson.Key) Aeson..= (" - <> (Text.unpack name & Case.camel) - <> (Text.unpack (PT.fieldName field) & Case.pascal) - <> " val & " - <> (serializerMap $ PT.fieldType field) + <> (Text.unpack name |> Case.camel) + <> (Text.unpack (PT.fieldName field) |> Case.pascal) + <> " val |> " + <> (serializerMap <| PT.fieldType field) <> ")" - ) ) fields - & List.intersperse "\n , " - & List.concat + |> List.intersperse "\n , " + |> List.concat ) <> "\n ]" - ) deserializer :: PT.DataTypes -> String deserializer (PT.Constant name meta type_ value) = "" deserializer (PT.Alias name meta type_) = - ( (Text.unpack name & Case.camel) + (Text.unpack name |> Case.camel) <> "Decoder :: " <> "AesonT.Value -> AesonT.Parser (" - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> ")\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> "Decoder =\n " - <> (deserializerMap type_) - ) + <> deserializerMap type_ deserializer (PT.Enum name meta members) = - ( (Text.unpack name & Case.camel) + (Text.unpack name |> Case.camel) <> "Decoder :: " <> "AesonT.Value -> AesonT.Parser (" - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> ")\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> "Decoder = Aeson.withText \"" - <> (Text.unpack name & Case.pascal) - <> "\"\n (\\t -> Text.unpack t & " + <> (Text.unpack name |> Case.pascal) + <> "\"\n (\\t -> Text.unpack t |> " <> "\\a -> case a of\n " <> ( List.map (\case PT.OtherValue txt -> ( "\"" - <> (Case.quietSnake $ Text.unpack txt) - <> "\" -> return $ " - <> (Text.unpack txt) + <> (Case.quietSnake <| Text.unpack txt) + <> "\" -> return <| " + <> Text.unpack txt ) _ -> "" ) members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n " + |> List.concat ) <> "\n _ -> fail \"Invalid string for type " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> "\"\n )" - ) deserializer (PT.Model name meta fields) = - ( (Text.unpack name & Case.camel) + (Text.unpack name |> Case.camel) <> "Decoder :: " <> "AesonT.Value -> AesonT.Parser (" - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> ")\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> "Decoder = Aeson.withObject \"" - <> (Text.unpack name & Case.pascal) - <> "\" $ \\value -> " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) + <> "\" <| \\value -> " + <> (Text.unpack name |> Case.pascal) <> "\n <$> " <> ( List.map (\field -> - ( "(value Aeson..: (fromString \"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) + "(value Aeson..: (fromString \"" + <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) <> "\" :: Aeson.Key) >>= (" - <> (deserializerMap $ PT.fieldType field) + <> (deserializerMap <| PT.fieldType field) <> "))" - ) ) fields - & List.intersperse "\n <*> " - & List.concat + |> List.intersperse "\n <*> " + |> List.concat ) - ) invTypeWriter :: PT.DataTypes -> String invTypeWriter (PT.Constant name meta type_ value) = - ( docComments meta - <> (Text.unpack name & Case.camel) + docComments meta + <> (Text.unpack name |> Case.camel) <> " :: " - <> (typesMap type_) + <> typesMap type_ <> "\n" - <> (Text.unpack name & Case.camel) + <> (Text.unpack name |> Case.camel) <> " = " - <> (valuesMap value) - ) + <> valuesMap value invTypeWriter (PT.Alias name meta type_) = - ( docComments meta + docComments meta <> "type " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> " = " - <> (typesMap type_) + <> typesMap type_ <> "\n\n\n" <> serializer (PT.Alias name meta type_) <> "\n\n\n" <> deserializer (PT.Alias name meta type_) - ) invTypeWriter (PT.Enum name meta members) = - ( docComments meta + docComments meta <> "data " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> "\n = " <> ( members - & List.map (\m -> valuesMap m) - & List.intersperse "\n | " - & List.concat + |> List.map valuesMap + |> List.intersperse "\n | " + |> List.concat ) <> "\n\n\n" <> serializer (PT.Enum name meta members) <> "\n\n\n" <> deserializer (PT.Enum name meta members) - ) invTypeWriter (PT.Model name meta fields) = - ( docComments meta + docComments meta <> "data " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> " = " - <> (Text.unpack name & Case.pascal) + <> (Text.unpack name |> Case.pascal) <> "\n" <> " { " <> ( fields - & List.map + |> List.map (\f -> - (Text.unpack name & Case.camel) - <> (Text.unpack (PT.fieldName f) & Case.pascal) + (Text.unpack name |> Case.camel) + <> (Text.unpack (PT.fieldName f) |> Case.pascal) <> " :: " <> typesMap (PT.fieldType f) ) - & List.intersperse "\n , " - & List.concat + |> List.intersperse "\n , " + |> List.concat ) <> "\n }" <> "\n\n\n" <> serializer (PT.Model name meta fields) <> "\n\n\n" <> deserializer (PT.Model name meta fields) - ) typesWriter :: [PT.DataTypes] -> String typesWriter types = List.map invTypeWriter types - & List.intersperse "\n\n\n" - & List.concat + |> List.intersperse "\n\n\n" + |> List.concat typesMap :: PT.Types -> String @@ -386,7 +367,7 @@ typesMap = \case <> ", " <> typesMap e <> ")" - PT.DefinedDataTypes t -> Text.unpack t & Case.pascal + PT.DefinedDataTypes t -> Text.unpack t |> Case.pascal valuesMap :: PT.Values -> String valuesMap = \case @@ -397,8 +378,8 @@ valuesMap = \case PT.VList lst -> "[" <> ( List.map valuesMap lst - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VDict map -> @@ -412,8 +393,8 @@ valuesMap = \case <> ")" ) (Map.toList map) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VNullable nll -> case nll of @@ -422,8 +403,8 @@ valuesMap = \case PT.VSet set -> "Set.fromList [" <> ( List.map valuesMap (Set.toList set) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VTuple2 (a, b) -> @@ -462,7 +443,7 @@ valuesMap = \case <> ", " <> valuesMap e <> ")" - PT.OtherValue t -> Text.unpack t & Case.pascal + PT.OtherValue t -> Text.unpack t |> Case.pascal serializerMap :: PT.Types -> String @@ -473,72 +454,72 @@ serializerMap = \case PT.DTTBool -> "(Aeson.toJSON)" PT.DTTList a -> "(\\l -> AesonT.listValue (" - <> (serializerMap a) + <> serializerMap a <> ") l) " PT.DTTDict v -> "(\\d -> Aeson.toJSON d) " PT.DTTNullable a -> "(Aeson.toJSON)" PT.DTTSet a -> "(\\a -> Aeson.object " <> "[ (fromString \"set\" :: Aeson.Key) Aeson..= " - <> (serializerMap a) + <> serializerMap a <> "a])" PT.DTTTuple2 a b -> "(\\(a, b) -> Aeson.object " <> "[ ((fromString \"first\" :: Aeson.Key) Aeson..= " - <> (serializerMap a) + <> serializerMap a <> " a)" <> ", ((fromString \"second\" :: Aeson.Key) Aeson..= " - <> (serializerMap b) + <> serializerMap b <> " b)" <> "])" PT.DTTTuple3 a b c -> "(\\(a, b, c) -> Aeson.object " <> "[ ((fromString \"first\" :: Aeson.Key) Aeson..= " - <> (serializerMap a) + <> serializerMap a <> " a)" <> ", ((fromString \"second\" :: Aeson.Key) Aeson..= " - <> (serializerMap b) + <> serializerMap b <> " b)" <> ", ((fromString \"third\" :: Aeson.Key) Aeson..= " - <> (serializerMap c) + <> serializerMap c <> " c)" <> "])" PT.DTTTuple4 a b c d -> "(\\(a, b, c, d) -> Aeson.object " <> "[ ((fromString \"first\" :: Aeson.Key) Aeson..= " - <> (serializerMap a) + <> serializerMap a <> " a)" <> ", ((fromString \"second\" :: Aeson.Key) Aeson..= " - <> (serializerMap b) + <> serializerMap b <> " b)" <> ", ((fromString \"third\" :: Aeson.Key) Aeson..= " - <> (serializerMap c) + <> serializerMap c <> " c)" <> ", ((fromString \"fourth\" :: Aeson.Key) Aeson..= " - <> (serializerMap d) + <> serializerMap d <> " d)" <> "])" PT.DTTTuple5 a b c d e -> "(\\(a, b, c, d, e) -> Aeson.object " <> "[ ((fromString \"first\" :: Aeson.Key) Aeson..= " - <> (serializerMap a) + <> serializerMap a <> " a)" <> ", ((fromString \"second\" :: Aeson.Key) Aeson..= " - <> (serializerMap b) + <> serializerMap b <> " b)" <> ", ((fromString \"third\" :: Aeson.Key) Aeson..= " - <> (serializerMap c) + <> serializerMap c <> " c)" <> ", ((fromString \"fourth\" :: Aeson.Key) Aeson..= " - <> (serializerMap d) + <> serializerMap d <> " d)" <> ", ((fromString \"fifth\" :: Aeson.Key) Aeson..= " - <> (serializerMap e) + <> serializerMap e <> " e)" <> "])" PT.DefinedDataTypes txt -> "(" - <> (Text.unpack txt & Case.camel) + <> (Text.unpack txt |> Case.camel) <> "Encoder) " @@ -550,68 +531,68 @@ deserializerMap = \case PT.DTTBool -> "(Aeson.parseJSON)" PT.DTTList a -> "(AesonT.listParser (" - <> (deserializerMap a) + <> deserializerMap a <> ")) " PT.DTTDict v -> "(Aeson.parseJSON)" PT.DTTNullable a -> "(Aeson.parseJSON)" PT.DTTSet a -> - "(Aeson.withObject \"Set\" $ \\v ->" + "(Aeson.withObject \"Set\" <| \\v ->" <> "v Aeson..: (fromString \"set\" :: Aeson.Key) " <> ">>= AesonT.listParser (" - <> (deserializerMap a) + <> deserializerMap a <> ") " - <> ">>= return . Set.fromList)" + <> ">>= return <. Set.fromList)" PT.DTTTuple2 a b -> - "(Aeson.withObject \"Tuple2\" $ \\v -> (,) " + "(Aeson.withObject \"Tuple2\" <| \\v -> (,) " <> "<$> (v Aeson..: (fromString \"first\" :: Aeson.Key) >>= (" - <> (deserializerMap a) + <> deserializerMap a <> ")) " <> "<*> (v Aeson..: (fromString \"second\" :: Aeson.Key) >>= (" - <> (deserializerMap b) + <> deserializerMap b <> ")))" PT.DTTTuple3 a b c -> - "(Aeson.withObject \"Tuple3\" $ \\v -> (,,) " + "(Aeson.withObject \"Tuple3\" <| \\v -> (,,) " <> "<$> (v Aeson..: (fromString \"first\" :: Aeson.Key) >>= (" - <> (deserializerMap a) + <> deserializerMap a <> ")) " <> "<*> (v Aeson..: (fromString \"second\" :: Aeson.Key) >>= (" - <> (deserializerMap b) + <> deserializerMap b <> ")) " <> "<*> (v Aeson..: (fromString \"third\" :: Aeson.Key) >>= (" - <> (deserializerMap c) + <> deserializerMap c <> ")))" PT.DTTTuple4 a b c d -> - "(Aeson.withObject \"Tuple4\" $ \\v -> (,,,) " + "(Aeson.withObject \"Tuple4\" <| \\v -> (,,,) " <> "<$> (v Aeson..: (fromString \"first\" :: Aeson.Key) >>= (" - <> (deserializerMap a) + <> deserializerMap a <> ")) " <> "<*> (v Aeson..: (fromString \"second\" :: Aeson.Key) >>= (" - <> (deserializerMap b) + <> deserializerMap b <> ")) " <> "<*> (v Aeson..: (fromString \"third\" :: Aeson.Key) >>= (" - <> (deserializerMap c) + <> deserializerMap c <> ")) " <> "<*> (v Aeson..: (fromString \"fourth\" :: Aeson.Key) >>= (" - <> (deserializerMap d) + <> deserializerMap d <> ")))" PT.DTTTuple5 a b c d e -> - "(Aeson.withObject \"Tuple5\" $ \\v -> (,,,,) " + "(Aeson.withObject \"Tuple5\" <| \\v -> (,,,,) " <> "<$> (v Aeson..: (fromString \"first\" :: Aeson.Key) >>= (" - <> (deserializerMap a) + <> deserializerMap a <> ")) " <> "<*> (v Aeson..: (fromString \"second\" :: Aeson.Key) >>= (" - <> (deserializerMap b) + <> deserializerMap b <> ")) " <> "<*> (v Aeson..: (fromString \"third\" :: Aeson.Key) >>= (" - <> (deserializerMap c) + <> deserializerMap c <> ")) " <> "<*> (v Aeson..: (fromString \"fourth\" :: Aeson.Key) >>= (" - <> (deserializerMap d) + <> deserializerMap d <> ")) " <> "<*> (v Aeson..: (fromString \"fifth\" :: Aeson.Key) >>= (" - <> (deserializerMap e) + <> deserializerMap e <> ")))" PT.DefinedDataTypes txt -> "(" - <> (Text.unpack txt & Case.camel) + <> (Text.unpack txt |> Case.camel) <> "Decoder) " diff --git a/app/Lang/Haskell/Writer.hs b/app/Lang/Haskell/Writer.hs index e35eb7e..ef9fe28 100644 --- a/app/Lang/Haskell/Writer.hs +++ b/app/Lang/Haskell/Writer.hs @@ -7,7 +7,6 @@ import Data.Version (Version, makeVersion, import Lang.Haskell.Config (LangConf (..)) import qualified Lang.Haskell.Versions.GHCNineEightOne as GHC9_8_1 import qualified Parsing.Types as PT -import System.IO (FilePath) haskellWriter :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () haskellWriter langConf path typeData = let diff --git a/app/Lang/Python/Config.hs b/app/Lang/Python/Config.hs index eb952c4..c7b31b7 100644 --- a/app/Lang/Python/Config.hs +++ b/app/Lang/Python/Config.hs @@ -3,7 +3,6 @@ module Lang.Python.Config where import Data.Version (Version) import Instances import Parsing.Types (DataTypes) -import System.IO (FilePath) import Text.Show.Functions data LangConf = diff --git a/app/Lang/Python/Toml.hs b/app/Lang/Python/Toml.hs index 5a86c76..7c0ecf0 100644 --- a/app/Lang/Python/Toml.hs +++ b/app/Lang/Python/Toml.hs @@ -4,7 +4,6 @@ module Lang.Python.Toml where import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) import Instances import Lang.Python.Config (LangConf (..)) import qualified Lang.Python.Versions.CPythonThreeTenTwelve as CPy3_10_12 diff --git a/app/Lang/Python/Versions/CPythonThreeTenTwelve.hs b/app/Lang/Python/Versions/CPythonThreeTenTwelve.hs index e25d091..ddfedca 100644 --- a/app/Lang/Python/Versions/CPythonThreeTenTwelve.hs +++ b/app/Lang/Python/Versions/CPythonThreeTenTwelve.hs @@ -1,10 +1,11 @@ module Lang.Python.Versions.CPythonThreeTenTwelve where - +import qualified Antelude.File as File +import qualified Antelude.List as List +import qualified Antelude.Maybe as Maybe import qualified Common.Utils as Com import qualified Config.Extras as Extras -import Data.Function ((&)) -import qualified Data.List as List +import qualified Control.Monad as Monad import qualified Data.Map.Strict as Map import qualified Data.Set as Set import qualified Data.Text as Text @@ -19,9 +20,9 @@ import qualified Text.Casing as Case writer :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () writer pythonConf baseDir = let path :: FilePath -> FilePath - path pth = (baseDir <> (outputLocation pythonConf) <> pythonDirPath pth) + path pth = (baseDir <> outputLocation pythonConf <> pythonDirPath pth) in - mapM_ + Monad.mapM_ (\(pth, lstType) -> Dir.createDirectoryIfMissing True @@ -30,7 +31,7 @@ writer pythonConf baseDir = let <> "/dtt/" <> pythonDirPath pth ) - >> writeFile + >> File.writeFile ( baseDir <> outputLocation pythonConf <> "/dtt/" @@ -43,24 +44,20 @@ writer pythonConf baseDir = let <> ( List.map invTypeWriter lstType - & List.intersperse "\n\n\n" - & List.concat + |> List.intersperse "\n\n\n" + |> List.concat ) ) ) pythonDirPath :: FilePath -> FilePath -pythonDirPath fp = - Com.typeFilePathToLangDirPath +pythonDirPath = Com.typeFilePathToLangDirPath Com.QuietSnakeCase - fp pythonFilePath :: FilePath -> FilePath -pythonFilePath fp = - Com.typeFilePathToLangFilePath +pythonFilePath = Com.typeFilePathToLangFilePath Com.QuietSnakeCase ".py" - fp -- for the header of files, imports, etc pythonCodePath :: FilePath -> String @@ -76,11 +73,10 @@ header = staticImports :: String staticImports = - ( "from json import (JSONDecodeError, loads, dumps)\n" + "from json import (JSONDecodeError, loads, dumps)\n" <> "from enum import Enum\n" <> "from dataclasses import dataclass\n" <> "import typing\n" - ) mapReferencedImports :: (FilePath, [PT.DataTypes]) -> String mapReferencedImports (_, lstDtaTypes) = let @@ -92,25 +88,25 @@ mapReferencedImports (_, lstDtaTypes) = let <> pythonCodePath fp <> " import (" <> ( lstTypes - & List.map + |> List.map (\case PT.DefinedDataTypes txt -> - (typesMap $ PT.DefinedDataTypes txt) + (typesMap <| PT.DefinedDataTypes txt) <> ", " - <> (Text.unpack txt & Case.quietSnake) + <> (Text.unpack txt |> Case.quietSnake) <> "_encoder, " - <> (deserializerMap $ PT.DefinedDataTypes txt) + <> (deserializerMap <| PT.DefinedDataTypes txt) _ -> "" ) - & List.filter (/= "") - & List.intersperse ", " - & List.concat + |> List.filter (/= "") + |> List.intersperse ", " + |> List.concat ) <> ")" ) metaStuff - & List.intersperse "\n" - & List.concat + |> List.intersperse "\n" + |> List.concat ) <> "\n" in @@ -121,273 +117,257 @@ mapReferencedImports (_, lstDtaTypes) = let PT.Enum _ meta _ -> meta PT.Model _ meta _ -> meta PT.Constant _ meta _ _ -> meta - & \case + |> \case Just m -> - case PT.metaRef m of - Just ref -> referencedImports ref - Nothing -> "" + Maybe.map referencedImports (PT.metaRef m) + |> Maybe.withDefault "" Nothing -> "" ) lstDtaTypes - & List.filter (/= "") - & List.intersperse "\n" - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n" + |> List.concat docCommentsString :: Maybe PT.MetaField -> String docCommentsString = \case Just meta -> - case (PT.metaDoc meta) of + case PT.metaDoc meta of Just doc -> - ( "\n \"\"\"\n " + "\n \"\"\"\n " <> (List.lines (Text.unpack doc) - & List.filter (/= "") - & List.intersperse "\n " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n " + |> List.concat ) <> "\n \"\"\"" - ) Nothing -> "" Nothing -> "" docCommentsHash :: Maybe PT.MetaField -> String docCommentsHash = \case Just meta -> - case (PT.metaDoc meta) of + case PT.metaDoc meta of Just doc -> - ( "# " + "# " <> (List.lines (Text.unpack doc) - & List.filter (/= "") - & List.intersperse "\n# " - & List.concat + |> List.filter (/= "") + |> List.intersperse "\n# " + |> List.concat ) <> "\n" - ) Nothing -> "" Nothing -> "" -serializer :: PT.DataTypes -> String -serializer (PT.Constant name meta type_ value) = "" -serializer (PT.Alias name meta type_) = - ( "def " - <> (Text.unpack name & Case.quietSnake) - <> "_encoder(obj: " - <> (Text.unpack name & Case.pascal) - <> ") -> str:" - <> "\n return dumps(" - <> (serializerMap type_) - <> "(obj))" - ) -serializer (PT.Enum name meta members) = - ( "def " - <> (Text.unpack name & Case.quietSnake) - <> "_encoder(obj: " - <> (Text.unpack name & Case.pascal) - <> ") -> str:" - <> "\n match obj:" - <> "\n " - <> ( List.map - (\case - PT.OtherValue txt -> - ( "case " - <> (Text.unpack name & Case.pascal) - <> "." - <> (Text.unpack txt & Case.pascal) - <> ":\n return dumps(\"" - <> (Text.unpack txt & Case.quietSnake) - <> "\")" - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat - ) - ) -serializer (PT.Model name meta fields) = - ( "def " - <> (Text.unpack name & Case.quietSnake) - <> "_encoder(obj: " - <> (Text.unpack name & Case.pascal) - <> ") -> str:" - <> "\n return dumps({" - <> "\n " - <> ( List.map - (\field -> - ( "\"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "\": " - <> (PT.fieldType field & serializerMap) - <> "(obj." - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> ")" - ) - ) - fields - & List.intersperse ",\n " - & List.concat - ) - <> "\n })" - - ) - - -deserializer :: PT.DataTypes -> String -deserializer (PT.Constant name meta type_ value) = "" -deserializer (PT.Alias name meta type_) = - ( "def " - <> (Text.unpack name & Case.quietSnake) - <> "_decoder(json_val: str | bytes | bytearray) -> " - <> (Text.unpack name & Case.pascal) - <> " | typing.NoReturn:" - <> "\n return " - <> (deserializerMap type_) - <> "(json_val) or None" - ) -deserializer (PT.Enum name meta members) = - ( "def " - <> (Text.unpack name & Case.quietSnake) - <> "_decoder(json_val: typing.Union[str, bytes, bytearray]) -> " - <> (Text.unpack name & Case.pascal) - <> " | typing.NoReturn:" - <> "\n match loads(json_val):" - <> "\n " - <> ( List.map - (\case - PT.OtherValue txt -> - ( "case \"" - <> (Text.unpack txt & Case.quietSnake) - <> "\":\n return " - <> (Text.unpack name & Case.pascal) - <> "." - <> (Text.unpack txt & Case.pascal) - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse "\n " - & List.concat - ) - <> "\n case err:" - <> "\n raise JSONDecodeError(" - <> "\n f\"Expected one of: " - <> ( List.map - (\case - PT.OtherValue txt -> - ( "\\\"" - <> (Text.unpack txt & Case.quietSnake) - <> "\\\"" - ) - _ -> "" - ) - members - & List.filter (/= "") - & List.intersperse ", " - & List.concat - ) - <> ", Found: {err}\", json_val, None" - <> "\n )" - ) -deserializer (PT.Model name meta fields) = - ( "def " - <> (Text.unpack name & Case.quietSnake) - <> "_decoder(json_val: typing.Union[str, bytes, bytearray, dict]) -> " - <> (Text.unpack name & Case.pascal) - <> " | typing.NoReturn:" - <> "\n json_dict = loads(json_val) if not isinstance(json_val, dict) else json_val\n " - <> "return " - <> (Text.unpack name & Case.pascal) - <> "(" - <> ( List.map - (\field -> - ( "\n " - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> " = " - <> (PT.fieldType field & deserializerMap) - <> "(json_dict.get(\"" - <> (PT.fieldName field & Text.unpack & Case.quietSnake) - <> "\"))" - ) - ) - fields - & List.intersperse "," - & List.concat - ) - <> "\n )" - ) +-- serializer :: PT.DataTypes -> String +-- serializer (PT.Constant name meta type_ value) = "" +-- serializer (PT.Alias name meta type_) = +-- "def " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_encoder(obj: " +-- <> (Text.unpack name |> Case.pascal) +-- <> ") -> str:" +-- <> "\n return dumps(" +-- <> serializerMap type_ +-- <> "(obj))" +-- serializer (PT.Enum name meta members) = +-- "def " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_encoder(obj: " +-- <> (Text.unpack name |> Case.pascal) +-- <> ") -> str:" +-- <> "\n match obj:" +-- <> "\n " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( "case " +-- <> (Text.unpack name |> Case.pascal) +-- <> "." +-- <> (Text.unpack txt |> Case.pascal) +-- <> ":\n return dumps(\"" +-- <> (Text.unpack txt |> Case.quietSnake) +-- <> "\")" +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- serializer (PT.Model name meta fields) = +-- "def " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_encoder(obj: " +-- <> (Text.unpack name |> Case.pascal) +-- <> ") -> str:" +-- <> "\n return dumps({" +-- <> "\n " +-- <> ( List.map +-- (\field -> +-- "\"" +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "\": " +-- <> (PT.fieldType field |> serializerMap) +-- <> "(obj." +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> ")" +-- ) +-- fields +-- |> List.intersperse ",\n " +-- |> List.concat +-- ) +-- <> "\n })" + + +-- deserializer :: PT.DataTypes -> String +-- deserializer (PT.Constant name meta type_ value) = "" +-- deserializer (PT.Alias name meta type_) = +-- "def " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_decoder(json_val: str | bytes | bytearray) -> " +-- <> (Text.unpack name |> Case.pascal) +-- <> " | typing.NoReturn:" +-- <> "\n return " +-- <> deserializerMap type_ +-- <> "(json_val) or None" +-- deserializer (PT.Enum name meta members) = +-- "def " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_decoder(json_val: typing.Union[str, bytes, bytearray]) -> " +-- <> (Text.unpack name |> Case.pascal) +-- <> " | typing.NoReturn:" +-- <> "\n match loads(json_val):" +-- <> "\n " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( "case \"" +-- <> (Text.unpack txt |> Case.quietSnake) +-- <> "\":\n return " +-- <> (Text.unpack name |> Case.pascal) +-- <> "." +-- <> (Text.unpack txt |> Case.pascal) +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse "\n " +-- |> List.concat +-- ) +-- <> "\n case err:" +-- <> "\n raise JSONDecodeError(" +-- <> "\n f\"Expected one of: " +-- <> ( List.map +-- (\case +-- PT.OtherValue txt -> +-- ( "\\\"" +-- <> (Text.unpack txt |> Case.quietSnake) +-- <> "\\\"" +-- ) +-- _ -> "" +-- ) +-- members +-- |> List.filter (/= "") +-- |> List.intersperse ", " +-- |> List.concat +-- ) +-- <> ", Found: {err}\", json_val, None" +-- <> "\n )" +-- deserializer (PT.Model name meta fields) = +-- "def " +-- <> (Text.unpack name |> Case.quietSnake) +-- <> "_decoder(json_val: typing.Union[str, bytes, bytearray, dict]) -> " +-- <> (Text.unpack name |> Case.pascal) +-- <> " | typing.NoReturn:" +-- <> "\n json_dict = loads(json_val) if not isinstance(json_val, dict) else json_val\n " +-- <> "return " +-- <> (Text.unpack name |> Case.pascal) +-- <> "(" +-- <> ( List.map +-- (\field -> +-- "\n " +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> " = " +-- <> (PT.fieldType field |> deserializerMap) +-- <> "(json_dict.get(\"" +-- <> (PT.fieldName field |> Text.unpack |> Case.quietSnake) +-- <> "\"))" +-- ) +-- fields +-- |> List.intersperse "," +-- |> List.concat +-- ) +-- <> "\n )" invTypeWriter :: PT.DataTypes -> String invTypeWriter (PT.Constant name meta type_ value) = - ( docCommentsHash meta - <> (Text.unpack name & Case.screamingSnake) + docCommentsHash meta + <> (Text.unpack name |> Case.screamingSnake) <> ": typing.Final[" - <> (typesMap type_) + <> typesMap type_ <> "] = " - <> (valuesMap value) - ) + <> valuesMap value invTypeWriter (PT.Alias name meta type_) = - ( docCommentsHash meta - <> (Text.unpack name & Case.pascal) + docCommentsHash meta + <> (Text.unpack name |> Case.pascal) <> ": typing.TypeAlias = " - <> (typesMap type_) + <> typesMap type_ <> "\n\n\n" - <> serializer (PT.Alias name meta type_) + -- <> serializer (PT.Alias name meta type_) <> "\n\n\n" - <> deserializer (PT.Alias name meta type_) - ) + -- <> deserializer (PT.Alias name meta type_) invTypeWriter (PT.Enum name meta members) = - ( "class " - <> (Text.unpack name & Case.pascal) + "class " + <> (Text.unpack name |> Case.pascal) <> "(Enum):" <> docCommentsString meta <> "\n " <> ( members - & List.map (\m -> - (valuesMap m & Case.pascal) + |> List.map (\m -> + (valuesMap m |> Case.pascal) <> " = \"" - <> (valuesMap m & Case.quietSnake) + <> (valuesMap m |> Case.quietSnake) <> "\"" ) - & List.intersperse "\n " - & List.concat + |> List.intersperse "\n " + |> List.concat ) <> "\n\n\n" - <> serializer (PT.Enum name meta members) + -- <> serializer (PT.Enum name meta members) <> "\n\n\n" - <> deserializer (PT.Enum name meta members) - ) + -- <> deserializer (PT.Enum name meta members) invTypeWriter (PT.Model name meta fields) = - ( "@dataclass\nclass " - <> (Text.unpack name & Case.pascal) + "@dataclass\nclass " + <> (Text.unpack name |> Case.pascal) <> ":" <> docCommentsString meta <> "\n " <> ( fields - & List.map + |> List.map (\f -> - (Text.unpack (PT.fieldName f) & Case.quietSnake) + (Text.unpack (PT.fieldName f) |> Case.quietSnake) <> ": " <> typesMap (PT.fieldType f) ) - & List.intersperse "\n " - & List.concat + |> List.intersperse "\n " + |> List.concat ) <> "\n\n\n" - <> serializer (PT.Model name meta fields) + -- <> serializer (PT.Model name meta fields) <> "\n\n\n" - <> deserializer (PT.Model name meta fields) - ) + -- <> deserializer (PT.Model name meta fields) typesWriter :: [PT.DataTypes] -> String typesWriter types = List.map invTypeWriter types - & List.intersperse "\n\n\n" - & List.concat + |> List.intersperse "\n\n\n" + |> List.concat typesMap :: PT.Types -> String @@ -436,7 +416,7 @@ typesMap = \case <> ", " <> typesMap e <> "]" - PT.DefinedDataTypes t -> Text.unpack t & Case.pascal + PT.DefinedDataTypes t -> Text.unpack t |> Case.pascal valuesMap :: PT.Values -> String valuesMap = \case @@ -447,8 +427,8 @@ valuesMap = \case PT.VList lst -> "[" <> ( List.map valuesMap lst - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "]" PT.VDict map -> @@ -462,18 +442,16 @@ valuesMap = \case <> ")" ) (Map.toList map) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "}" - PT.VNullable nll -> case nll of - Just j -> valuesMap j - Nothing -> "None" + PT.VNullable nll -> (Maybe.mapWithDefault "None" valuesMap nll) PT.VSet set -> "{" <> ( List.map valuesMap (Set.toList set) - & List.intersperse ", " - & List.concat + |> List.intersperse ", " + |> List.concat ) <> "}" PT.VTuple2 (a, b) -> @@ -512,7 +490,7 @@ valuesMap = \case <> ", " <> valuesMap e <> ")" - PT.OtherValue t -> Text.unpack t & Case.pascal + PT.OtherValue t -> Text.unpack t |> Case.pascal serializerMap :: PT.Types -> String @@ -567,7 +545,7 @@ serializerMap = \case <> "(t[3]), \"fifth\": " <> serializerMap b <> "(t[4])})" - PT.DefinedDataTypes txt -> "(lambda dt: loads(" <> (Text.unpack txt & Case.quietSnake) <> "_encoder(dt)))" + PT.DefinedDataTypes txt -> "(lambda dt: loads(" <> (Text.unpack txt |> Case.quietSnake) <> "_encoder(dt)))" deserializerMap :: PT.Types -> String @@ -626,4 +604,4 @@ deserializerMap = \case <> deserializerMap e <> "(tup_json.get(\"fifth\"))" <> "))" - PT.DefinedDataTypes txt -> Text.unpack txt & Case.quietSnake & (<> "_decoder") + PT.DefinedDataTypes txt -> Text.unpack txt |> Case.quietSnake |> (<> "_decoder") diff --git a/app/Lang/Python/Writer.hs b/app/Lang/Python/Writer.hs index 5cfc717..29ef2a8 100644 --- a/app/Lang/Python/Writer.hs +++ b/app/Lang/Python/Writer.hs @@ -8,7 +8,6 @@ import Data.Version (Version, import Lang.Python.Config (LangConf (..)) import qualified Lang.Python.Versions.CPythonThreeTenTwelve as CPy3_10_12 import qualified Parsing.Types as PT -import System.IO (FilePath) pythonWriter :: LangConf -> FilePath -> [(FilePath, [PT.DataTypes])] -> IO () diff --git a/app/Main.hs b/app/Main.hs index 653c42a..f46b72a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,24 +1,23 @@ module Main where +import qualified Antelude.List as List +import qualified Antelude.List.NonEmpty as NE +import qualified Antelude.Maybe as Maybe +import qualified Antelude.Monad as Monad +import Antelude.String (fromString) import qualified Cli -import qualified Common.Utils as Com -import qualified Config.Config as Config -import qualified Control.Monad as CtrlMonad -import Data.Function ((&)) -import qualified Data.List as List -import qualified Data.List.NonEmpty as NE -import qualified Data.Map.Strict as Map -import Data.String (fromString) -import qualified Data.Text as Text -import Data.Void (Void) +import qualified Common.Utils as Com +import qualified Config.Config as Config +import qualified Control.Monad as CtrlMonad +import qualified Data.Map.Strict as Map +import qualified Data.Text as Text import Instances -import qualified Options.Applicative as Opts -import qualified Parsing.Parser as Parser -import qualified Parsing.Types as PT -import qualified System.Directory as Dir -import System.FilePath (pathSeparator) -import System.IO (FilePath) -import qualified Text.Megaparsec as MP +import qualified Options.Applicative as Opts +import qualified Parsing.Parser as Parser +import qualified Parsing.Types as PT +import qualified System.Directory as Dir +import System.FilePath (pathSeparator) +import qualified Text.Megaparsec as MP import Text.Show.Functions import qualified Toml @@ -29,53 +28,53 @@ main = do where run :: Cli.Flags -> IO () run flags = do - cfg <- Toml.decodeFileEither Config.datatypetoolCodec $ Cli.tomlPath flags - - case cfg of + Toml.decodeFileEither Config.datatypetoolCodec <| Cli.tomlPath flags + >>= \case Left _ -> print "Error occurred!" Right config -> let tomlDir :: FilePath -> FilePath tomlDir tomlAbsFp = List.reverse tomlAbsFp - & List.dropWhile (/= pathSeparator) - & List.drop 1 - & List.reverse + |> List.dropWhile (/= pathSeparator) + |> List.drop 1 + |> List.reverse mkTypeDir :: FilePath -> IO FilePath mkTypeDir tomlAbsFp = Dir.canonicalizePath - ( (tomlDir tomlAbsFp) - ++ [pathSeparator] - ++ (Config.sources config) + ( tomlDir tomlAbsFp + <> [pathSeparator] + <> Config.sources config ) absTomlDir absPath = absPath - & List.reverse - & List.dropWhile (/= pathSeparator) - & List.reverse + |> List.reverse + |> List.dropWhile (/= pathSeparator) + |> List.reverse in do - absTomlPath <- Dir.canonicalizePath $ Cli.tomlPath flags + absTomlPath <- Dir.canonicalizePath <| Cli.tomlPath flags absTypeDir <- mkTypeDir absTomlPath -- Filter for valid directories - (Dir.doesDirectoryExist absTypeDir) >>= \case - True -> do - -- runParsersTest absTypeDir config - runParsers absTypeDir config - >>= mapM_ - (\(fp, res) -> case res of - Left l -> print ("Some error occurred: " <> show l) - Right r -> - mapM_ - (\trgt -> - Config.runLangWriter - trgt - (absTomlDir absTomlPath) - [(fp, r)] - ) - (NE.toList (Config.targets config)) - ) - return () - False -> return () + Dir.doesDirectoryExist absTypeDir + >>= \case + True -> do + -- runParsersTest absTypeDir config + runParsers absTypeDir config + >>= Monad.mapM_ + (\(fp, res) -> case res of + Left l -> print ("Some error occurred: " <> show l) + Right r -> + Monad.mapM_ + (\trgt -> + Config.runLangWriter + trgt + (absTomlDir absTomlPath) + [(fp, r)] + ) + (NE.toList <| Config.targets config) + ) + return () + False -> return () runParsers :: FilePath -> @@ -85,15 +84,14 @@ main = do )] runParsers typeFileDirectory config = Com.recurseDirectory typeFileDirectory - >>= return . List.map + >>= return <. List.map ( \(pth, str) -> - (case List.stripPrefix typeFileDirectory pth of - Just newPth -> newPth - Nothing -> pth - , (case List.unsnoc str of - Just (_, '\n') -> str - _ -> (str <> "\n") - ) & MP.parse (Parser.parser (Config.spacing config)) pth + (List.stripPrefix typeFileDirectory pth + |> Maybe.withDefault pth + , (case List.last str of + Just '\n' -> str + _ -> str <> "\n" + ) |> MP.parse (Parser.parser (Config.spacing config)) pth ) ) @@ -103,7 +101,7 @@ main = do IO () runParsersTest typeFileDirectory config = do Com.recurseDirectory typeFileDirectory - >>= mapM_ + >>= Monad.mapM_ ( \(pth, str) -> MP.parseTest (Parser.parser (Config.spacing config)) str ) diff --git a/app/Parsing/Parser.hs b/app/Parsing/Parser.hs index 7d28fd0..4acf127 100644 --- a/app/Parsing/Parser.hs +++ b/app/Parsing/Parser.hs @@ -1,24 +1,19 @@ -{-# LANGUAGE RecordWildCards #-} module Parsing.Parser where +import qualified Antelude.List as List +import qualified Antelude.Maybe as Maybe +import Antelude.Monad (fail, mapM, replicateM_, void) +import Antelude.String (fromString) import qualified Common.Utils as Com import qualified Config.Config as Config -import Control.Applicative ((<|>)) -import Control.Monad (mapM, replicateM_, void) -import Data.Function ((&)) -import qualified Data.List as List -import Data.Map.Strict (Map, (!)) +import qualified Data.List as UnsafeList (head) +import Data.Map.Strict ((!)) import qualified Data.Map.Strict as Map -import qualified Data.Maybe as Maybe import qualified Data.Set as Set -import Data.String (fromString) -import Data.Text (Text) import qualified Data.Text as Text -import Data.Void (Void) import qualified Parsing.Types as PT import qualified System.Directory as Dir -import System.IO (FilePath) import qualified Text.Megaparsec as MP import qualified Text.Megaparsec.Char as MPC import qualified Text.Megaparsec.Char.Lexer as MPCL @@ -51,48 +46,48 @@ parser spacing = let dataTypeParser = MPCL.nonIndented spaceAny ( MP.choice - [ MPCL.indentBlock spaceAny $ do + [ MPCL.indentBlock spaceAny <| do voidString "alias " name <- nameParser MPC.space colon return (MPCL.IndentMany - (Just (MP.mkPos $ 1 + spacePos)) - (aliasMapper name . Map.toList . Map.fromList) + (Just (MP.mkPos <| 1 + spacePos)) + (aliasMapper name <. Map.toList <. Map.fromList) aliasParser ) - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "enum " name <- nameParser MPC.space colon return (MPCL.IndentMany - (Just (MP.mkPos $ 1 + spacePos)) - (enumMapper name . Map.toList . Map.fromList) + (Just (MP.mkPos <| 1 + spacePos)) + (enumMapper name <. Map.toList <. Map.fromList) enumParser ) - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "model " name <- nameParser MPC.space colon return (MPCL.IndentMany - (Just (MP.mkPos $ 1 + spacePos)) - (modelMapper name . Map.toList . Map.fromList) + (Just (MP.mkPos <| 1 + spacePos)) + (modelMapper name <. Map.toList <. Map.fromList) modelParser ) - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "constant " name <- nameParser MPC.space colon return (MPCL.IndentMany - (Just (MP.mkPos $ 1 + spacePos)) - (constantMapper name . Map.toList . Map.fromList) + (Just (MP.mkPos <| 1 + spacePos)) + (constantMapper name <. Map.toList <. Map.fromList) constantParser ) ] @@ -102,38 +97,38 @@ parser spacing = let aliasMapper name map = let meta :: Maybe PT.MetaField meta = (List.lookup "meta" map - & \case + |> \case Just (PT.AliasMeta m) -> m _ -> Nothing ) type_ :: Maybe PT.Types type_ = (List.lookup "type" map - & \case + |> \case Just (PT.AliasType t) -> Just t _ -> Nothing ) in case type_ of - Just t -> return $ PT.Alias name meta t + Just t -> return <| PT.Alias name meta t _ -> fail "Insufficient parameters passed to alias type" aliasParser :: Parser (String, PT.AliasParams) aliasParser = MPCL.lexeme spacer - $ MP.choice - [ MPCL.indentBlock spaceAny $ do + <| MP.choice + [ MPCL.indentBlock spaceAny <| do voidString "meta:" indentWrapper "meta" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.AliasMeta . metaMapper . Map.toList . Map.fromList) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.AliasMeta <. metaMapper <. Map.toList <. Map.fromList) metaParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "type:" indentWrapper "type" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.AliasType . List.head) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.AliasType <. UnsafeList.head) typeParser ] @@ -141,38 +136,38 @@ parser spacing = let enumMapper name map = let meta :: Maybe PT.MetaField meta = (List.lookup "meta" map - & \case + |> \case Just (PT.EnumMeta m) -> m _ -> Nothing ) members :: Maybe [PT.Values] members = (List.lookup "members" map - & \case + |> \case Just (PT.EnumMembers e) -> Just e _ -> Nothing ) in case members of - Just m -> return $ PT.Enum name meta m + Just m -> return <| PT.Enum name meta m _ -> fail "Insufficient parameters passed to enum type" enumParser :: Parser (String, PT.EnumParams) enumParser = MPCL.lexeme spacer - $ MP.choice - [ MPCL.indentBlock spaceAny $ do + <| MP.choice + [ MPCL.indentBlock spaceAny <| do voidString "meta:" indentWrapper "meta" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.EnumMeta . metaMapper . Map.toList . Map.fromList) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.EnumMeta <. metaMapper <. Map.toList <. Map.fromList) metaParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "members:" indentWrapper "members" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.EnumMembers) + (MP.mkPos <| 1 + (spacePos * 2)) + PT.EnumMembers valueParser ] @@ -180,67 +175,67 @@ parser spacing = let modelMapper name map = let meta :: Maybe PT.MetaField meta = (List.lookup "meta" map - & \case + |> \case Just (PT.ModelMeta m) -> m _ -> Nothing ) fields :: Maybe [PT.ModelField] fields = (List.lookup "fields" map - & \case + |> \case Just (PT.ModelFields f) -> Just f _ -> Nothing ) in case fields of - Just f -> return $ PT.Model name meta f + Just f -> return <| PT.Model name meta f _ -> fail "Insufficient parameters passed to model type" modelParser :: Parser (String, PT.ModelParams) modelParser = MPCL.lexeme spacer - $ MP.choice - [ MPCL.indentBlock spaceAny $ do + <| MP.choice + [ MPCL.indentBlock spaceAny <| do voidString "meta:" indentWrapper "meta" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.ModelMeta . metaMapper . Map.toList . Map.fromList) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.ModelMeta <. metaMapper <. Map.toList <. Map.fromList) metaParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "fields:" indentWrapper "fields" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.ModelFields) + (MP.mkPos <| 1 + (spacePos * 2)) + PT.ModelFields modelFieldsParser ] modelFieldsParser :: Parser PT.ModelField modelFieldsParser = MPCL.lexeme spacer - $ MPCL.indentBlock spaceAny $ do + <| MPCL.indentBlock spaceAny <| do voidString "field " name <- nameParser MPC.space colon return (MPCL.IndentMany - (Just $ (MP.mkPos $ 1 + (spacePos * 3))) - (return . fieldMapper name) + (Just <| (MP.mkPos <| 1 + (spacePos * 3))) + (return <. fieldMapper name) fieldParser ) fieldMapper :: Text -> [PT.Types] -> PT.ModelField fieldMapper name types = - PT.ModelField name (List.head types) + PT.ModelField name (UnsafeList.head types) fieldParser :: Parser PT.Types fieldParser = MPCL.lexeme spacer - $ MPCL.indentBlock spaceAny $ do + <| MPCL.indentBlock spaceAny <| do voidString "type:" return (MPCL.IndentMany - (Just $ (MP.mkPos $ 1 + (spacePos * 4))) - (return . List.head) + (Just <| (MP.mkPos <| 1 + (spacePos * 4))) + (return <. UnsafeList.head) typeParser ) @@ -248,52 +243,52 @@ parser spacing = let constantMapper name map = let meta :: Maybe PT.MetaField meta = (List.lookup "meta" map - & \case + |> \case Just (PT.ConstantMeta m) -> m _ -> Nothing ) type_ :: Maybe PT.Types type_ = (List.lookup "type" map - & \case + |> \case Just (PT.ConstantType t) -> Just t _ -> Nothing ) value :: Maybe PT.Values value = (List.lookup "value" map - & \case + |> \case Just (PT.ConstantValue v) -> Just v _ -> Nothing ) in case (type_, value) of - (Just t, Just v) -> return $ PT.Constant name meta t v + (Just t, Just v) -> return <| PT.Constant name meta t v _ -> fail "Insufficient parameters passed to constant type" constantParser :: Parser (String, PT.ConstantParams) constantParser = MPCL.lexeme spacer - $ MP.choice - [ MPCL.indentBlock spaceAny $ do + <| MP.choice + [ MPCL.indentBlock spaceAny <| do voidString "meta:" indentWrapper "meta" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.ConstantMeta . metaMapper . Map.toList . Map.fromList) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.ConstantMeta <. metaMapper <. Map.toList <. Map.fromList) metaParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "type:" indentWrapper "type" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.ConstantType . List.head) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.ConstantType <. UnsafeList.head) typeParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "value:" indentWrapper "value" - (MP.mkPos $ 1 + (spacePos * 2)) - (PT.ConstantValue . List.head) + (MP.mkPos <| 1 + (spacePos * 2)) + (PT.ConstantValue <. UnsafeList.head) valueParser ] @@ -301,70 +296,70 @@ parser spacing = let metaMapper map = let doc :: Maybe Text doc = (List.lookup "doc" map - & \case + |> \case Just (PT.MetaDoc d) -> d _ -> Nothing ) ref :: Maybe [(FilePath, [PT.Types])] ref = (List.lookup "ref" map - & \case + |> \case Just (PT.MetaRef r) -> r _ -> Nothing ) lang :: Maybe [Text] lang = (List.lookup "lang" map - & \case + |> \case Just (PT.MetaLang l) -> l _ -> Nothing ) in case (doc, ref, lang) of (Nothing, Nothing, Nothing) -> fail "No parameters passed to meta field, but a meta field was opened." - _ -> return $ PT.MetaField doc ref lang + _ -> return <| PT.MetaField doc ref lang metaParser :: Parser (String, PT.MetaParams) metaParser = MPCL.lexeme spacer - $ MP.choice - [ MPCL.indentBlock spaceAny $ do + <| MP.choice + [ MPCL.indentBlock spaceAny <| do voidString "doc:" indentWrapper "doc" - (MP.mkPos $ 1 + (spacePos * 3)) - (PT.MetaDoc . docMapper) + (MP.mkPos <| 1 + (spacePos * 3)) + (PT.MetaDoc <. docMapper) docParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "ref:" indentWrapper "ref" - (MP.mkPos $ 1 + (spacePos * 3)) - (PT.MetaRef . Just) + (MP.mkPos <| 1 + (spacePos * 3)) + (PT.MetaRef <. Just) refParser - , MPCL.indentBlock spaceAny $ do + , MPCL.indentBlock spaceAny <| do voidString "lang:" indentWrapper "lang" - (MP.mkPos $ 1 + (spacePos * 3)) - (PT.MetaLang . Just) + (MP.mkPos <| 1 + (spacePos * 3)) + (PT.MetaLang <. Just) langParser ] docMapper :: [Text] -> Maybe Text - docMapper docLines = Just $ List.foldl (\a b -> a <> Text.pack "\n" <> b ) (Text.pack "") docLines + docMapper docLines = Just <| List.foldl (\a b -> a <> Text.pack "\n" <> b ) (Text.pack "") docLines docParser :: Parser Text docParser = MPCL.lexeme spacer - $ MP.manyTill MPCL.charLiteral eol >>= return . Text.pack + <| MP.manyTill MPCL.charLiteral eol >>= return <. Text.pack refParser :: Parser (FilePath, [PT.Types]) refParser = MPCL.lexeme spacer - $ do + <| do path <- pathParser colon types_ <- MP.sepBy1 definedTypeParser colon return - $ ( path + <| ( path , types_ ) @@ -372,14 +367,14 @@ parser spacing = let langParser = langLangParser langLangParser :: Parser Text - langLangParser = MPCL.lexeme spacer $ do + langLangParser = MPCL.lexeme spacer <| do lang <- MP.choice [ MPC.string "haskell" , MPC.string "python" , MPC.string "elm" , MPC.string "gleam" ] - >>= return . Text.pack + >>= return <. Text.pack eol return lang @@ -474,20 +469,20 @@ parser spacing = let val = MP.choice [ MPC.char '"' >> MP.manyTill MPCL.charLiteral (MPC.char '"') - >>= return . PT.VStr + >>= return <. PT.VStr , (MPCL.signed spaceAny - $ MPCL.lexeme spacer - $ MPCL.decimal - ) >>= return . PT.VInt + <| MPCL.lexeme spacer + <| MPCL.decimal + ) >>= return <. PT.VInt , (MPCL.signed spaceAny - $ MPCL.lexeme spacer - $ MPCL.float - ) >>= return . PT.VFloat + <| MPCL.lexeme spacer + <| MPCL.float + ) >>= return <. PT.VFloat , MP.choice [ PT.VBool True <$ MPC.string "True" , PT.VBool False <$ MPC.string "False" ] - , MPCL.lexeme spaceAny $ voidString "Dict" + , MPCL.lexeme spaceAny <| voidString "Dict" >> spaceAny >> MP.between (MPCL.symbol spaceAny "[") @@ -506,30 +501,30 @@ parser spacing = let ) (voidString ", ") ) - >>= return . PT.VDict . Map.fromList - , MPCL.lexeme spaceAny $ voidString "List" + >>= return <. PT.VDict <. Map.fromList + , MPCL.lexeme spaceAny <| voidString "List" >> spaceAny >> MP.between (MPCL.symbol spaceAny "[") (MPCL.symbol spaceAny "]") (MP.sepBy valueParser (MPC.char ',')) - >>= return . PT.VList + >>= return <. PT.VList , MP.choice [ do voidString "NotNull " jVal <- valueParser - return $ PT.VNullable $ Just jVal + return <| PT.VNullable <| Just jVal , PT.VNullable Nothing <$ voidString "Null" ] - , MPCL.lexeme spaceAny $ voidString "Set" + , MPCL.lexeme spaceAny <| voidString "Set" >> spaceAny >> MP.between (MPCL.symbol spaceAny "[") (MPCL.symbol spaceAny "]") (MP.sepBy valueParser (MPC.char ',') - >>= return . PT.VSet . Set.fromList + >>= return <. PT.VSet <. Set.fromList ) - , MPCL.lexeme spaceAny $ voidString "Tuple2" + , MPCL.lexeme spaceAny <| voidString "Tuple2" >> spaceAny >> MP.between (MPCL.symbol spaceAny "{") @@ -539,8 +534,8 @@ parser spacing = let b <- valueParser return (a, b) ) - >>= return . PT.VTuple2 - , MPCL.lexeme spaceAny $ voidString "Tuple3" + >>= return <. PT.VTuple2 + , MPCL.lexeme spaceAny <| voidString "Tuple3" >> spaceAny >> MP.between (MPCL.symbol spaceAny "{") @@ -551,8 +546,8 @@ parser spacing = let c <- valueParser return (a, b, c) ) - >>= return . PT.VTuple3 - , MPCL.lexeme spaceAny $ voidString "Tuple4" + >>= return <. PT.VTuple3 + , MPCL.lexeme spaceAny <| voidString "Tuple4" >> spaceAny >> MP.between (MPCL.symbol spaceAny "{") @@ -564,8 +559,8 @@ parser spacing = let d <- valueParser return (a, b, c, d) ) - >>= return . PT.VTuple4 - , MPCL.lexeme spaceAny $ voidString "Tuple5" + >>= return <. PT.VTuple4 + , MPCL.lexeme spaceAny <| voidString "Tuple5" >> spaceAny >> MP.between (MPCL.symbol spaceAny "{") @@ -578,9 +573,9 @@ parser spacing = let e <- valueParser return (a, b, c, d, e) ) - >>= return . PT.VTuple5 - , MPCL.lexeme spaceAny $ nameParser - >>= return . PT.OtherValue + >>= return <. PT.VTuple5 + , MPCL.lexeme spaceAny <| nameParser + >>= return <. PT.OtherValue ] in MP.between @@ -590,7 +585,7 @@ parser spacing = let <|> val nameParser :: Parser Text - nameParser = MPCL.lexeme spacer $ do + nameParser = MPCL.lexeme spacer <| do first <- MPC.upperChar rest <- MP.many (MP.choice @@ -598,11 +593,11 @@ parser spacing = let , MPC.char '_' ] ) - return $ Text.pack (first : rest) + return <| Text.pack (first : rest) -- A specific packed version of nameParser definedTypeParser :: Parser PT.Types -- Specifically (DefinedDataTypes Text) - definedTypeParser = MPCL.lexeme spacer $ do + definedTypeParser = MPCL.lexeme spacer <| do first <- MPC.upperChar rest <- MP.many (MP.choice @@ -610,10 +605,10 @@ parser spacing = let , MPC.char '_' ] ) - return $ PT.DefinedDataTypes $ Text.pack (first : rest) + return <| PT.DefinedDataTypes <| Text.pack (first : rest) pathParser :: Parser FilePath - pathParser = MPCL.lexeme spacer $ MP.many + pathParser = MPCL.lexeme spacer <| MP.many (MP.choice [ MPC.alphaNumChar , MPC.char '_' @@ -624,7 +619,7 @@ parser spacing = let ) pathAsTextParser :: Parser Text - pathAsTextParser = MPCL.lexeme spacer $ MP.many + pathAsTextParser = MPCL.lexeme spacer <| MP.many (MP.choice [ MPC.alphaNumChar , MPC.char '_' @@ -633,23 +628,23 @@ parser spacing = let , MPC.char '\\' ] ) - >>= return . Text.pack + >>= return <. Text.pack anyNameParser :: Parser Text - anyNameParser = MPCL.lexeme spacer $ do + anyNameParser = MPCL.lexeme spacer <| do MP.many (MP.choice [ MPC.alphaNumChar , MPC.char '_' ] ) - >>= return . Text.pack + >>= return <. Text.pack indentWrapper :: String -> MP.Pos -> ([a] -> c) -> - (Parser a) -> + Parser a -> Parser (MPCL.IndentOpt Parser @@ -699,7 +694,7 @@ space4 = MPCL.space tab :: Parser () tab = MPCL.space - (void $ MPC.char '\t') + (void <| MPC.char '\t') lineComment MP.empty diff --git a/app/Parsing/Types.hs b/app/Parsing/Types.hs index 5ab2553..d12491e 100644 --- a/app/Parsing/Types.hs +++ b/app/Parsing/Types.hs @@ -1,12 +1,8 @@ module Parsing.Types where -import qualified Data.List as List -import Data.Map.Strict (Map) +import qualified Antelude.List as List import qualified Data.Map.Strict as Map -import Data.Set (Set) -import Data.Text (Text) import qualified Data.Text as Text -import System.IO (FilePath) import Text.Show.Functions type DataTypeFile = [DataTypes] diff --git a/app/Prelude.hs b/app/Prelude.hs new file mode 100644 index 0000000..2fd1817 --- /dev/null +++ b/app/Prelude.hs @@ -0,0 +1,11 @@ +module Prelude + ( module Antelude + , (++) + ) where + +import Antelude + +infixr 5 ++ + +(++) :: List a -> List a -> List a +(++) = (<>) diff --git a/datatypetool.cabal b/datatypetool.cabal index 016b5aa..85f2297 100644 --- a/datatypetool.cabal +++ b/datatypetool.cabal @@ -18,8 +18,10 @@ executable datatypetool hs-source-dirs: app -- testProject/app default-language: GHC2021 - default-extensions: LambdaCase, MultiWayIf - + default-extensions: LambdaCase + , MultiWayIf + , TupleSections + , OverloadedRecordDot -- ghc-options: other-modules: @@ -47,8 +49,10 @@ executable datatypetool Parsing.Parser Parsing.Types Paths_datatypetool + Prelude build-depends: + , antelude , aeson , base , casing diff --git a/exampleProject/app/DTT/BasicTest.hs b/exampleProject/app/DTT/BasicTest.hs index 5bc6868..7d53887 100644 --- a/exampleProject/app/DTT/BasicTest.hs +++ b/exampleProject/app/DTT/BasicTest.hs @@ -11,8 +11,6 @@ import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as AesonT -import Data.Function ((&)) -import Control.Applicative ((<|>)) import Control.Monad.Fail (fail) import DTT.Folder.AnotherTest @@ -30,7 +28,7 @@ pointEncoder = (\(a, b) -> Aeson.object [ ((fromString "first" :: Aeson.Key) Aes pointDecoder :: AesonT.Value -> AesonT.Parser (Point) pointDecoder = - (Aeson.withObject "Tuple2" $ \v -> (,) <$> (v Aeson..: (fromString "first" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (v Aeson..: (fromString "second" :: Aeson.Key) >>= ((Aeson.parseJSON)))) + (Aeson.withObject "Tuple2" <| \v -> (,) <$> (v Aeson..: (fromString "first" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (v Aeson..: (fromString "second" :: Aeson.Key) >>= ((Aeson.parseJSON)))) data CardinalDirections @@ -50,11 +48,11 @@ cardinalDirectionsEncoder t = case t of cardinalDirectionsDecoder :: AesonT.Value -> AesonT.Parser (CardinalDirections) cardinalDirectionsDecoder = Aeson.withText "CardinalDirections" - (\t -> Text.unpack t & \a -> case a of - "north" -> return $ North - "south" -> return $ South - "east" -> return $ East - "west" -> return $ West + (\t -> Text.unpack t |> \a -> case a of + "north" -> return <| North + "south" -> return <| South + "east" -> return <| East + "west" -> return <| West _ -> fail "Invalid string for type CardinalDirections" ) @@ -68,14 +66,14 @@ data ImportantData = ImportantData importantDataEncoder :: ImportantData -> AesonT.Value importantDataEncoder val = Aeson.object - [ (fromString "this" :: Aeson.Key) Aeson..= (importantDataThis val & (Aeson.toJSON)) - , (fromString "that" :: Aeson.Key) Aeson..= (importantDataThat val & (Aeson.toJSON)) - , (fromString "something" :: Aeson.Key) Aeson..= (importantDataSomething val & (aBCEncoder) ) + [ (fromString "this" :: Aeson.Key) Aeson..= (importantDataThis val |> (Aeson.toJSON)) + , (fromString "that" :: Aeson.Key) Aeson..= (importantDataThat val |> (Aeson.toJSON)) + , (fromString "something" :: Aeson.Key) Aeson..= (importantDataSomething val |> (aBCEncoder) ) ] importantDataDecoder :: AesonT.Value -> AesonT.Parser (ImportantData) -importantDataDecoder = Aeson.withObject "ImportantData" $ \value -> ImportantData +importantDataDecoder = Aeson.withObject "ImportantData" <| \value -> ImportantData <$> (value Aeson..: (fromString "this" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (value Aeson..: (fromString "that" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (value Aeson..: (fromString "something" :: Aeson.Key) >>= ((aBCDecoder) )) diff --git a/exampleProject/app/DTT/Folder/AnotherTest.hs b/exampleProject/app/DTT/Folder/AnotherTest.hs index 7da9d62..f35c1dc 100644 --- a/exampleProject/app/DTT/Folder/AnotherTest.hs +++ b/exampleProject/app/DTT/Folder/AnotherTest.hs @@ -11,8 +11,6 @@ import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as AesonT -import Data.Function ((&)) -import Control.Applicative ((<|>)) import Control.Monad.Fail (fail) @@ -31,10 +29,10 @@ aBCEncoder t = case t of aBCDecoder :: AesonT.Value -> AesonT.Parser (ABC) aBCDecoder = Aeson.withText "ABC" - (\t -> Text.unpack t & \a -> case a of - "a" -> return $ A - "b" -> return $ B - "c" -> return $ C + (\t -> Text.unpack t |> \a -> case a of + "a" -> return <| A + "b" -> return <| B + "c" -> return <| C _ -> fail "Invalid string for type ABC" ) diff --git a/exampleProject/app/DTT/Folder/ComplexTests.hs b/exampleProject/app/DTT/Folder/ComplexTests.hs index d68c2b7..5584130 100644 --- a/exampleProject/app/DTT/Folder/ComplexTests.hs +++ b/exampleProject/app/DTT/Folder/ComplexTests.hs @@ -11,8 +11,6 @@ import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as AesonT -import Data.Function ((&)) -import Control.Applicative ((<|>)) import Control.Monad.Fail (fail) import DTT.BasicTest @@ -26,15 +24,15 @@ data ComplicatedData = ComplicatedData complicatedDataEncoder :: ComplicatedData -> AesonT.Value complicatedDataEncoder val = Aeson.object - [ (fromString "big_tuple" :: Aeson.Key) Aeson..= (complicatedDataBigTuple val & (\(a, b, c, d, e) -> Aeson.object [ ((fromString "first" :: Aeson.Key) Aeson..= (Aeson.toJSON) a), ((fromString "second" :: Aeson.Key) Aeson..= (\l -> AesonT.listValue ((pointEncoder) ) l) b), ((fromString "third" :: Aeson.Key) Aeson..= (\d -> Aeson.toJSON d) c), ((fromString "fourth" :: Aeson.Key) Aeson..= (pointEncoder) d), ((fromString "fifth" :: Aeson.Key) Aeson..= (importantDataEncoder) e)])) - , (fromString "directions" :: Aeson.Key) Aeson..= (complicatedDataDirections val & (cardinalDirectionsEncoder) ) - , (fromString "is_true" :: Aeson.Key) Aeson..= (complicatedDataIsTrue val & (Aeson.toJSON)) + [ (fromString "big_tuple" :: Aeson.Key) Aeson..= (complicatedDataBigTuple val |> (\(a, b, c, d, e) -> Aeson.object [ ((fromString "first" :: Aeson.Key) Aeson..= (Aeson.toJSON) a), ((fromString "second" :: Aeson.Key) Aeson..= (\l -> AesonT.listValue ((pointEncoder) ) l) b), ((fromString "third" :: Aeson.Key) Aeson..= (\d -> Aeson.toJSON d) c), ((fromString "fourth" :: Aeson.Key) Aeson..= (pointEncoder) d), ((fromString "fifth" :: Aeson.Key) Aeson..= (importantDataEncoder) e)])) + , (fromString "directions" :: Aeson.Key) Aeson..= (complicatedDataDirections val |> (cardinalDirectionsEncoder) ) + , (fromString "is_true" :: Aeson.Key) Aeson..= (complicatedDataIsTrue val |> (Aeson.toJSON)) ] complicatedDataDecoder :: AesonT.Value -> AesonT.Parser (ComplicatedData) -complicatedDataDecoder = Aeson.withObject "ComplicatedData" $ \value -> ComplicatedData - <$> (value Aeson..: (fromString "big_tuple" :: Aeson.Key) >>= ((Aeson.withObject "Tuple5" $ \v -> (,,,,) <$> (v Aeson..: (fromString "first" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (v Aeson..: (fromString "second" :: Aeson.Key) >>= ((AesonT.listParser ((pointDecoder) )) )) <*> (v Aeson..: (fromString "third" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (v Aeson..: (fromString "fourth" :: Aeson.Key) >>= ((pointDecoder) )) <*> (v Aeson..: (fromString "fifth" :: Aeson.Key) >>= ((importantDataDecoder) ))))) +complicatedDataDecoder = Aeson.withObject "ComplicatedData" <| \value -> ComplicatedData + <$> (value Aeson..: (fromString "big_tuple" :: Aeson.Key) >>= ((Aeson.withObject "Tuple5" <| \v -> (,,,,) <$> (v Aeson..: (fromString "first" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (v Aeson..: (fromString "second" :: Aeson.Key) >>= ((AesonT.listParser ((pointDecoder) )) )) <*> (v Aeson..: (fromString "third" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (v Aeson..: (fromString "fourth" :: Aeson.Key) >>= ((pointDecoder) )) <*> (v Aeson..: (fromString "fifth" :: Aeson.Key) >>= ((importantDataDecoder) ))))) <*> (value Aeson..: (fromString "directions" :: Aeson.Key) >>= ((cardinalDirectionsDecoder) )) <*> (value Aeson..: (fromString "is_true" :: Aeson.Key) >>= ((Aeson.parseJSON))) diff --git a/exampleProject/app/DTT/TestFile.hs b/exampleProject/app/DTT/TestFile.hs index f519815..a91f52c 100644 --- a/exampleProject/app/DTT/TestFile.hs +++ b/exampleProject/app/DTT/TestFile.hs @@ -11,8 +11,6 @@ import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as AesonT -import Data.Function ((&)) -import Control.Applicative ((<|>)) import Control.Monad.Fail (fail) @@ -25,14 +23,14 @@ data Address = Address addressEncoder :: Address -> AesonT.Value addressEncoder val = Aeson.object - [ (fromString "street" :: Aeson.Key) Aeson..= (addressStreet val & (Aeson.toJSON)) - , (fromString "city" :: Aeson.Key) Aeson..= (addressCity val & (Aeson.toJSON)) - , (fromString "postal_code" :: Aeson.Key) Aeson..= (addressPostalCode val & (Aeson.toJSON)) + [ (fromString "street" :: Aeson.Key) Aeson..= (addressStreet val |> (Aeson.toJSON)) + , (fromString "city" :: Aeson.Key) Aeson..= (addressCity val |> (Aeson.toJSON)) + , (fromString "postal_code" :: Aeson.Key) Aeson..= (addressPostalCode val |> (Aeson.toJSON)) ] addressDecoder :: AesonT.Value -> AesonT.Parser (Address) -addressDecoder = Aeson.withObject "Address" $ \value -> Address +addressDecoder = Aeson.withObject "Address" <| \value -> Address <$> (value Aeson..: (fromString "street" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (value Aeson..: (fromString "city" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (value Aeson..: (fromString "postal_code" :: Aeson.Key) >>= ((Aeson.parseJSON))) @@ -60,23 +58,23 @@ data TestData = TestData testDataEncoder :: TestData -> AesonT.Value testDataEncoder val = Aeson.object - [ (fromString "uid" :: Aeson.Key) Aeson..= (testDataUid val & (Aeson.toJSON)) - , (fromString "name" :: Aeson.Key) Aeson..= (testDataName val & (Aeson.toJSON)) - , (fromString "dob" :: Aeson.Key) Aeson..= (testDataDob val & (Aeson.toJSON)) - , (fromString "address" :: Aeson.Key) Aeson..= (testDataAddress val & (addressEncoder) ) - , (fromString "telephone" :: Aeson.Key) Aeson..= (testDataTelephone val & (Aeson.toJSON)) - , (fromString "pets" :: Aeson.Key) Aeson..= (testDataPets val & (\l -> AesonT.listValue ((Aeson.toJSON)) l) ) - , (fromString "score" :: Aeson.Key) Aeson..= (testDataScore val & (Aeson.toJSON)) - , (fromString "email" :: Aeson.Key) Aeson..= (testDataEmail val & (Aeson.toJSON)) - , (fromString "url" :: Aeson.Key) Aeson..= (testDataUrl val & (Aeson.toJSON)) - , (fromString "description" :: Aeson.Key) Aeson..= (testDataDescription val & (Aeson.toJSON)) - , (fromString "verified" :: Aeson.Key) Aeson..= (testDataVerified val & (Aeson.toJSON)) - , (fromString "salary" :: Aeson.Key) Aeson..= (testDataSalary val & (Aeson.toJSON)) + [ (fromString "uid" :: Aeson.Key) Aeson..= (testDataUid val |> (Aeson.toJSON)) + , (fromString "name" :: Aeson.Key) Aeson..= (testDataName val |> (Aeson.toJSON)) + , (fromString "dob" :: Aeson.Key) Aeson..= (testDataDob val |> (Aeson.toJSON)) + , (fromString "address" :: Aeson.Key) Aeson..= (testDataAddress val |> (addressEncoder) ) + , (fromString "telephone" :: Aeson.Key) Aeson..= (testDataTelephone val |> (Aeson.toJSON)) + , (fromString "pets" :: Aeson.Key) Aeson..= (testDataPets val |> (\l -> AesonT.listValue ((Aeson.toJSON)) l) ) + , (fromString "score" :: Aeson.Key) Aeson..= (testDataScore val |> (Aeson.toJSON)) + , (fromString "email" :: Aeson.Key) Aeson..= (testDataEmail val |> (Aeson.toJSON)) + , (fromString "url" :: Aeson.Key) Aeson..= (testDataUrl val |> (Aeson.toJSON)) + , (fromString "description" :: Aeson.Key) Aeson..= (testDataDescription val |> (Aeson.toJSON)) + , (fromString "verified" :: Aeson.Key) Aeson..= (testDataVerified val |> (Aeson.toJSON)) + , (fromString "salary" :: Aeson.Key) Aeson..= (testDataSalary val |> (Aeson.toJSON)) ] testDataDecoder :: AesonT.Value -> AesonT.Parser (TestData) -testDataDecoder = Aeson.withObject "TestData" $ \value -> TestData +testDataDecoder = Aeson.withObject "TestData" <| \value -> TestData <$> (value Aeson..: (fromString "uid" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (value Aeson..: (fromString "name" :: Aeson.Key) >>= ((Aeson.parseJSON))) <*> (value Aeson..: (fromString "dob" :: Aeson.Key) >>= ((Aeson.parseJSON))) diff --git a/exampleProject/app/Main.hs b/exampleProject/app/Main.hs index 030f604..b505175 100644 --- a/exampleProject/app/Main.hs +++ b/exampleProject/app/Main.hs @@ -12,11 +12,11 @@ main :: IO () main = do case testJson of Just t -> do - decoded <- return $ AesonT.parseMaybe Test.testDataDecoder t + decoded <- return <| AesonT.parseMaybe Test.testDataDecoder t case decoded of Just d -> do - reencoded <- return $ Test.testDataEncoder d - print $ Aeson.encode reencoded + reencoded <- return <| Test.testDataEncoder d + print <| Aeson.encode reencoded print "" print (t == reencoded) Nothing -> @@ -26,4 +26,4 @@ main = do testJson :: Maybe Aeson.Value -testJson = Aeson.decode' $ fromString "{\"uid\": \"1NZR6G48Q7086GKV\",\"name\": \"Sylvia Valenti\",\"dob\": \"2015-12-02\",\"address\": {\"street\": \"9176 Wincombe Street\",\"city\": \"Uppingham\",\"postal_code\": \"SY5 7PF\"},\"telephone\": \"+53-4720-814-866\",\"pets\": [\"Blackie\",\"Riley\"],\"score\": 5.5,\"email\": \"sade6144@freeze.nh.us\",\"url\": \"http://portable.trentinosud-tirol.it\",\"description\": \"cooler propecia modification vice alternative those drove rapidly warren rd solutions media person dale idol permitted hacker hats greatest debian\",\"verified\": true,\"salary\": 67737}" +testJson = Aeson.decode' <| fromString "{\"uid\": \"1NZR6G48Q7086GKV\",\"name\": \"Sylvia Valenti\",\"dob\": \"2015-12-02\",\"address\": {\"street\": \"9176 Wincombe Street\",\"city\": \"Uppingham\",\"postal_code\": \"SY5 7PF\"},\"telephone\": \"+53-4720-814-866\",\"pets\": [\"Blackie\",\"Riley\"],\"score\": 5.5,\"email\": \"sade6144@freeze.nh.us\",\"url\": \"http://portable.trentinosud-tirol.it\",\"description\": \"cooler propecia modification vice alternative those drove rapidly warren rd solutions media person dale idol permitted hacker hats greatest debian\",\"verified\": true,\"salary\": 67737}" diff --git a/exampleProject/testProject.cabal b/exampleProject/exampleProject.cabal similarity index 71% rename from exampleProject/testProject.cabal rename to exampleProject/exampleProject.cabal index 204b829..c9a50da 100644 --- a/exampleProject/testProject.cabal +++ b/exampleProject/exampleProject.cabal @@ -1,5 +1,5 @@ cabal-version: 3.4 -name: testProject +name: exampleProject version: 0.1.0.0 common warnings @@ -8,7 +8,11 @@ common warnings executable testProject import: warnings main-is: Main.hs + mixins: base hiding (Prelude) + , antelude (Antelude as Prelude) + , antelude build-depends: base ^>=4.19.1.0 + , antelude , aeson , bytestring , containers diff --git a/exampleProject/src/ElmTest.elm b/exampleProject/src/ElmTest.elm index eba8885..71734b8 100644 --- a/exampleProject/src/ElmTest.elm +++ b/exampleProject/src/ElmTest.elm @@ -1,10 +1,6 @@ -<<<<<<< HEAD:testProject/src/ElmTest.elm -{- Uses elm-posix to test this: https://github.com/albertdahlin/elm-posix/tree/master -} -======= {- Uses elm-posix to test this: https://github.com/albertdahlin/elm-posix/tree/master elm-cli run src/ElmTest.elm -} ->>>>>>> feature/testing:exampleProject/src/ElmTest.elm module ElmTest exposing (program)