summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorqfpl <>2018-08-10 06:13:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-08-10 06:13:00 (GMT)
commit76c3793a5e122e48888b31b569bb4c79909e34aa (patch)
tree02773483460fb5d45c2fff7684a2eef4799b1374
parent7cf57baaffb0bf11c9a92bc10210073779887b2b (diff)
version 1.1.11.1.1
-rw-r--r--changelog.md4
-rw-r--r--src/Data/Sv.hs6
-rw-r--r--sv.cabal5
-rw-r--r--test/Data/Sv/EncodeTest.hs44
4 files changed, 54 insertions, 5 deletions
diff --git a/changelog.md b/changelog.md
index a9cad00..13f26aa 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,9 @@
# Revision history for sv
+## 1.1.1 -- 2018-08-10
+
+* Depend on sv-core 0.2.1 to get column-name-based encoding
+
## 1.1 -- 2018-07-25
* Add column-name-based decoding by adding parseDecodeNamed and associated functions
diff --git a/src/Data/Sv.hs b/src/Data/Sv.hs
index 00177f0..108ff25 100644
--- a/src/Data/Sv.hs
+++ b/src/Data/Sv.hs
@@ -32,6 +32,7 @@ module Data.Sv (
, decodeMay
, decodeEither
, decodeEither'
+ , (.:)
, (>>==)
, (==<<)
, module Data.Sv.Parse
@@ -43,7 +44,12 @@ module Data.Sv (
, encodeToFile
, encodeToHandle
, encodeBuilder
+ , encodeNamed
+ , encodeNamedToFile
+ , encodeNamedToHandle
+ , encodeNamedBuilder
, encodeRow
+ , (=:)
, module Data.Sv.Encode.Type
, module Data.Sv.Encode.Options
diff --git a/sv.cabal b/sv.cabal
index 377b299..bba1f64 100644
--- a/sv.cabal
+++ b/sv.cabal
@@ -1,5 +1,5 @@
name: sv
-version: 1.1
+version: 1.1.1
license: BSD3
license-file: LICENCE
author: George Wilson
@@ -46,6 +46,7 @@ description:
* Decoding a real CSV: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/Species.lhs Species.lhs>
* Decoding by column name: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/Columnar.hs Columnar.hs>
* Encoding data to a CSV: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/Encoding.hs Encoding.hs>
+ * Encoding data to a CSV with a header: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/EncodingWithHeader.hs EncodingWithHeader.hs>
* Handling NULL and Unknown occuring in a column of numbers: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/Numbers.hs Numbers.hs>
* Dealing with non-rectangular data: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/Ragged.hs Ragged.hs>
* Integrating with an existing attoparsec parser to read date stamps: <https://github.com/qfpl/sv/blob/master/examples/src/Data/Sv/Example/TableTennis.hs TableTennis.hs>
@@ -97,7 +98,7 @@ library
, contravariant >= 1.2 && < 1.6
, hw-dsv >= 0.2.1 && < 0.3
, semigroupoids >= 5 && <6
- , sv-core >= 0.2 && < 0.3
+ , sv-core >= 0.2.1 && < 0.3
, transformers >= 0.2 && < 0.6
, utf8-string >= 1 && < 1.1
, validation >= 1 && < 1.1
diff --git a/test/Data/Sv/EncodeTest.hs b/test/Data/Sv/EncodeTest.hs
index 77b30c8..775904a 100644
--- a/test/Data/Sv/EncodeTest.hs
+++ b/test/Data/Sv/EncodeTest.hs
@@ -7,6 +7,7 @@ import Control.Lens (makeClassyPrisms)
import Data.Functor.Contravariant
import Data.Functor.Contravariant.Divisible
import Data.Semigroup ((<>))
+import Data.Text (Text)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=))
@@ -21,9 +22,6 @@ makeClassyPrisms ''IntOrString
data IntAndString = IAS { getInt :: Int, getString :: String }
-intAndString :: Encode IntAndString
-intAndString = contramap getInt E.int <> contramap getString E.string
-
test_Encode :: TestTree
test_Encode =
testGroup "Encode" [
@@ -31,8 +29,13 @@ test_Encode =
, decidableTests
, encodeTests
, escapeTests
+ , encodeNamedTests
]
+intAndString :: Encode IntAndString
+intAndString = contramap getInt E.int <> contramap getString E.string
+
+
opts :: EncodeOptions
opts = defaultEncodeOptions
@@ -87,3 +90,38 @@ escapeTests =
, testCase "bytestring - lazy" $
encodeRow E.lazyByteString opts "\"" @?= "\"\"\"\""
]
+
+data Three = Three {
+ int :: Int
+, double :: Double
+, text :: Text
+} deriving (Eq, Ord, Show)
+
+three :: NameEncode Three
+three =
+ E.named "first" (contramap int E.int)
+ <> E.named "\"Second\"" (contramap double E.double)
+ <> E.named "third" (contramap text E.text)
+
+myInt :: NameEncode Int
+myInt = E.named "my int" E.int
+
+encodeNamedTests :: TestTree
+encodeNamedTests = testGroup "named" [
+ testCase "empty decoder" $
+ encodeNamed mempty opts [] @?= ""
+ , testCase "single column, zero rows" $
+ encodeNamed myInt opts [] @?= "my int"
+ , testCase "single column, one row" $
+ encodeNamed myInt opts [5] @?= "my int\n5"
+ , testCase "single column, many rows" $
+ encodeNamed myInt opts [1..5] @?= "my int\n1\n2\n3\n4\n5"
+ , testCase "multiple columns, zero rows" $
+ encodeNamed three opts [] @?= "first,\"\"\"Second\"\"\",third"
+ , testCase "multiple columns, one row" $
+ encodeNamed three opts [Three 1 2 "th\"ree"]
+ @?= "first,\"\"\"Second\"\"\",third\n1,2.0,\"th\"\"ree\""
+ , testCase "multiple columns, multiple rows" $
+ encodeNamed three opts [Three 1 2 "three", Three 4 5 "SIX"]
+ @?= "first,\"\"\"Second\"\"\",third\n1,2.0,three\n4,5.0,SIX"
+ ]