-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
351 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace CsvParser | ||
|
||
open HCollections | ||
open System | ||
open ShapeSifter | ||
open ShapeSifter.Patterns | ||
open TypeEquality | ||
|
||
[<RequireQualifiedAccess>] | ||
module CsvParser = | ||
let parseCell<'a> : string -> 'a = | ||
match tType<'a> with | ||
| String (teq : Teq<'a, string>) -> Teq.castFrom teq | ||
| Bool (teq : Teq<'a, bool>) -> Boolean.Parse >> Teq.castFrom teq | ||
| Int (teq : Teq<'a, int>) -> Int32.Parse >> Teq.castFrom teq | ||
| Float (teq : Teq<'a, float>) -> Double.Parse >> Teq.castFrom teq | ||
| DateTime (teq : Teq<'a, DateTime>) -> DateTime.Parse >> Teq.castFrom teq | ||
| _ -> failwithf "Error - the type %s is not supported" typeof<'a>.FullName | ||
|
||
let rec parseRow<'ts> (ts : 'ts TypeList) (cells : string list) : 'ts HList = | ||
match TypeList.split ts with | ||
| Choice1Of2 (teq : Teq<'ts, unit>) -> HList.empty |> Teq.castFrom (HList.cong teq) | ||
| Choice2Of2 crate -> | ||
|
||
crate.Apply | ||
{ new TypeListConsEvaluator<_, _> with | ||
member _.Eval (us : 'us TypeList) (teq : Teq<'ts, 'u -> 'us>) = | ||
let head = cells |> List.head |> parseCell<'u> | ||
let tail = cells |> List.tail |> parseRow us | ||
|
||
HList.cons head tail |> Teq.castFrom (HList.cong teq) | ||
} | ||
|
||
let tryParse<'record> (data : string seq) : 'record seq option = | ||
match tType<'record> with | ||
| Record crate -> | ||
crate.Apply | ||
{ new RecordConvEvaluator<_, _> with | ||
member _.Eval | ||
(fields : RecordTypeField list) | ||
(ts : 'ts TypeList) | ||
(conv : Conv<'record, 'ts HList>) | ||
= | ||
data | ||
|> Seq.map (fun row -> row.Split ',' |> List.ofArray |> parseRow ts |> conv.From) | ||
|> Some | ||
} | ||
| _ -> None |
Oops, something went wrong.