static void ValidationOverridePOCOTest() { ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration(); var idConfig = new ChoCSVRecordFieldConfiguration("Id", 1); idConfig.Validators = new ValidationAttribute[] { new RequiredAttribute() }; config.CSVRecordFieldConfigurations.Add(idConfig); config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name", 2)); config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Salary", 3) { FieldType = typeof(ChoCurrency) }); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoCSVReader <EmployeeRecWithCurrency>(reader, config)) { parser.Configuration.ObjectValidationMode = ChoObjectValidationMode.ObjectLevel; writer.WriteLine("1,Carl,$100000"); writer.WriteLine("2,Mark,$50000"); writer.WriteLine("3,Tom,1000"); writer.Flush(); stream.Position = 0; object rec; while ((rec = parser.Read()) != null) { Console.WriteLine(rec.ToStringEx()); } } }
private static void OldTest() { //var t = ChoTypeDescriptor.GetPropetyAttributes<ChoTypeConverterAttribute>(ChoTypeDescriptor.GetProperty<ChoTypeConverterAttribute>(typeof(EmployeeRecMeta), "Name")).ToArray(); //return; //ChoMetadataObjectCache.Default.Attach(typeof(EmployeeRec), new EmployeeRecMeta()); //string v = @"4,'123\r\n4,abc'"; //foreach (var ss in v.SplitNTrim(",", ChoStringSplitOptions.None, '\'')) // Console.WriteLine(ss + "-"); //return; ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration(); //config.AutoDiscoverColumns = false; config.FileHeaderConfiguration.HasHeaderRecord = true; //config.CSVFileHeaderConfiguration.FillChar = '$'; config.ThrowAndStopOnMissingField = false; //config.HasExcelSeparator = true; config.ColumnCountStrict = false; //config.MapRecordFields<EmployeeRec>(); ChoCSVRecordFieldConfiguration idConfig = new ChoCSVRecordFieldConfiguration("Id", 1); idConfig.AddConverter(new IntConverter()); config.CSVRecordFieldConfigurations.Add(idConfig); config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name", 2)); config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name1", 2)); dynamic rec = new ExpandoObject(); rec.Id = 1; rec.Name = "Raj"; //using (var wr = new ChoCSVWriter("EmpOut.csv", config)) //{ // wr.Write(new List<ExpandoObject>() { rec }); //} //List<EmployeeRec> recs = new List<EmployeeRec>(); //recs.Add(new EmployeeRec() { Id = 1, Name = "Raj" }); //recs.Add(new EmployeeRec() { Id = 2, Name = "Mark" }); //using (var stream = new MemoryStream()) //using (var reader = new StreamReader(stream)) //using (var writer = new StreamWriter(stream)) //using (var parser = new ChoCSVWriter<EmployeeRec>(writer, config)) //{ // parser.Write(recs); // writer.Flush(); // stream.Position = 0; // Console.WriteLine(reader.ReadToEnd()); //} //return; //string txt = "Id, Name\r\n1, Mark"; //foreach (var e in ChoCSVReader.LoadText(txt)) // Console.WriteLine(e.ToStringEx()); //return; //dynamic row; //using (var stream = new MemoryStream()) //using (var reader = new StreamReader(stream)) //using (var writer = new StreamWriter(stream)) //using (var parser = new ChoCSVReader(reader, config)) //{ // //writer.WriteLine("Id,Name"); // writer.WriteLine("1,Carl"); // writer.WriteLine("2,Mark"); // writer.Flush(); // stream.Position = 0; // while ((row = parser.Read()) != null) // { // Console.WriteLine(row.Id); // } //} //return; //DataTable dt = new ChoCSVReader<EmployeeRec>("Emp.csv").AsDataTable(); //var z = dt.Rows.Count; //return; foreach (var item in new ChoCSVReader <EmployeeRec>("Emp.csv")) { Console.WriteLine(item.ToStringEx()); } return; //var reader = new ChoCSVReader<EmployeeRec>("Emp.csv"); //var rec = (object)null; //while ((rec = reader.Read()) != null) // Console.WriteLine(rec.ToStringEx()); //var config = new ChoCSVRecordConfiguration(typeof(EmployeeRec)); //var e = new ChoCSVReader("Emp.csv", config); //dynamic i; //while ((i = e.Read()) != null) // Console.WriteLine(i.Id); ChoETLFramework.Initialize(); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoCSVReader <EmployeeRec>(reader)) { writer.WriteLine("Id,Name"); writer.WriteLine("1,Carl"); writer.WriteLine("2,Mark"); writer.Flush(); stream.Position = 0; //var dr = parser.AsDataReader(); //while (dr.Read()) //{ // Console.WriteLine(dr[0]); //} object row1 = null; //parser.Configuration.ColumnCountStrict = true; while ((row1 = parser.Read()) != null) { Console.WriteLine(row1.ToStringEx()); } } }
private string CleanFieldValue(ChoCSVRecordFieldConfiguration config, Type fieldType, string fieldValue) { if (fieldValue == null) { return(fieldValue); } ChoFieldValueTrimOption fieldValueTrimOption = ChoFieldValueTrimOption.Trim; if (config.FieldValueTrimOption == null) { //if (fieldType == typeof(string)) // fieldValueTrimOption = ChoFieldValueTrimOption.None; } else { fieldValueTrimOption = config.FieldValueTrimOption.Value; } switch (fieldValueTrimOption) { case ChoFieldValueTrimOption.Trim: fieldValue = fieldValue.Trim(); break; case ChoFieldValueTrimOption.TrimStart: fieldValue = fieldValue.TrimStart(); break; case ChoFieldValueTrimOption.TrimEnd: fieldValue = fieldValue.TrimEnd(); break; } if (config.Size != null) { if (fieldValue.Length > config.Size.Value) { if (!config.Truncate) { throw new ChoHL7Exception("Incorrect field value length found for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(config.FieldName, config.Size.Value, fieldValue.Length)); } else { fieldValue = fieldValue.Substring(0, config.Size.Value); } } } //char startChar; //char endChar; //quotes are quoted and doubled (excel) i.e. 15" -> field1,"15""",field3 //if (fieldValue.Contains(Configuration.DoubleQuoteChar)) // fieldValue = fieldValue.Replace(Configuration.DoubleQuoteChar, Configuration.QuoteChar.ToString()); //if (fieldValue.Contains(Configuration.BackslashQuote)) // fieldValue = fieldValue.Replace(Configuration.BackslashQuote, Configuration.QuoteChar.ToString()); //if (fieldValue.Length >= 2) //{ // startChar = fieldValue[0]; // endChar = fieldValue[fieldValue.Length - 1]; // if (config.QuoteField != null && config.QuoteField.Value && startChar == Configuration.QuoteChar && endChar == Configuration.QuoteChar) // return fieldValue.Substring(1, fieldValue.Length - 2); // else if (startChar == Configuration.QuoteChar && endChar == Configuration.QuoteChar && // (fieldValue.Contains(Configuration.Delimiter) // || fieldValue.Contains(Configuration.EOLDelimiter))) // return fieldValue.Substring(1, fieldValue.Length - 2); //} return(fieldValue); }