Example #1
0
        public void ConvertFromStringTest()
        {
            var converter = new CharConverter();

            var propertyMapData = new CsvPropertyMapData(null);

            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            Assert.AreEqual('a', converter.ConvertFromString("a", null, propertyMapData));
            Assert.AreEqual('a', converter.ConvertFromString(" a ", null, propertyMapData));
            Assert.AreEqual(' ', converter.ConvertFromString(" ", null, propertyMapData));

            try
            {
                converter.ConvertFromString(null, null, propertyMapData);
                Assert.Fail();
            }
            catch (CsvTypeConverterException)
            {
            }
        }
Example #2
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                                ? row.CurrentRecord.Length - 1
                                : propertyMapData.IndexEnd;

            var arraySize = indexEnd - propertyMapData.Index + 1;

            var array = (Array)ReflectionHelper.CreateInstance(propertyMapData.Property.PropertyType, arraySize);

            var type       = propertyMapData.Property.PropertyType.GetElementType();
            var arrayIndex = 0;

            for (var i = propertyMapData.Index; i <= indexEnd; i++)
            {
                array.SetValue(row.GetField(type, i), arrayIndex);
                arrayIndex++;
            }

            return(array);
        }
Example #3
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            // Since we're using the PropertyType here, this converter can be used for multiple types
            // as long as they implement IList.
            var list = (IList)ReflectionHelper.CreateInstance(propertyMapData.Member.MemberType());
            var type = propertyMapData.Member.MemberType().GetGenericArguments()[0];

            if (propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet)
            {
                // Use the name.
                var nameIndex = 0;
                while (true)
                {
                    object field;
                    if (!row.TryGetField(type, propertyMapData.Names.FirstOrDefault(), nameIndex, out field))
                    {
                        break;
                    }

                    list.Add(field);
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                                        ? row.CurrentRecord.Length - 1
                                        : propertyMapData.IndexEnd;

                for (var i = propertyMapData.Index; i <= indexEnd; i++)
                {
                    var field = row.GetField(type, i);

                    list.Add(field);
                }
            }

            return(list);
        }
        public void ConvertNoIndexEndTest()
        {
            var rowMock       = new Mock <ICsvReaderRow>();
            var currentRecord = new[] { "1", "one", "1", "2", "3" };

            rowMock.Setup(m => m.CurrentRecord).Returns(currentRecord);
            rowMock.Setup(m => m.GetField(It.IsAny <Type>(), It.IsAny <int>())).Returns <Type, int>((type, index) => Convert.ToInt32(currentRecord[index]));
            var data = new CsvPropertyMapData(typeof(Test).GetProperty("List"))
            {
                Index = 2
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter = new CollectionGenericConverter();
            var list      = (List <int>)converter.ConvertFromString("1", rowMock.Object, data);

            Assert.AreEqual(3, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
            Assert.AreEqual(3, list[2]);
        }
Example #5
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var type     = propertyMapData.Property.PropertyType.GetGenericArguments()[0];
            var listType = typeof(List <>);

            listType = listType.MakeGenericType(type);
            var list = (IList)ReflectionHelper.CreateInstance(listType);

            if (propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet)
            {
                // Use the name.
                var nameIndex = 0;
                while (true)
                {
                    object field;
                    if (!row.TryGetField(type, propertyMapData.Names.FirstOrDefault(), nameIndex, out field))
                    {
                        break;
                    }

                    list.Add(field);
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                                        ? row.CurrentRecord.Length - 1
                                        : propertyMapData.IndexEnd;

                for (var i = propertyMapData.Index; i <= indexEnd; i++)
                {
                    list.Add(row.GetField(type, i));
                }
            }

            return(list);
        }
        public void ReadConvertWithIndexEndTest()
        {
            var rowMock       = new Mock <ICsvReaderRow>();
            var currentRecord = new[] { "1", "one", "1", "2", "3" };

            rowMock.Setup(m => m.CurrentRecord).Returns(currentRecord);
            rowMock.Setup(m => m.GetField(It.IsAny <Type>(), It.IsAny <int>())).Returns <Type, int>((type, index) => Convert.ToInt32(currentRecord[index]));
            var data = new CsvPropertyMapData(typeof(Test).GetProperty("List"))
            {
                Index                = 2,
                IndexEnd             = 3,
                TypeConverterOptions = { CultureInfo = CultureInfo.CurrentCulture }
            };

            var converter  = new ArrayConverter();
            var enumerable = (int[])converter.ConvertFromString("1", rowMock.Object, data);
            var list       = enumerable.ToList();

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(1, list[0]);
            Assert.AreEqual(2, list[1]);
        }
Example #7
0
        public void ConvertWithIndexEndTest()
        {
            var rowMock       = new Mock <ICsvReaderRow>();
            var currentRecord = new[] { "1", "one", "1", "2", "3" };

            rowMock.Setup(m => m.CurrentRecord).Returns(currentRecord);
            rowMock.Setup(m => m.GetField(It.IsAny <int>())).Returns <int>(index => currentRecord[index]);
            var data = new CsvPropertyMapData(typeof(Test).GetProperty("List"))
            {
                Index    = 2,
                IndexEnd = 3
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter = new ArrayListConverter();
            var list      = (ArrayList)converter.ConvertFromString("1", rowMock.Object, data);

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual("1", list[0]);
            Assert.AreEqual("2", list[1]);
        }
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var keyType        = propertyMapData.Member.MemberType().GetGenericArguments()[0];
            var valueType      = propertyMapData.Member.MemberType().GetGenericArguments()[1];
            var dictionaryType = typeof(Dictionary <,>);

            dictionaryType = dictionaryType.MakeGenericType(keyType, valueType);
            var dictionary = (IDictionary)ReflectionHelper.CreateInstance(dictionaryType);

            var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                                ? row.CurrentRecord.Length - 1
                                : propertyMapData.IndexEnd;

            for (var i = propertyMapData.Index; i <= indexEnd; i++)
            {
                var field = row.GetField(valueType, i);

                dictionary.Add(row.FieldHeaders[i], field);
            }

            return(dictionary);
        }
Example #9
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="IReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString(string text, IReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var list = new List <string>();

            if (propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet)
            {
                // Use the name.
                var nameIndex = 0;
                while (true)
                {
                    string field;
                    if (!row.TryGetField(propertyMapData.Names.FirstOrDefault(), nameIndex, out field))
                    {
                        break;
                    }

                    list.Add(field);
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                                        ? row.Context.Record.Length - 1
                                        : propertyMapData.IndexEnd;

                for (var i = propertyMapData.Index; i <= indexEnd; i++)
                {
                    string field;
                    if (row.TryGetField(i, out field))
                    {
                        list.Add(field);
                    }
                }
            }

            return(list);
        }
        public void ConvertFromStringTest()
        {
            var converter = new ByteConverter();

            var propertyMapData = new CsvPropertyMapData(null);

            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var mockRow = new Mock <ICsvReaderRow>();

            Assert.AreEqual((byte)123, converter.ConvertFromString("123", null, propertyMapData));
            Assert.AreEqual((byte)123, converter.ConvertFromString(" 123 ", null, propertyMapData));

            try
            {
                converter.ConvertFromString(null, mockRow.Object, propertyMapData);
                Assert.Fail();
            }
            catch (CsvTypeConverterException)
            {
            }
        }
        public void ComponentModelCompatibilityTest()
        {
            var converter   = new TimeSpanConverter();
            var cmConverter = new System.ComponentModel.TimeSpanConverter();

            var propertyMapData = new CsvPropertyMapData(null);

            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;
            var rowMock = new Mock <ICsvReaderRow>();

            try
            {
                cmConverter.ConvertFromString("");
                Assert.Fail();
            }
            catch (FormatException) {}

            try
            {
                var val = (DateTime)converter.ConvertFromString("", rowMock.Object, propertyMapData);
                Assert.Fail();
            }
            catch (CsvTypeConverterException) {}

            try
            {
                cmConverter.ConvertFromString(null);
                Assert.Fail();
            }
            catch (NotSupportedException) { }

            try
            {
                converter.ConvertFromString(null, rowMock.Object, propertyMapData);
                Assert.Fail();
            }
            catch (CsvTypeConverterException) { }
        }
        public void ComponentModelCompatibilityTest()
        {
            var converter   = new DateTimeOffsetConverter();
            var cmConverter = new System.ComponentModel.DateTimeOffsetConverter();

            var propertyMapData = new CsvPropertyMapData(null);

            propertyMapData.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var mockRow = new Mock <IReaderRow>();

            try
            {
                cmConverter.ConvertFromString(null);
                Assert.Fail();
            }
            catch (NotSupportedException) { }

            try
            {
                converter.ConvertFromString(null, mockRow.Object, propertyMapData);
                Assert.Fail();
            }
            catch (CsvTypeConverterException) { }

            try
            {
                cmConverter.ConvertFromString("blah");
                Assert.Fail();
            }
            catch (FormatException) { }

            try
            {
                converter.ConvertFromString("blah", mockRow.Object, propertyMapData);
            }
            catch (FormatException) { }
        }
Example #13
0
        /// <summary>
        /// Converts the string to an object.
        /// </summary>
        /// <param name="text">The string to convert to an object.</param>
        /// <param name="row">The <see cref="ICsvReaderRow"/> for the current record.</param>
        /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property being created.</param>
        /// <returns>The object created from the string.</returns>
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var dictionary = new Dictionary <string, string>();

            if (propertyMapData.IsNameSet || row.Configuration.HasHeaderRecord && !propertyMapData.IsIndexSet)
            {
                // Use the name.
                var nameIndex = 0;
                while (true)
                {
                    string field;
                    var    name = propertyMapData.Names.FirstOrDefault() ?? string.Empty;
                    row.TryGetField(name, nameIndex, out field);
                    if (field == null)
                    {
                        break;
                    }

                    dictionary.Add(name, field);
                    nameIndex++;
                }
            }
            else
            {
                // Use the index.
                var indexEnd = propertyMapData.IndexEnd < propertyMapData.Index
                                        ? row.CurrentRecord.Length - 1
                                        : propertyMapData.IndexEnd;

                for (var i = propertyMapData.Index; i <= indexEnd; i++)
                {
                    dictionary.Add(row.FieldHeaders[i], row.GetField(i));
                }
            }

            return(dictionary);
        }
Example #14
0
        public void ConvertWithIndexEndTest()
        {
            var rowMock       = new Mock <ICsvReaderRow>();
            var headers       = new[] { "Id", "Name", "Prop1", "Prop2", "Prop3" };
            var currentRecord = new[] { "1", "One", "1", "2", "3" };

            rowMock.Setup(m => m.FieldHeaders).Returns(headers);
            rowMock.Setup(m => m.CurrentRecord).Returns(currentRecord);
            rowMock.Setup(m => m.GetField(It.IsAny <Type>(), It.IsAny <int>())).Returns <Type, int>((type, index) => Convert.ToInt32(currentRecord[index]));
            var data = new CsvPropertyMapData(typeof(Test).GetProperty("Dictionary"))
            {
                Index    = 2,
                IndexEnd = 3
            };

            data.TypeConverterOptions.CultureInfo = CultureInfo.CurrentCulture;

            var converter  = new IDictionaryGenericConverter();
            var dictionary = (IDictionary)converter.ConvertFromString("1", rowMock.Object, data);

            Assert.AreEqual(2, dictionary.Count);
            Assert.AreEqual(1, dictionary["Prop1"]);
            Assert.AreEqual(2, dictionary["Prop2"]);
        }
Example #15
0
 public override Object ConvertFromString(string textToConvert, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     if (string.IsNullOrWhiteSpace(textToConvert))
     {
         return(null);
     }
     try {
         int m = Int32.Parse(textToConvert);
     } catch {
         return(null); // sometimes the CSV data says "Black" instead of "22"
     }
     return(base.ConvertFromString(textToConvert, row, propertyMapData));
 }
 /// <summary>
 /// Converts the object to a string.
 /// </summary>
 /// <param name="value">The object to convert to a string.</param>
 /// <param name="row"></param>
 /// <param name="propertyMapData"></param>
 /// <returns>The string representation of the object.</returns>
 public override string ConvertToString(object value, IWriterRow row, CsvPropertyMapData propertyMapData)
 {
     return(UnderlyingTypeConverter.ConvertToString(value, row, propertyMapData));
 }
Example #17
0
 /// <summary>
 /// Converts the string to an object.
 /// </summary>
 /// <param name="text">The string to convert to an object.</param>
 /// <param name="row">The <see cref="IReaderRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being created.</param>
 /// <returns>The object created from the string.</returns>
 public virtual object ConvertFromString(string text, IReaderRow row, CsvPropertyMapData propertyMapData)
 {
     throw new CsvTypeConverterException((ReadingContext)row.Context, "The conversion cannot be performed.");
 }
Example #18
0
 public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     return("test");
 }
Example #19
0
 public object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
 {
     throw new NotImplementedException();
 }
Example #20
0
 public string ConvertToString(object value, ICsvWriterRow row, CsvPropertyMapData propertyMapData)
 {
     return("test");
 }
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var trimmed = text.Trim(new char[] { ' ', '"', '\n' });

            return(TimeUtils.DateStringToDateTimeUtc(trimmed));
        }
        public override object ConvertFromString(string text, ICsvReaderRow row, CsvPropertyMapData propertyMapData)
        {
            var dequotedText = text.Trim(new char[] { ' ', '"' });

            return(base.ConvertFromString(dequotedText, row, propertyMapData));
        }
 /// <summary>
 /// Throws an exception.
 /// </summary>
 /// <param name="value">The object to convert to a string.</param>
 /// <param name="row">The <see cref="ICsvWriterRow"/> for the current record.</param>
 /// <param name="propertyMapData">The <see cref="CsvPropertyMapData"/> for the property/field being written.</param>
 /// <returns>The string representation of the object.</returns>
 public override string ConvertToString(object value, ICsvWriterRow row, CsvPropertyMapData propertyMapData)
 {
     throw new CsvTypeConverterException("Converting IEnumerable types is not supported for a single field. " +
                                         "If you want to do this, create your own ITypeConverter and register " +
                                         "it in the TypeConverterFactory by calling AddConverter.");
 }