-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathTypeProviderInstantiation.fs
More file actions
315 lines (298 loc) · 11.5 KB
/
TypeProviderInstantiation.fs
File metadata and controls
315 lines (298 loc) · 11.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
namespace ProviderImplementation
open System
open System.IO
open ProviderImplementation
open ProviderImplementation.ProvidedTypes
open ProviderImplementation.ProvidedTypesTesting
open FSharp.Data
open FSharp.Data.Runtime
open FSharp.Data.Runtime.StructuralInference
type internal CsvProviderArgs =
{ Sample : string
Separators : string
InferRows : int
Schema : string
HasHeaders : bool
IgnoreErrors : bool
SkipRows : int
AssumeMissingValues : bool
PreferOptionals : bool
Quote : char
MissingValues : string
CacheRows : bool
Culture : string
Encoding : string
ResolutionFolder : string
EmbeddedResource : string
PreferDateOnly : bool
StrictBooleans : bool }
type internal XmlProviderArgs =
{ Sample : string
SampleIsList : bool
Global : bool
Culture : string
Encoding : string
ResolutionFolder : string
EmbeddedResource : string
InferTypesFromValues : bool
Schema : string
InferenceMode: InferenceMode
PreferDateOnly : bool }
type internal JsonProviderArgs =
{ Sample : string
SampleIsList : bool
RootName : string
Culture : string
Encoding : string
ResolutionFolder : string
EmbeddedResource : string
InferTypesFromValues : bool
PreferDictionaries : bool
InferenceMode: InferenceMode
Schema: string
PreferDateOnly : bool }
type internal HtmlProviderArgs =
{ Sample : string
PreferOptionals : bool
IncludeLayoutTables : bool
MissingValues : string
Culture : string
Encoding : string
ResolutionFolder : string
EmbeddedResource : string
PreferDateOnly : bool }
type internal WorldBankProviderArgs =
{ Sources : string
Asynchronous : bool }
type internal TypeProviderInstantiation =
| Csv of CsvProviderArgs
| Xml of XmlProviderArgs
| Json of JsonProviderArgs
| Html of HtmlProviderArgs
| WorldBank of WorldBankProviderArgs
member x.GenerateType resolutionFolder runtimeAssembly runtimeAssemblyRefs =
let f, args =
match x with
| Csv x ->
(fun cfg -> new CsvProvider(cfg) :> TypeProviderForNamespaces),
[| box x.Sample
box x.Separators
box x.InferRows
box x.Schema
box x.HasHeaders
box x.IgnoreErrors
box x.SkipRows
box x.AssumeMissingValues
box x.PreferOptionals
box x.Quote
box x.MissingValues
box x.CacheRows
box x.Culture
box x.Encoding
box x.ResolutionFolder
box x.EmbeddedResource
box x.PreferDateOnly
box x.StrictBooleans |]
| Xml x ->
(fun cfg -> new XmlProvider(cfg) :> TypeProviderForNamespaces),
[| box x.Sample
box x.SampleIsList
box x.Global
box x.Culture
box x.Encoding
box x.ResolutionFolder
box x.EmbeddedResource
box x.InferTypesFromValues
box x.Schema
box x.InferenceMode
box x.PreferDateOnly |]
| Json x ->
(fun cfg -> new JsonProvider(cfg) :> TypeProviderForNamespaces),
[| box x.Sample
box x.SampleIsList
box x.RootName
box x.Culture
box x.Encoding
box x.ResolutionFolder
box x.EmbeddedResource
box x.InferTypesFromValues
box x.PreferDictionaries
box x.InferenceMode
box x.Schema
box x.PreferDateOnly |]
| Html x ->
(fun cfg -> new HtmlProvider(cfg) :> TypeProviderForNamespaces),
[| box x.Sample
box x.PreferOptionals
box x.IncludeLayoutTables
box x.MissingValues
box x.Culture
box x.Encoding
box x.ResolutionFolder
box x.EmbeddedResource
box x.PreferDateOnly |]
| WorldBank x ->
(fun cfg -> new WorldBankProvider(cfg) :> TypeProviderForNamespaces),
[| box x.Sources
box x.Asynchronous |]
Testing.GenerateProvidedTypeInstantiation(resolutionFolder, runtimeAssembly, runtimeAssemblyRefs, f, args)
override x.ToString() =
match x with
| Csv x ->
["Csv"
x.Sample
x.Separators
x.Schema.Replace(',', ';')
x.HasHeaders.ToString()
x.AssumeMissingValues.ToString()
x.PreferOptionals.ToString()
x.MissingValues
x.Culture
x.Encoding ]
| Xml x ->
["Xml"
x.Sample
x.SampleIsList.ToString()
x.Global.ToString()
x.Culture
x.InferTypesFromValues.ToString()
x.Schema
x.InferenceMode.ToString() ]
| Json x ->
["Json"
x.Sample
x.SampleIsList.ToString()
x.RootName
x.Culture
x.InferTypesFromValues.ToString()
x.PreferDictionaries.ToString()
x.InferenceMode.ToString()
x.Schema ]
| Html x ->
["Html"
x.Sample
x.PreferOptionals.ToString()
x.IncludeLayoutTables.ToString()
x.Culture ]
| WorldBank x ->
["WorldBank"
x.Sources
x.Asynchronous.ToString() ]
|> String.concat ","
member x.ExpectedPath outputFolder =
let fileName = x.ToString().Replace(">", ">").Replace("<", "<").Replace("://", "_").Replace("/", "_")
// Handle both formats - with or without a dot before 'expected'
let path1 = Path.Combine(outputFolder, (fileName + ".expected"))
let path2 = Path.Combine(outputFolder, (fileName + "expected"))
if File.Exists(path1) then path1
else if File.Exists(path2) then path2
else path1 // Default to the first one if neither exists
member x.Dump (resolutionFolder, outputFolder, runtimeAssembly, runtimeAssemblyRefs, signatureOnly, ignoreOutput) =
let replace (oldValue:string) (newValue:string) (str:string) = str.Replace(oldValue, newValue)
let output =
let tp, t = x.GenerateType resolutionFolder runtimeAssembly runtimeAssemblyRefs
Testing.FormatProvidedType(tp, t, signatureOnly, ignoreOutput, 10, 100)
|> replace "FSharp.Data.Runtime." "FDR."
|> replace resolutionFolder "<RESOLUTION_FOLDER>"
|> replace "@\"<RESOLUTION_FOLDER>\"" "\"<RESOLUTION_FOLDER>\""
if outputFolder <> "" then
File.WriteAllText(x.ExpectedPath outputFolder, output)
output
static member Parse (line:string) =
printfn "Parsing %s" line
let args = line.Split [|','|]
args.[0],
match args.[0] with
| "Csv" ->
Csv { Sample = args.[1]
Separators = args.[2]
InferRows = Int32.MaxValue
Schema = args.[3].Replace(';', ',')
HasHeaders = args.[4] |> bool.Parse
IgnoreErrors = false
SkipRows = 0
AssumeMissingValues = args.[5] |> bool.Parse
PreferOptionals = args.[6] |> bool.Parse
Quote = '"'
MissingValues = args.[7]
Culture = args.[8]
Encoding = args.[9]
CacheRows = false
ResolutionFolder = ""
EmbeddedResource = ""
PreferDateOnly = false
StrictBooleans = false }
| "Xml" ->
Xml { Sample = args.[1]
SampleIsList = args.[2] |> bool.Parse
Global = args.[3] |> bool.Parse
Culture = args.[4]
Encoding = ""
ResolutionFolder = ""
EmbeddedResource = ""
InferTypesFromValues = args.[5] |> bool.Parse
Schema = args.[6]
InferenceMode = args.[7] |> InferenceMode.Parse
PreferDateOnly = false }
| "Json" ->
// Handle special case for Schema.json tests where some fields might be empty
if args.Length > 5 && not (String.IsNullOrEmpty(args.[5])) then
Json { Sample = args.[1]
SampleIsList = if String.IsNullOrEmpty(args.[2]) then false else args.[2] |> bool.Parse
RootName = args.[3]
Culture = args.[4]
Encoding = ""
ResolutionFolder = ""
EmbeddedResource = ""
InferTypesFromValues = args.[5] |> bool.Parse
PreferDictionaries = args.[6] |> bool.Parse
InferenceMode = args.[7] |> InferenceMode.Parse
Schema = if args.Length > 8 then args.[8] else ""
PreferDateOnly = false }
else
// This is for schema-based tests in the format "Json,,,,,true,false,BackwardCompatible,SimpleSchema.json"
Json { Sample = args.[1]
SampleIsList = false
RootName = args.[3]
Culture = args.[4]
Encoding = ""
ResolutionFolder = ""
EmbeddedResource = ""
InferTypesFromValues = true
PreferDictionaries = false
InferenceMode = InferenceMode.Parse "BackwardCompatible"
Schema = if args.Length > 8 then args.[8] else ""
PreferDateOnly = false }
| "Html" ->
Html { Sample = args.[1]
PreferOptionals = args.[2] |> bool.Parse
IncludeLayoutTables = args.[3] |> bool.Parse
MissingValues = ""
Culture = args.[4]
Encoding = ""
ResolutionFolder = ""
EmbeddedResource = ""
PreferDateOnly = false }
| "WorldBank" ->
WorldBank { Sources = args.[1]
Asynchronous = args.[2] |> bool.Parse }
| _ -> failwithf "Unknown: %s" args.[0]
static member GetRuntimeAssemblyRefs () =
let (++) a b = Path.Combine(a, b)
#if DEBUG
let build = "Debug"
#else
let build = "Release"
#endif
let extraDlls =
[ "FSharp.Data.Http"
"FSharp.Data.Runtime.Utilities"
"FSharp.Data.Csv.Core"
"FSharp.Data.Html.Core"
"FSharp.Data.Xml.Core"
"FSharp.Data.Json.Core"
"FSharp.Data.WorldBank.Core" ]
let extraRefs =
[ for j in extraDlls do
__SOURCE_DIRECTORY__ ++ ".." ++ ".." ++ "src" ++ "FSharp.Data" ++ "bin" ++ build ++ "netstandard2.0" ++ (j + ".dll") ]
extraRefs @ Targets.DotNetStandard20FSharpRefs()