Skip to content

Commit

Permalink
Add an example for encoding with a header
Browse files Browse the repository at this point in the history
  • Loading branch information
gwils committed Aug 6, 2018
1 parent 0602ca7 commit 63a6092
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/csv/encoding-header.expected.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bytestring,Text,Int,Double,Text2,Double2,Sum
Hello,Goodbye,5,5.1,text,0.5,20
Yes,no,200,-4.5,,22.0,19.3
a,b,0,0.0,"words ""words"" words",15.0,More words
2 changes: 1 addition & 1 deletion examples/src/Data/Sv/Example/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ examples =
]

opts :: EncodeOptions
opts = defaultEncodeOptions & terminalNewline .~ False
opts = defaultEncodeOptions

main :: IO ()
main = do
Expand Down
65 changes: 65 additions & 0 deletions examples/src/Data/Sv/Example/EncodingWithHeader.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Data.Sv.Example.EncodingWithHeader (main) where

import Control.Monad (when)
import qualified Data.ByteString.Lazy as LBS
import Data.Semigroup ((<>))
import System.Exit (exitFailure)

import Data.Sv
import qualified Data.Sv.Encode as E
import Data.Sv.Example.Encoding (Example (..), sumEnc, examples, e1,e2,e3,e4,e5,e6,p1,p2)

-- This module demonstrates encoding with a header.
--
-- It is worth looking at Encoding.hs before this module, as this module
-- imports definitions from that module.
--
-- EncodingWithHeader.hs focuses on encoding with a header, whereas Encode.hs
-- is mostly about showing off different ways to define an encoder.

expectedFile :: FilePath
expectedFile = "csv/encoding-header.expected.csv"

opts :: EncodeOptions
opts = defaultEncodeOptions

-- Here we build our encoder, attaching a name to each column
exampleEnc :: NameEncode Example
exampleEnc =
"Bytestring" =: E.encodeOf e1 E.byteString
<> "Text" =: E.encodeOf e2 E.text
<> "Int" =: E.encodeOf e3 E.int
<> "Double" =: E.encodeOf e4 E.double
-- Notice that we have had to inline the definition of productEnc to
-- attach a name to each component of it.
--
-- The lesson is that if one wants encoding with column names, this should
-- be built-in as you go. It can be tricky to attach names to the right
-- places after the fact.
-- This could also be avoided by not using ADTs within the data
-- type to be encoded.
<> "Text2" =: E.encodeOf (e5.p1) E.text
<> "Double2" =: E.encodeOf (e5.p2) E.double
<> "Sum" =: E.encodeOf e6 sumEnc



main :: IO ()
main = do
expected <- LBS.readFile expectedFile
putStrLn "Data to encode:"
print examples
putStrLn "Expected"
LBS.putStr expected
putStrLn ""
putStrLn "Encoded:"
let encoded = encodeNamed exampleEnc opts examples
LBS.putStr encoded
putStrLn ""

-- validation
when (expected /= encoded)
(putStrLn "expected /= encoded" *> exitFailure)
1 change: 1 addition & 0 deletions examples/sv-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tested-with: GHC == 7.10.3
library
exposed-modules: Data.Sv.Example.Columnar
, Data.Sv.Example.Encoding
, Data.Sv.Example.EncodingWithHeader
, Data.Sv.Example.Numbers
, Data.Sv.Example.Ragged
, Data.Sv.Example.Species
Expand Down
2 changes: 2 additions & 0 deletions examples/test/test.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import qualified Data.Sv.Example.Columnar as Columnar
import qualified Data.Sv.Example.Encoding as Encoding
import qualified Data.Sv.Example.EncodingWithHeader as EncodingWithHeader
import qualified Data.Sv.Example.Numbers as Numbers
import qualified Data.Sv.Example.Ragged as Ragged
import qualified Data.Sv.Example.Species as Species
Expand All @@ -9,6 +10,7 @@ main :: IO ()
main = do
Columnar.main
Encoding.main
EncodingWithHeader.main
Numbers.main
Ragged.main
Species.main
Expand Down
2 changes: 2 additions & 0 deletions src/Data/Sv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Data.Sv (
, decodeMay
, decodeEither
, decodeEither'
, (.:)
, (>>==)
, (==<<)
, module Data.Sv.Parse
Expand All @@ -48,6 +49,7 @@ module Data.Sv (
, encodeNamedToHandle
, encodeNamedBuilder
, encodeRow
, (=:)
, module Data.Sv.Encode.Type
, module Data.Sv.Encode.Options

Expand Down

0 comments on commit 63a6092

Please sign in to comment.