-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathCsvGenerator.fs
More file actions
180 lines (144 loc) · 6.92 KB
/
CsvGenerator.fs
File metadata and controls
180 lines (144 loc) · 6.92 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// --------------------------------------------------------------------------------------
// CSV type provider - generate code for accessing inferred elements
// --------------------------------------------------------------------------------------
namespace ProviderImplementation
open System
open System.Reflection
open FSharp.Quotations
open FSharp.Reflection
open FSharp.Data
open FSharp.Data.Runtime
open ProviderImplementation
open ProviderImplementation.ProvidedTypes
open ProviderImplementation.QuotationBuilder
module internal CsvTypeBuilder =
type private FieldInfo =
{
/// The representation type that is part of the tuple we extract the field from
TypeForTuple: Type
/// The provided property corresponding to the field
ProvidedProperty: ProvidedProperty
Convert: Expr -> Expr
ConvertBack: Expr -> Expr
/// The provided parameter corresponding to the field
ProvidedParameter: ProvidedParameter
}
let generateTypes asm ns typeName (missingValuesStr, cultureStr) useOriginalNames inferredFields =
let fields =
inferredFields
|> List.mapi (fun index field ->
let typ, typWithoutMeasure, conv, convBack =
ConversionsGenerator.convertStringValue missingValuesStr cultureStr false field
let propertyName =
if useOriginalNames then
field.Name
else
NameUtils.capitalizeFirstLetter field.Name
let prop =
ProvidedProperty(
propertyName,
typ,
getterCode =
fun (Singleton row) ->
match inferredFields with
| [ _ ] -> row
| _ -> Expr.TupleGet(row, index)
)
let convert rowVarExpr =
conv <@ TextConversions.AsString((%%rowVarExpr: string[]).[index]) @>
let convertBack rowVarExpr =
convBack (
match inferredFields with
| [ _ ] -> rowVarExpr
| _ -> Expr.TupleGet(rowVarExpr, index)
)
let paramName =
if useOriginalNames then
field.Name
else
NameUtils.niceCamelName propertyName
{ TypeForTuple = typWithoutMeasure
ProvidedProperty = prop
Convert = convert
ConvertBack = convertBack
ProvidedParameter = ProvidedParameter(paramName, typ) })
// The erased row type will be a tuple of all the field types (without the units of measure). If there is a single column then it is just the column type.
let rowErasedType =
match fields with
| [ field ] -> field.TypeForTuple
| _ -> FSharpType.MakeTupleType([| for field in fields -> field.TypeForTuple |])
let rowType =
ProvidedTypeDefinition("Row", Some rowErasedType, hideObjectMethods = true, nonNullable = true)
let ctor =
let parameters = [ for field in fields -> field.ProvidedParameter ]
let invoke args =
match args with
| [ arg ] -> arg
| _ -> Expr.NewTuple(args)
ProvidedConstructor(parameters, invokeCode = invoke)
rowType.AddMember ctor
// Each property of the generated row type will simply be a tuple get
for field in fields do
rowType.AddMember field.ProvidedProperty
// Add With* methods so users can create a modified copy of a row
// e.g. myRow.WithAmount(42.0) returns a new Row identical to myRow except Amount = 42.0
for targetIdx, targetField in List.indexed fields do
let methodName = "With" + targetField.ProvidedProperty.Name
let withMethod =
ProvidedMethod(
methodName,
[ ProvidedParameter(targetField.ProvidedParameter.Name, targetField.ProvidedProperty.PropertyType) ],
rowType,
invokeCode =
fun args ->
let row = args.[0]
let newVal = args.[1]
match fields with
| [ _ ] ->
// Single-column CSV: Row erases to the value itself
newVal
| _ ->
let tupleArgs =
fields
|> List.mapi (fun i _ -> if i = targetIdx then newVal else Expr.TupleGet(row, i))
Expr.NewTuple tupleArgs
)
rowType.AddMember withMethod
// The erased csv type will be parameterised by the tuple type
let csvErasedTypeWithRowErasedType =
typedefof<CsvFile<_>>.MakeGenericType(rowErasedType)
let csvErasedTypeWithGeneratedRowType =
typedefof<CsvFile<_>>.MakeGenericType(rowType)
let csvType =
ProvidedTypeDefinition(
asm,
ns,
typeName,
Some csvErasedTypeWithGeneratedRowType,
hideObjectMethods = true,
nonNullable = true
)
csvType.AddMember rowType
// Based on the set of fields, create a function that converts a string[] to the tuple type
let stringArrayToRow =
let parentVar = Var("parent", typeof<obj>)
let rowVar = Var("row", typeof<string[]>)
let rowVarExpr = Expr.Var rowVar
// Convert each element of the row using the appropriate conversion
let body =
match [ for field in fields -> field.Convert rowVarExpr ] with
| [ col ] -> col
| cols -> Expr.NewTuple cols
let delegateType =
typedefof<Func<_, _, _>>.MakeGenericType(typeof<obj>, typeof<string[]>, rowErasedType)
Expr.NewDelegate(delegateType, [ parentVar; rowVar ], body)
// Create a function that converts the tuple type to a string[]
let rowToStringArray =
let rowVar = Var("row", rowErasedType)
let rowVarExpr = Expr.Var rowVar
let body =
Expr.NewArray(typeof<string>, [ for field in fields -> field.ConvertBack rowVarExpr ])
let delegateType =
typedefof<Func<_, _>>.MakeGenericType(rowErasedType, typeof<string[]>)
Expr.NewDelegate(delegateType, [ rowVar ], body)
csvType, csvErasedTypeWithRowErasedType, rowType, stringArrayToRow, rowToStringArray