Safe Haskell | None |
---|---|
Language | Haskell2010 |
Infer row types from comma-separated values (CSV) data and read that data from files. Template Haskell is used to generate the necessary types so that you can write type safe programs referring to those types.
- type Separator = Text
- type QuoteChar = Char
- data QuotingMode
- data ParserOptions = ParserOptions {}
- defaultParser :: ParserOptions
- defaultSep :: Separator
- tokenizeRow :: ParserOptions -> Text -> [Text]
- reassembleRFC4180QuotedParts :: Separator -> QuoteChar -> [Text] -> [Text]
- prefixInference :: (ColumnTypeable a, Monoid a, Monad m) => ParserOptions -> Parser Text m [a]
- readColHeaders :: (ColumnTypeable a, Monoid a, Monad m) => ParserOptions -> Producer Text m () -> m [(Text, a)]
- class ReadRec rs where
- readFileLatin1Ln :: MonadSafe m => FilePath -> Producer Text m ()
- readRow :: ReadRec rs => ParserOptions -> Text -> Rec Maybe rs
- readTableMaybeOpt :: (MonadSafe m, ReadRec rs) => ParserOptions -> FilePath -> Producer (Rec Maybe rs) m ()
- pipeTableMaybeOpt :: (Monad m, ReadRec rs) => ParserOptions -> Pipe Text (Rec Maybe rs) m ()
- readTableMaybe :: (MonadSafe m, ReadRec rs) => FilePath -> Producer (Rec Maybe rs) m ()
- pipeTableMaybe :: (Monad m, ReadRec rs) => Pipe Text (Rec Maybe rs) m ()
- readTableOpt :: forall m rs. (MonadSafe m, ReadRec rs) => ParserOptions -> FilePath -> Producer (Record rs) m ()
- pipeTableOpt :: (ReadRec rs, Monad m) => ParserOptions -> Pipe Text (Record rs) m ()
- readTable :: forall m rs. (MonadSafe m, ReadRec rs) => FilePath -> Producer (Record rs) m ()
- pipeTable :: (ReadRec rs, Monad m) => Pipe Text (Record rs) m ()
- recDec :: [(Text, Q Type)] -> Q Type
- capitalize1 :: Text -> Text
- sanitizeTypeName :: Text -> Text
- mkColTDec :: TypeQ -> Name -> DecQ
- mkColPDec :: Name -> TypeQ -> Text -> DecsQ
- lowerHead :: Text -> Maybe Text
- colDec :: ColumnTypeable a => Text -> Text -> a -> DecsQ
- declareColumn :: Text -> Name -> DecsQ
- data RowGen a = RowGen {
- columnNames :: [String]
- tablePrefix :: String
- separator :: Separator
- rowTypeName :: String
- columnUniverse :: Proxy a
- lineReader :: Producer Text (SafeT IO) ()
- colQ :: Name -> Q Exp
- rowGen :: FilePath -> RowGen Columns
- tableType :: String -> FilePath -> DecsQ
- tableTypes :: String -> FilePath -> DecsQ
- prefixSize :: Int
- tableType' :: forall a. (ColumnTypeable a, Monoid a) => RowGen a -> DecsQ
- colNamesP :: Monad m => ParserOptions -> Producer Text m () -> m [Text]
- tableTypesText' :: forall a. (ColumnTypeable a, Monoid a) => RowGen a -> DecsQ
- tableTypes' :: forall a. (ColumnTypeable a, Monoid a) => RowGen a -> DecsQ
- produceCSV :: forall f ts m. (ColumnHeaders ts, AsVinyl ts, Foldable f, Monad m, RecAll Identity (UnColumn ts) Show) => f (Record ts) -> Producer String m ()
- writeCSV :: (ColumnHeaders ts, AsVinyl ts, Foldable f, RecAll Identity (UnColumn ts) Show) => FilePath -> f (Record ts) -> IO ()
Documentation
data QuotingMode Source #
NoQuoting | No quoting enabled. The separator may not appear in values |
RFC4180Quoting QuoteChar | Quoted values with the given quoting character. Quotes are escaped by doubling them. Mostly RFC4180 compliant, except doesn't support newlines in values |
defaultParser :: ParserOptions Source #
Default ParseOptions
get column names from a header line, and
use commas to separate columns.
defaultSep :: Separator Source #
Default separator string.
Parsing
tokenizeRow :: ParserOptions -> Text -> [Text] Source #
Helper to split a Text
on commas and strip leading and
trailing whitespace from each resulting chunk.
reassembleRFC4180QuotedParts :: Separator -> QuoteChar -> [Text] -> [Text] Source #
Post processing applied to a list of tokens split by the separator which should have quoted sections reassembeld
prefixInference :: (ColumnTypeable a, Monoid a, Monad m) => ParserOptions -> Parser Text m [a] Source #
Infer column types from a prefix (up to 1000 lines) of a CSV file.
readColHeaders :: (ColumnTypeable a, Monoid a, Monad m) => ParserOptions -> Producer Text m () -> m [(Text, a)] Source #
Extract column names and inferred types from a CSV file.
Loading Data
class ReadRec rs where Source #
Parsing each component of a RecF
from a list of text chunks,
one chunk per record component.
readFileLatin1Ln :: MonadSafe m => FilePath -> Producer Text m () Source #
Produce the lines of a latin1 (or ISO8859 Part 1) encoded file as ’T.Text’ values. Similar to ’PT.readFileLn’ that uses the system locale for decoding, but built on the ’PT.decodeIso8859_1’ decoder.
readRow :: ReadRec rs => ParserOptions -> Text -> Rec Maybe rs Source #
Read a RecF
from one line of CSV.
readTableMaybeOpt :: (MonadSafe m, ReadRec rs) => ParserOptions -> FilePath -> Producer (Rec Maybe rs) m () Source #
Produce rows where any given entry can fail to parse.
pipeTableMaybeOpt :: (Monad m, ReadRec rs) => ParserOptions -> Pipe Text (Rec Maybe rs) m () Source #
Stream lines of CSV data into rows of ’Rec’ values values where any given entry can fail to parse.
readTableMaybe :: (MonadSafe m, ReadRec rs) => FilePath -> Producer (Rec Maybe rs) m () Source #
Produce rows where any given entry can fail to parse.
pipeTableMaybe :: (Monad m, ReadRec rs) => Pipe Text (Rec Maybe rs) m () Source #
Stream lines of CSV data into rows of ’Rec’ values where any given entry can fail to parse.
readTableOpt :: forall m rs. (MonadSafe m, ReadRec rs) => ParserOptions -> FilePath -> Producer (Record rs) m () Source #
Returns a producer of rows for which each column was successfully parsed.
pipeTableOpt :: (ReadRec rs, Monad m) => ParserOptions -> Pipe Text (Record rs) m () Source #
Pipe lines of CSV text into rows for which each column was successfully parsed.
readTable :: forall m rs. (MonadSafe m, ReadRec rs) => FilePath -> Producer (Record rs) m () Source #
Returns a producer of rows for which each column was successfully parsed.
pipeTable :: (ReadRec rs, Monad m) => Pipe Text (Record rs) m () Source #
Pipe lines of CSV text into rows for which each column was successfully parsed.
Template Haskell
sanitizeTypeName :: Text -> Text Source #
Massage a column name from a CSV file into a valid Haskell type identifier.
mkColPDec :: Name -> TypeQ -> Text -> DecsQ Source #
Declare a singleton value of the given column type and lenses for working with that column.
colDec :: ColumnTypeable a => Text -> Text -> a -> DecsQ Source #
For each column, we declare a type synonym for its type, and a Proxy value of that type.
declareColumn :: Text -> Name -> DecsQ Source #
Splice for manually declaring a column of a given type. For
example, declareColumn "x2" ''Double
will declare a type synonym
type X2 = "x2" :-> Double
and a lens x2
.
Default CSV Parsing
Control how row and named column types are generated.
RowGen | |
|
colQ :: Name -> Q Exp Source #
Shorthand for a Proxy
value of ColumnUniverse
applied to the
given type list.
tableType :: String -> FilePath -> DecsQ Source #
Generate a type for each row of a table. This will be something
like Record ["x" :-> a, "y" :-> b, "z" :-> c]
.
tableTypes :: String -> FilePath -> DecsQ Source #
Like tableType
, but additionally generates a type synonym for
each column, and a proxy value of that type. If the CSV file has
column names "foo", "bar", and "baz", then this will declare
type Foo = "foo" :-> Int
, for example, foo = rlens (Proxy :: Proxy
Foo)
, and foo' = rlens' (Proxy :: Proxy Foo)
.
Customized Data Set Parsing
prefixSize :: Int Source #
Inspect no more than this many lines when inferring column types.
tableType' :: forall a. (ColumnTypeable a, Monoid a) => RowGen a -> DecsQ Source #
Generate a type for a row of a table. This will be something like
Record ["x" :-> a, "y" :-> b, "z" :-> c]
. Column type synonyms
are not generated (see tableTypes'
).
colNamesP :: Monad m => ParserOptions -> Producer Text m () -> m [Text] Source #
Tokenize the first line of a ’P.Producer’.
tableTypesText' :: forall a. (ColumnTypeable a, Monoid a) => RowGen a -> DecsQ Source #
Generate a type for a row of a table all of whose columns remain
unparsed Text
values.
tableTypes' :: forall a. (ColumnTypeable a, Monoid a) => RowGen a -> DecsQ Source #
Like tableType'
, but additionally generates a type synonym for
each column, and a proxy value of that type. If the CSV file has
column names "foo", "bar", and "baz", then this will declare
type Foo = "foo" :-> Int
, for example, foo = rlens (Proxy ::
Proxy Foo)
, and foo' = rlens' (Proxy :: Proxy Foo)
.