| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Dhall.CsvToDhall
Contents
Description
Convert CSV data to Dhall providing an expected Dhall type necessary to know which type will be interpreted.
The translation process will produce a Dhall expression where
its type is a List of records and the type of each field of the
records is one of the following:
BoolsNaturalsIntegersDoublesTextsOptionals (of valid field types)- unions (of empty alternatives or valid record field types)
It is exactly the same as dhall-to-csv supported input types.
You can use this code as a library (this module) or as an executable
named csv-to-dhall, which is used in the examples below.
For now, csv-to-dhall does not support type inference so you must
always specify the Dhall type you expect.
$ cat example.csv
example
1
$ csv-to-dhall 'List { example : Integer }' < example.csv
[{ example = +1 }]When using the csv-to-dhall executable you can specify that the CSV
you want to translate does not have a header with the flag `--no-header`.
In this case the resulting record fields will be named _1, _2, ...
in the same order they were in the input CSV. You must still provide the
expected Dhall type taking this into consideration.
$ cat no-header-example.csv
1,3.14,Hello
-1,2.68,Goodbye
$ csv-to-dhall --no-header 'List { _1 : Integer, _2 : Double, _3 : Text } < no-header-example.csv
[ { _1 = +1, _2 = 3.14, _3 = "Hello" }
, { _1 = -1, _2 = 2.68, _3 = "Goodbye" }
]Primitive types
Strings true and false can translate to Dhall Bools
$ cat example.csv
exampleBool
true
false
$ csv-to-dhall 'List { exampleBool : Bool }' < example.csv
[ { exampleBool = True }, { exampleBool = False } ]Numeric strings can translate to Dhall numbers:
$ cat example.csv
exampleNatural,exampleInt,exampleDouble
1,2,3
0,-2,3.14
0,+2,-3.14
$ csv-to-dhall 'List { exampleNatural : Natural, exampleInt : Integer, exampleDouble : Double }' < example.csv
[ { exampleNatural = 1, exampleInt = +2, exampleDouble = 3.0 }
, { exampleNatural = 0, exampleInt = -2, exampleDouble = 3.14 }
, { exampleNatural = 0, exampleInt = +2, exampleDouble = -3.14 }
]Every CSV Field can translate directly to Dhall Text:
$ cat example.csv
exampleText
Hello
false
","
$ csv-to-dhall 'List { exampleText : Text }' < example.csv
[ { exampleText = "Hello" }
, { exampleText = "false" }
, { exampleText = "" }
, { exampleText = "," }
]Unions and Optionals
By default, when a union is expected, the first alternative that matches the CSV field is chosen. With the `--unions-strict` flag one can make sure that only one alternative matches. With the `--unions-none` unions are not allowed.
An union alternative matches a CSV field if
- It's an empty alternative and the name is the same as the text in the CSV field.
- It's a non-empty alternative and the CSV field can be converted to the underlying type.
$ cat example.csv
exampleUnion
Hello
1
1.11
$ csv-to-dhall 'List { exampleUnion : <Hello | Nat : Natural | Dob : Double> }' < example.csv
[ { exampleUnion = <Hello | Nat : Natural | Dob : Double>.Hello }
, { exampleUnion = <Hello | Nat : Natural | Dob : Double>.Nat 1 }
, { exampleUnion = <Hello | Nat : Natural | Dob : Double>.Dob 1.11 }
]Optional values can be either missing or have the expected value. The missing value is represented by the empty string. If a field's expected value is an Optional and the field is not in the CSV, then all the values will be None.
$ cat example.csv
exampleOptional
1
3
$ csv-to-dhall 'List { exampleOptional : Optional Natural, exampleMissing : Optional Natural }' < example.csv
[ { exampleOptional = Some 1, exampleMissing = None Natural }
, { exampleOptional = None Natural, exampleMissing = None Natural }
, { exampleOptional = Some 3, exampleMissing = None Natural }
]Synopsis
- dhallFromCsv :: Conversion -> ExprX -> [NamedRecord] -> Either CompileError ExprX
- parseConversion :: Parser Conversion
- defaultConversion :: Conversion
- resolveSchemaExpr :: Text -> IO ExprX
- typeCheckSchemaExpr :: (Exception e, MonadCatch m) => (CompileError -> e) -> ExprX -> m ExprX
- data Conversion = Conversion {
- strictRecs :: Bool
- unions :: UnionConv
- data CompileError
- = Unsupported ExprX
- | NotAList ExprX
- | NotARecord ExprX
- | TypeError (TypeError Src Void)
- | BadSchemaType ExprX ExprX
- | MissingField Text
- | UnhandledFields [Text]
- | Mismatch ExprX Text Text
- | ContainsUnion ExprX
- | UndecidableUnion ExprX Text Text [ExprX]
- | UndecidableMissingUnion ExprX Text [ExprX]
- | UnicodeError UnicodeException
CSV to Dhall
dhallFromCsv :: Conversion -> ExprX -> [NamedRecord] -> Either CompileError ExprX Source #
Convert a list of CSV NameRecord to a Dhall expression given the expected Dhall Type
of the output.
parseConversion :: Parser Conversion Source #
Standard parser for options related to the conversion method
defaultConversion :: Conversion Source #
Default conversion options
Parse schema code and resolve imports
typeCheckSchemaExpr :: (Exception e, MonadCatch m) => (CompileError -> e) -> ExprX -> m ExprX Source #
Check that the Dhall type expression actually has type Type
data Conversion Source #
CSV-to-dhall translation options
Constructors
| Conversion | |
Fields
| |
Instances
| Show Conversion Source # | |
Defined in Dhall.CsvToDhall Methods showsPrec :: Int -> Conversion -> ShowS # show :: Conversion -> String # showList :: [Conversion] -> ShowS # | |
Exceptions
data CompileError Source #
This is the exception type for errors that can arise when converting from CSV to Dhall.
It contains information on the specific cases that might fail to give a better insight.
Constructors
| Unsupported ExprX | |
| NotAList ExprX | |
| NotARecord ExprX | |
| TypeError (TypeError Src Void) | |
| BadSchemaType ExprX ExprX | |
| MissingField Text | |
| UnhandledFields [Text] | |
| Mismatch ExprX Text Text | |
| ContainsUnion ExprX | |
| UndecidableUnion ExprX Text Text [ExprX] | |
| UndecidableMissingUnion ExprX Text [ExprX] | |
| UnicodeError UnicodeException |
Instances
| Show CompileError Source # | |
Defined in Dhall.CsvToDhall Methods showsPrec :: Int -> CompileError -> ShowS # show :: CompileError -> String # showList :: [CompileError] -> ShowS # | |
| Exception CompileError Source # | |
Defined in Dhall.CsvToDhall Methods toException :: CompileError -> SomeException # fromException :: SomeException -> Maybe CompileError # displayException :: CompileError -> String # | |