Skip to content

Commit c445a0c

Browse files
committed
feat: Add TH function for deriving schema from CSV file.
1 parent c89c5a1 commit c445a0c

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/DataFrame/Typed.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ module DataFrame.Typed (
165165

166166
-- * Template Haskell
167167
deriveSchema,
168+
deriveSchemaFromCsvFile,
168169

169170
-- * Schema type families (for advanced use)
170171
Lookup,
@@ -208,7 +209,7 @@ import DataFrame.Typed.Freeze (freeze, freezeWithError, thaw, unsafeFreeze)
208209
import DataFrame.Typed.Join (fullOuterJoin, innerJoin, leftJoin, rightJoin)
209210
import DataFrame.Typed.Operations
210211
import DataFrame.Typed.Schema
211-
import DataFrame.Typed.TH (deriveSchema)
212+
import DataFrame.Typed.TH (deriveSchema, deriveSchemaFromCsvFile)
212213
import DataFrame.Typed.Types (
213214
Column,
214215
TExpr (..),

src/DataFrame/Typed/Operations.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ derive ::
215215
, Columnable a
216216
, AssertAbsent name cols
217217
) =>
218-
TExpr cols a -> TypedDataFrame cols -> TypedDataFrame (Snoc cols (T.Column name a))
218+
TExpr cols a ->
219+
TypedDataFrame cols ->
220+
TypedDataFrame (Snoc cols (T.Column name a))
219221
derive (TExpr expr) (TDF df) = unsafeFreeze (D.derive colName expr df)
220222
where
221223
colName = T.pack (symbolVal (Proxy @name))

src/DataFrame/Typed/Schema.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type family Lookup (name :: Symbol) (cols :: [Type]) :: Type where
7979

8080
-- | Add type to the end of a list.
8181
type family Snoc (xs :: [k]) (x :: k) :: [k] where
82-
Snoc '[] x = '[x]
82+
Snoc '[] x = '[x]
8383
Snoc (y ': ys) x = y ': Snoc ys x
8484

8585
-- | Check whether a column name exists in a schema (type-level Bool).

src/DataFrame/Typed/TH.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
module DataFrame.Typed.TH (
88
-- * Schema inference
99
deriveSchema,
10+
deriveSchemaFromCsvFile,
1011

1112
-- * Re-export for TH splices
1213
TypedDataFrame,
1314
Column,
1415
) where
1516

17+
import Control.Monad.IO.Class
1618
import qualified Data.List as L
1719
import qualified Data.Map as M
1820
import qualified Data.Text as T
1921

2022
import Language.Haskell.TH
2123
import Language.Haskell.TH.Syntax (Lift (..))
2224

25+
import qualified DataFrame.IO.CSV as D
2326
import qualified DataFrame.Internal.Column as C
2427
import qualified DataFrame.Internal.DataFrame as D
2528
import DataFrame.Typed.Types (Column, TypedDataFrame)
@@ -46,6 +49,11 @@ deriveSchema typeName df = do
4649
let synName = mkName typeName
4750
pure [TySynD synName [] schemaType]
4851

52+
deriveSchemaFromCsvFile :: String -> String -> DecsQ
53+
deriveSchemaFromCsvFile typeName path = do
54+
df <- liftIO (D.readCsv path)
55+
deriveSchema typeName df
56+
4957
-------------------------------------------------------------------------------
5058
-- Internal helpers
5159
-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)