summaryrefslogtreecommitdiff
path: root/ThirdParty/CsvHelper-master/src/CsvHelper/TypeConversion/TypeConverterOptions.cs
blob: 4a8e51f3afc4aed969615e92053b170819f23d29 (plain)
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
// 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
{
	/// <summary>
	/// Options used when doing type conversion.
	/// </summary>
	public class TypeConverterOptions
	{
		private static readonly string[] defaultBooleanTrueValues = { };
		private static readonly string[] defaultBooleanFalseValues = { };
		private static readonly string[] defaultNullValues = { };

		/// <summary>
		/// Gets or sets the culture info.
		/// </summary>
		public CultureInfo CultureInfo { get; set; }

		/// <summary>
		/// Gets or sets the date time style.
		/// </summary>
		public DateTimeStyles? DateTimeStyle { get; set; }

		/// <summary>
		/// Gets or sets the time span style.
		/// </summary>
		public TimeSpanStyles? TimeSpanStyle { get; set; }

		/// <summary>
		/// Gets or sets the number style.
		/// </summary>
		public NumberStyles? NumberStyles { get; set; }

		/// <summary>
		/// Gets or sets the string format.
		/// </summary>
		public string[] Formats { get; set; }

		/// <summary>
		/// Gets or sets the <see cref="UriKind"/>.
		/// </summary>
		public UriKind? UriKind { get; set; }

		/// <summary>
		/// Ingore case when parsing enums. Default is false.
		/// </summary>
		public bool? EnumIgnoreCase { get; set; }

		/// <summary>
		/// Gets the list of values that can be
		/// used to represent a boolean of true.
		/// </summary>
		public List<string> BooleanTrueValues { get; } = new List<string>(defaultBooleanTrueValues);

		/// <summary>
		/// Gets the list of values that can be
		/// used to represent a boolean of false.
		/// </summary>
		public List<string> BooleanFalseValues { get; } = new List<string>(defaultBooleanFalseValues);

		/// <summary>
		/// Gets the list of values that can be used to represent a null value.
		/// </summary>
		public List<string> NullValues { get; } = new List<string>(defaultNullValues);

		/// <summary>
		/// Merges TypeConverterOptions by applying the values of sources in order on to each other.
		/// The first object is the source object.
		/// </summary>
		/// <param name="sources">The sources that will be applied.</param>
		/// <returns>The updated source object.</returns>
		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;
		}
	}
}