-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/dhall to csv basic conversion (#2226)
* feat(dhall-to-csv): add basic functions to convert from dhall to csv * feat(dhall-to-csv): add basic main to test new dhallToCsv function * feat(dhall-to-csv): basic test for dhall-to-csv * feat(dhall-to-csv): removed hello function * fix(dhall-to-csv): add gitattributes to avoid conversion of newlines to fail in tests * fix(dhall-to-csv): avoid git conversion of newlines now for real * fix: treat test files as binary to pass windows tests * fix(dhall-csv): import _ERROR unqualified from Dhall.Util * fix(dhall-csv): simplify code for listConvert function * fix: removed golden file and add it again to fix windows testing * fix(dhall-csv): moved function encodeDefaultCsv to new module Dhall.Csv.Util * Update dhall-csv/dhall-csv.cabal Co-authored-by: Simon Jakobi <[email protected]> * fix(dhall-csv): removed alpha-normalization from dhall-to-csv conversion * fix(dhall-csv): sort header to standardize output Co-authored-by: Simon Jakobi <[email protected]>
- Loading branch information
Showing
10 changed files
with
170 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Main where | ||
|
||
import qualified Data.Text.IO | ||
import qualified Dhall.Csv | ||
import qualified Dhall.Csv.Util | ||
import qualified GHC.IO.Encoding | ||
|
||
main :: IO () | ||
main = putStrLn Dhall.Csv.hello | ||
main = do | ||
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8 | ||
|
||
text <- Data.Text.IO.getContents | ||
csv <- Dhall.Csv.codeToValue Nothing text | ||
|
||
Data.Text.IO.putStr $ Dhall.Csv.Util.encodeCsvDefault csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,89 @@ | ||
module Dhall.Csv (hello) where | ||
{-#LANGUAGE OverloadedStrings#-} | ||
|
||
hello :: String | ||
hello = "Hello Dhall to CSV!" | ||
module Dhall.Csv ( | ||
dhallToCsv | ||
, codeToValue | ||
) where | ||
|
||
import Control.Exception (Exception, throwIO) | ||
import Data.Csv (ToField (..)) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Sequence (Seq) | ||
import Data.Text (Text) | ||
import Data.Text.Prettyprint.Doc (Pretty) | ||
import Data.Void (Void) | ||
import Dhall.Core (Expr, DhallDouble (..)) | ||
import Dhall.Import (SemanticCacheMode (..)) | ||
import Dhall.Util (_ERROR) | ||
|
||
import qualified Data.Csv | ||
import qualified Data.Foldable | ||
import qualified Data.Text | ||
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty | ||
import qualified Dhall.Core as Core | ||
import qualified Dhall.Import | ||
import qualified Dhall.Map | ||
import qualified Dhall.Parser | ||
import qualified Dhall.Pretty | ||
import qualified Dhall.TypeCheck | ||
import qualified Dhall.Util | ||
import qualified System.FilePath | ||
|
||
data CompileError = Unsupported (Expr Void Void) | ||
|
||
instance Show CompileError where | ||
show (Unsupported e) = | ||
Data.Text.unpack $ | ||
_ERROR <> ": Cannot translate to CSV \n\ | ||
\ \n\ | ||
\Explanation: Only records of primitive values can be \n\ | ||
\translated from Dhall to CSV. \n\ | ||
\ \n\ | ||
\The following Dhall expression could not be translated to CSV: \n\ | ||
\ \n\ | ||
\" <> insert e | ||
|
||
instance Exception CompileError | ||
|
||
insert :: Pretty a => a -> Text | ||
insert = Pretty.renderStrict . Dhall.Pretty.layout . Dhall.Util.insert | ||
|
||
dhallToCsv | ||
:: Expr s Void | ||
-> Either CompileError (Seq Data.Csv.NamedRecord) | ||
dhallToCsv e0 = listConvert $ Core.normalize e0 | ||
where | ||
listConvert :: Expr Void Void -> Either CompileError (Seq Data.Csv.NamedRecord) | ||
listConvert (Core.ListLit _ a) = traverse recordConvert a | ||
listConvert e = Left $ Unsupported e | ||
recordConvert :: Expr Void Void -> Either CompileError Data.Csv.NamedRecord | ||
recordConvert (Core.RecordLit a) = do | ||
a' <- traverse (fieldConvert . Core.recordFieldValue) a | ||
return $ Data.Csv.toNamedRecord $ Dhall.Map.toMap a' | ||
recordConvert e = Left $ Unsupported e | ||
fieldConvert :: Expr Void Void -> Either CompileError Data.Csv.Field | ||
fieldConvert (Core.NaturalLit a) = return $ toField a | ||
fieldConvert (Core.IntegerLit a) = return $ toField a | ||
fieldConvert (Core.DoubleLit (DhallDouble a)) = return $ toField a | ||
fieldConvert (Core.TextLit (Core.Chunks [] a)) = return $ toField a | ||
fieldConvert (Core.App (Core.Field (Core.Union _) _) a) = fieldConvert a | ||
fieldConvert e = Left $ Unsupported e | ||
|
||
codeToValue | ||
:: Maybe FilePath | ||
-> Text | ||
-> IO [Data.Csv.NamedRecord] | ||
codeToValue mFilePath code = do | ||
parsedExpression <- Core.throws (Dhall.Parser.exprFromText (fromMaybe "(input)" mFilePath) code) | ||
|
||
let rootDirectory = case mFilePath of | ||
Nothing -> "." | ||
Just fp -> System.FilePath.takeDirectory fp | ||
|
||
resolvedExpression <- Dhall.Import.loadRelativeTo rootDirectory UseSemanticCache parsedExpression | ||
|
||
_ <- Core.throws (Dhall.TypeCheck.typeOf resolvedExpression) | ||
|
||
case dhallToCsv resolvedExpression of | ||
Left err -> throwIO err | ||
Right csv -> return $ Data.Foldable.toList csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Dhall.Csv.Util (encodeCsvDefault) where | ||
|
||
import Data.List (sort) | ||
import Data.Text (Text) | ||
|
||
import qualified Data.ByteString.Lazy as ByteString | ||
import qualified Data.Csv | ||
import qualified Data.HashMap.Strict as HashMap | ||
import qualified Data.Text.Encoding | ||
import qualified Data.Vector as Vector | ||
|
||
encodeCsvDefault :: [Data.Csv.NamedRecord] -> Text | ||
encodeCsvDefault csv = Data.Text.Encoding.decodeUtf8 $ ByteString.toStrict $ Data.Csv.encodeByName header csv | ||
where | ||
header = case csv of | ||
[] -> Vector.empty | ||
(m:_) -> Vector.fromList $ sort $ HashMap.keys m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.csv binary | ||
*.dhall binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
testDouble,testNatural,testText | ||
3.14,42,text | ||
1.0e-4,4,dummy | ||
NaN,2,test | ||
0.0,10,dhall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ { testNatural = 42, testText = "text", testDouble = 3.14 } | ||
, { testNatural = 4, testText = "dummy", testDouble = 1e-4 } | ||
, { testNatural = 2, testText = "test", testDouble = NaN } | ||
, { testNatural = 10, testText = "dhall", testDouble = 0.0 } | ||
] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.