wrote basic structure of decodeRows

This commit is contained in:
Jonathan Lamothe 2022-04-19 16:03:14 -04:00
parent 365dc3995b
commit 08c77d4d0f
5 changed files with 70 additions and 4 deletions

View File

@ -29,6 +29,9 @@ library
ghc-options: -Wall
build-depends:
base >=4.7 && <5
, bytestring
, conduit >=1.3.4.2 && <1.4
, text
default-language: Haskell2010
autogen-modules: Paths_csv_slurp
@ -43,7 +46,10 @@ test-suite csv-slurp-test
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, bytestring
, conduit >=1.3.4.2 && <1.4
, csv-slurp
, hspec >=2.8.5 && <2.9
, text
default-language: Haskell2010
autogen-modules: Paths_csv_slurp

View File

@ -23,6 +23,9 @@ ghc-options:
dependencies:
- base >= 4.7 && < 5
- bytestring
- conduit >= 1.3.4.2 && < 1.4
- text
library:
source-dirs: src

View File

@ -23,6 +23,29 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
module Data.CSV.Slurp where
{-# LANGUAGE OverloadedStrings #-}
module Data.CSV.Slurp (
decodeRows,
decodeRawRows,
decodeUTF8,
) where
import Conduit (ConduitT, mapC, (.|))
import qualified Data.ByteString as BS
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
-- | decode the rows from a stream of ByteStrings
decodeRows :: Monad m => ConduitT BS.ByteString [T.Text] m ()
decodeRows = decodeRawRows .| mapC (map $ fromMaybe "" . decodeUTF8)
-- | decode the rows returning raw ByteStrings instead of text
decodeRawRows :: Monad m => ConduitT BS.ByteString [BS.ByteString] m ()
decodeRawRows = return ()
-- | decode a raw ByteString into Text (if possible)
decodeUTF8 :: BS.ByteString -> Maybe T.Text
decodeUTF8 = const Nothing
--jl

View File

@ -18,11 +18,45 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
{-# LANGUAGE OverloadedStrings #-}
module Data.CSV.SlurpSpec (spec) where
import Test.Hspec (Spec, describe)
import Conduit (runConduit, (.|))
import Data.Conduit.List (consume, sourceList)
import Test.Hspec (Spec, context, describe, shouldBe, xit)
import Data.CSV.Slurp
spec :: Spec
spec = describe "Data.CSV.Slurp" $ return ()
spec = describe "Data.CSV.Slurp"
decodeRowsSpec
decodeRowsSpec :: Spec
decodeRowsSpec = describe "decodeRows" $ mapM_
( \(label, input, expected) -> context label $ do
result <- runConduit $ sourceList input .| decodeRows .| consume
let
expLen = length expected
resLen = length result
xit ("should have " ++ show expLen ++ " rows") $
resLen `shouldBe` expLen
mapM_
( \(n, expected', result') -> context ("row " ++ show n) $
xit ("should be " ++ show expected') $
result' `shouldBe` expected'
) $ zip3 [(0::Int)..] expected result
)
-- label, input, expected
[ ( "valid", validIn, validRes )
, ( "invalid", invalidIn, [] )
, ( "empty", [], [] )
]
where
validIn = ["foo,bar\r\n", "baz,quuux\r\n"]
invalidIn = ["\"a"]
validRes = [["foo", "bar"], ["baz", "quux"]]
--jl

View File

@ -25,6 +25,6 @@ import Test.Hspec (hspec)
import qualified Data.CSV.SlurpSpec as Slurp
main :: IO ()
main = hspec $ Slurp.spec
main = hspec Slurp.spec
--jl