Parsing CSV files from Hyperlambda
This project provides CSV helper slots for Magic. More specifically, it provides the following slots.
- [lambda2csv] - Creates a CSV string from a lambda object
- [csv2lambda] - Creates a lambda object from a CSV string
.data . name:Thomas Hansen age:int:47 . name:John Doe age:int:67 lambda2csv:x:-/* add:x:+/*/types get-nodes:x:@lambda2csv/* csv2lambda:x:@lambda2csv types
Notice, the [lambda2csv] slot will return type information, and the [csv2lambda] slot can optionally be given type information in its [types] argument, allowing you to convert correctly back to the correct type(s) as you convert a CSV string to its lambda objects. The [lambda2csv] slot will also create headers for your CSV string, and the [csv2lambda] slot will use the first line to retrieve these headers, to rebuild the lambda object correctly.
This allows you to roundtrip from a lambda object, to CSV format, and back to lambda again, without loosing type information or names of your nodes. This is necessary since CSV doesn’t preserve types in any ways. By default the [lambda2csv] will also treat null values correctly, by adding these into the CSV file as “[NULL]”. This allows you to preserve null values, which isn’t really possible with CSV normally - However, for some edge cases you might want to use different null value, at which point you can supply this as [null-value] argument to both slots. If you supply a [null-value] argument, but set its value to null - Null values will be ignored, and no null values will be persisted in any direction.
The source code for this repository can be found at github.com/polterguy/magic.lambda.csv, and you can provide feedback, provide bug reports, etc at the same place.