-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCsvUtil.fs
58 lines (47 loc) · 1.93 KB
/
CsvUtil.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace CsvHelperfs
(*
FILE : CsvUtil.fs
AUTHOR : callmekohei
LICENSE :
DESC :
*)
module CsvUtil =
open System
open System.Collections.Generic
open System.Dynamic
open System.Reflection
open CsvHelper
let csvHeaderValidate<'T,'U> (args: HeaderValidatedArgs) =
let requiredHeaders =
// The value__ field is removed by specifying we only want the public, static fields.
typeof<'T>.GetFields(BindingFlags.Public ||| BindingFlags.Static)
|> Array.filter(fun fieldInfo -> isNull (Attribute.GetCustomAttribute(fieldInfo,typeof<'U>)))
|> Array.map(fun x -> x.Name)
let optionalHeaders =
// The value__ field is removed by specifying we only want the public, static fields.
typeof<'T>.GetFields(BindingFlags.Public ||| BindingFlags.Static)
|> Array.filter(fun fieldInfo -> isNull (Attribute.GetCustomAttribute(fieldInfo,typeof<'U>)) |> not )
|> Array.map(fun x -> x.Name)
let headers = args.Context.Parser.Record
let missingHeaders = requiredHeaders |> Array.except headers |> Array.except optionalHeaders
let unexpectedHeaders = headers |> Array.except requiredHeaders |> Array.except optionalHeaders
if (Seq.isEmpty missingHeaders && Seq.isEmpty unexpectedHeaders) |> not
then failwith <| $"""(Invalid Header) short: {String.Join(" ",missingHeaders)}. invalid: {String.Join(" ",unexpectedHeaders)}"""
let skipRows (csv: CsvReader) i =
let skipRow _ =
async {
return! csv.ReadAsync() |> Async.AwaitTask
}
let sequential = Async.Sequential >> Async.Ignore
async {
do!
seq { 1..i }
|> Seq.map skipRow
|> sequential
}
// Idictionary => ExpandoObject.IDictionary
let asExpandoIDictionary (idic:IDictionary<string,obj>) =
let expando = new ExpandoObject()
let expandoIDic = expando :> IDictionary<string,obj>
idic.Keys |> Seq.iter(fun k -> expandoIDic.Add(k,idic.Item(k)))
expandoIDic