implemented labelFields
This commit is contained in:
@@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
|
||||
|
||||
module Data.CSV.Sip (
|
||||
labelFields,
|
||||
decodeRows,
|
||||
decodeRawRows,
|
||||
decodeUTF8,
|
||||
@@ -37,11 +38,18 @@ import Control.Monad (unless)
|
||||
import Control.Monad.Trans.Class (lift)
|
||||
import Control.Monad.Trans.State (StateT, evalStateT, get, gets, modify)
|
||||
import qualified Data.ByteString as BS
|
||||
import qualified Data.Map as M
|
||||
import Data.Maybe (fromMaybe)
|
||||
import qualified Data.Text as T
|
||||
import Data.Text.Encoding (decodeUtf8')
|
||||
import Data.Word (Word8)
|
||||
|
||||
-- | read a CSV stream, using the first row as a header containing field labels
|
||||
labelFields :: (Monad m, Ord a) => ConduitT [a] (M.Map a a) m ()
|
||||
labelFields = await >>= \case
|
||||
Just headers -> labelLoop headers
|
||||
Nothing -> return ()
|
||||
|
||||
-- | decode the rows from a stream of ByteStrings
|
||||
decodeRows :: Monad m => ConduitT BS.ByteString [T.Text] m ()
|
||||
decodeRows = decodeRawRows .| mapC (map $ fromMaybe "" . decodeUTF8)
|
||||
@@ -83,6 +91,15 @@ newDecodeState = DecodeState
|
||||
, collected = ""
|
||||
}
|
||||
|
||||
-- Conduits
|
||||
|
||||
labelLoop :: (Monad m, Ord a) => [a] -> ConduitT [a] (M.Map a a) m ()
|
||||
labelLoop headers = await >>= \case
|
||||
Just values -> do
|
||||
yield $ M.fromList $ zip headers values
|
||||
labelLoop headers
|
||||
Nothing -> return ()
|
||||
|
||||
-- Decoders
|
||||
|
||||
decodeLoop :: Monad m => Decoder m
|
||||
|
||||
Reference in New Issue
Block a user