private PropertyModel[] GetPropertyModels(string filePath, bool useTypeInference) { ExpandoObject headerNames; using var reader = new StreamReader(filePath); using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); csv.Configuration.BadDataFound = context => { }; csv.Configuration.HasHeaderRecord = false; headerNames = csv.GetRecords <object>().First() as ExpandoObject; var propertiesModels = headerNames.Select(x => new PropertyModel { RecordHeaderName = x.Value.ToString() }).ToArray(); if (!useTypeInference) { return(propertiesModels); } var records = csv.GetRecords <object>().Cast <IDictionary <string, object> >().ToArray(); foreach (var record in records) { for (int i = 0; i < record.Count; i++) { if (propertiesModels[i].TypeInferenceState == TypeInferenceStates.MultipleTypes) { continue; } var previousState = propertiesModels[i].TypeInferenceState; var previousType = propertiesModels[i].CSharpType; var inferredType = CSharpSourceHelper.TryInferredCSharpType(record.ElementAt(i).Value); if (inferredType == null) { continue; } if (previousState == TypeInferenceStates.Inferred && previousType != inferredType) { propertiesModels[i].TypeInferenceState = TypeInferenceStates.MultipleTypes; propertiesModels[i].CSharpType = null; continue; } propertiesModels[i].CSharpType = inferredType; propertiesModels[i].TypeInferenceState = TypeInferenceStates.Inferred; } } return(propertiesModels); }
public static CSharpSourceGeneratorInput CreateInput(string jsonFilePath, string dataSourceName) { return(new CSharpSourceGeneratorInput() { DataContextTypeName = "Record", DataContextTypeDefaultPropertiesType = "dynamic", DataProviderFullIdentifier = "Davidlep.LINQPadDrivers.SimpleJsonDriver.DataProvider", DataProviderMethod = "GetRecordsJson", DataSourceFilePath = jsonFilePath, DataSourceMemberName = dataSourceName, PropertyAttributeGenerator = (prop) => $" [JsonProperty(\"{CSharpSourceHelper.SanitizeStringForCSharpString(prop)}\")]", Imports = new[] { "System", "System.Collections.Generic", "System.Linq", "System.IO", "System.Dynamic", "Newtonsoft.Json", } }); }
public static CSharpSourceGeneratorInput CreateInput(string csvFilePath, string dataSourceName) { return(new CSharpSourceGeneratorInput() { DataContextTypeName = "Record", DataContextTypeDefaultPropertiesType = "string", DataProviderFullIdentifier = "Davidlep.LINQPadDrivers.SimpleCsvDriver.DataProvider", DataProviderMethod = "GetRecordsCsv", DataSourceFilePath = csvFilePath, DataSourceMemberName = dataSourceName, PropertyAttributeGenerator = (prop) => $" [Name(\"{CSharpSourceHelper.SanitizeStringForCSharpString(prop)}\")]", Imports = new[] { "System", "System.Collections.Generic", "System.Linq", "System.Globalization", "System.IO", "CsvHelper", "CsvHelper.Configuration.Attributes", } }); }