tapioca: A tasty enhancement to cassava for easy csv exporting

[ bsd3, csv, library, text, web ] [ Propose Tags ] [ Report a vulnerability ]

tapioca is a package that builds on cassava, to provide a simpler, more succinct method of mapping records to and from csv data.


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.1.0
Change log CHANGELOG.md
Dependencies attoparsec (>=0.13.2 && <0.14), base (>=4.10.0 && <1000), binary (>=0.8.6 && <0.9), bytestring (>=0.10.8 && <0.11), cassava (>=0.5.1 && <0.6), errors (>=2.3.0 && <2.4), lens (>=4.17 && <4.18), unordered-containers (>=0.2.9 && <0.3), vector (>=0.12.0 && <0.13) [details]
Tested with ghc ==8.6.3, ghc ==8.6.2, ghc ==8.2.2
License BSD-3-Clause
Author Christopher Fraser
Maintainer cfraz89@gmail.com
Category Text, Web, CSV
Source repo head: git clone https://github.com/cfraz89/tapioca.git
Uploaded by cfraz89 at 2019-02-03T19:01:40Z
Distributions
Downloads 601 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for tapioca-0.1.1.0

[back to package description]

tapioca

tapioca is a package that builds on cassava, to provide a simpler, more succinct method of encoding and decoding CSV's with headers.

Why?

Let's say we have a list of data MyRecord which we want to encode and decode to and from a CSV file:

data MyRecord = MyRecord
  { field1 :: Int
  , field2 :: String
  }

myRecords :: [a]
myRecords = ..

Here is how it might be done in cassava:

import Data.Csv

instance ToNamedRecord MyRecord where
  toNamedRecord (MyRecord field1 field2)= namedRecord
    [ "Header for Field 1" .= field1
    , "Header for Field 2" .= field2
    ]

instance DefaultOrdered MyRecord where
  headerOrder _ =
    [ "Header for Field 1"
    , "Header for Field 2"
    ]

instance FromNamedRecord MyRecord where
    parseNamedRecord m = MyRecord
      <$> m .: "Header for Field 1"
      <*> m .: "Header for Field 2"

-- Example usage
myCSV :: ByteString
myCSV = encodeDefaultOrderedByName myRecords

fromCSV :: ByteString -> Either String (Vector MyRecord)
fromCSV = (snd <$>) . decodeByName

While serviceable, the need to define headers twice is less than ideal, resulting in code that is bulkier and more fragile.

Here's how we do it in tapioca:

import Data.Tapioca

instance CsvMapped MyRecord where
  csvMap = CsvMap
     $ "Header for Field 1" <-> #field1
    :| "Header for Field 2" <-> #field2

-- Example usage
myCSV :: ByteString
myCSV = encode HasHeader myRecords

fromCSV :: ByteString -> Either String (Vector MyRecord)
fromCSV = decode HasHeader

We see here that tapioca provides us with a more succinct definition for defining CSV mappings, avoiding any unnecessary duplication, and keeping the entire definition within a single typeclass.

Usage

As seen earlier, the key part of using Tapioca is to define an instance of CsvMapped for your type:

instance CsvMapped MyRecord where
  csvMap = CsvMap
     $ "Header for Field 1" <-> #field1
    :| "Header for Field 2" <-> #field2
    :| "Header for Field 3" <-> #field3

Mapping selectors

Fields can be mapped on top of cassava's FromField and ToField instances on a per-field basis. As mappings are expected to be isomorphisms, they are defined a lens Iso. If you wish to map a field over an isomorphism, you can use either the <:> or codec (synonyms of each other) combinators:

import Control.Lens.Iso

instance CsvMapped MyRecord where
  csvMap = mkCsvMap
     $ "Header for Field 1" <-> #field1 <:> iso asOrdinal fromOrdinal
     -- or
    :| "Header for Field 2" <-> codec #field2 (iso asOrdinal fromOrdinal)

Refer to the CodecField example to see this in practice.

Nesting maps

Occasionally you may want to nest a record within another record. Provided that both your records implement CsvMapped, this can be done by using the Nest constructor:

data NestingRecord = NestingRecord
  { exampleRecord :: ExampleRecord
  , other :: Int
  }
  deriving (Show, Generic)

instance CsvMapped NestingRecord where
  csvMap = CsvMap
     $ Nest #exampleRecord
    :| "Other" <-> #other

Then in this example, for each row, the fields of ExampleRecord will precede the "Other" column field. Note that when decoding a spliced CSV with Headers, order of each field of ExampleRecord within the row is inferred from the order of the CSV headers. It is not required that the CSV's ExampleRecord columns are contiguous.

Refer to the NestedEncode and NestedDecode examples to see this in practice.