// Copyright 2009-2022 Josh Close // This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0. // See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0. // https://github.com/JoshClose/CsvHelper using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace CsvHelper.TypeConversion { /// /// Options used when doing type conversion. /// public class TypeConverterOptions { private static readonly string[] defaultBooleanTrueValues = { }; private static readonly string[] defaultBooleanFalseValues = { }; private static readonly string[] defaultNullValues = { }; /// /// Gets or sets the culture info. /// public CultureInfo CultureInfo { get; set; } /// /// Gets or sets the date time style. /// public DateTimeStyles? DateTimeStyle { get; set; } /// /// Gets or sets the time span style. /// public TimeSpanStyles? TimeSpanStyle { get; set; } /// /// Gets or sets the number style. /// public NumberStyles? NumberStyles { get; set; } /// /// Gets or sets the string format. /// public string[] Formats { get; set; } /// /// Gets or sets the . /// public UriKind? UriKind { get; set; } /// /// Ingore case when parsing enums. Default is false. /// public bool? EnumIgnoreCase { get; set; } /// /// Gets the list of values that can be /// used to represent a boolean of true. /// public List BooleanTrueValues { get; } = new List(defaultBooleanTrueValues); /// /// Gets the list of values that can be /// used to represent a boolean of false. /// public List BooleanFalseValues { get; } = new List(defaultBooleanFalseValues); /// /// Gets the list of values that can be used to represent a null value. /// public List NullValues { get; } = new List(defaultNullValues); /// /// Merges TypeConverterOptions by applying the values of sources in order on to each other. /// The first object is the source object. /// /// The sources that will be applied. /// The updated source object. public static TypeConverterOptions? Merge(params TypeConverterOptions[] sources) { if (sources == null || sources.Length == 0) { return null; } var options = sources[0]; for (var i = 1; i < sources.Length; i++) { var source = sources[i]; if (source == null) { continue; } if (source.CultureInfo != null) { options.CultureInfo = source.CultureInfo; } if (source.DateTimeStyle != null) { options.DateTimeStyle = source.DateTimeStyle; } if (source.TimeSpanStyle != null) { options.TimeSpanStyle = source.TimeSpanStyle; } if (source.NumberStyles != null) { options.NumberStyles = source.NumberStyles; } if (source.Formats != null) { options.Formats = source.Formats; } if (source.UriKind != null) { options.UriKind = source.UriKind; } if (source.EnumIgnoreCase != null) { options.EnumIgnoreCase = source.EnumIgnoreCase; } // Only change the values if they are different than the defaults. // This means there were explicit changes made to the options. if (!defaultBooleanTrueValues.SequenceEqual(source.BooleanTrueValues)) { options.BooleanTrueValues.Clear(); options.BooleanTrueValues.AddRange(source.BooleanTrueValues); } if (!defaultBooleanFalseValues.SequenceEqual(source.BooleanFalseValues)) { options.BooleanFalseValues.Clear(); options.BooleanFalseValues.AddRange(source.BooleanFalseValues); } if (!defaultNullValues.SequenceEqual(source.NullValues)) { options.NullValues.Clear(); options.NullValues.AddRange(source.NullValues); } } return options; } } }