|
4 | 4 | using System.Linq;
|
5 | 5 | using System.Reflection;
|
6 | 6 | using System.Text;
|
| 7 | +using System.Text.RegularExpressions; |
7 | 8 | using System.Threading.Tasks;
|
8 | 9 |
|
9 | 10 | namespace KustoCopyConsole.Entity.RowItems
|
10 | 11 | {
|
11 | 12 | internal class RowItemSerializer
|
12 | 13 | {
|
| 14 | + private static readonly Regex _csvRegex = new Regex( |
| 15 | + @"(?<=^|,)(?:""((?:[^""]|"""")*)""|([^,\r\n]*))", |
| 16 | + RegexOptions.Compiled); |
| 17 | + |
13 | 18 | private readonly IImmutableDictionary<RowType, Func<RowItemBase>> _factoryIndex;
|
14 | 19 | private readonly IImmutableDictionary<Type, RowType> _typeIndex;
|
15 | 20 | private readonly IImmutableDictionary<Type, IImmutableList<PropertyInfo>>
|
@@ -110,10 +115,67 @@ private static void WriteEnum<T>(TextWriter writer, T enumValue)
|
110 | 115 | }
|
111 | 116 | #endregion
|
112 | 117 |
|
| 118 | + #region Deserialize |
113 | 119 | public RowItemBase? Deserialize(TextReader reader)
|
114 | 120 | {
|
115 |
| - throw new NotImplementedException(); |
| 121 | + var line = reader.ReadLine(); |
| 122 | + |
| 123 | + if (line != null) |
| 124 | + { |
| 125 | + var matches = _csvRegex |
| 126 | + .Matches(line) |
| 127 | + .Select(m => m.Value) |
| 128 | + .ToImmutableArray(); |
| 129 | + |
| 130 | + if (matches.Length == 1) |
| 131 | + { |
| 132 | + throw new InvalidDataException($"Impossible to parse CSV line: '{line}'"); |
| 133 | + } |
| 134 | + if (matches.Length > 1) |
| 135 | + { |
| 136 | + var rowType = Enum.Parse<RowType>(matches.First()); |
| 137 | + var rowItem = _factoryIndex[rowType](); |
| 138 | + var properties = _typeToPropertyMap[rowItem.GetType()]; |
| 139 | + var pairs = matches.Skip(1).Zip(properties, (m, p) => new |
| 140 | + { |
| 141 | + Text = m, |
| 142 | + Property = p |
| 143 | + }); |
| 144 | + |
| 145 | + if (matches.Length - 1 != properties.Count) |
| 146 | + { |
| 147 | + throw new InvalidDataException( |
| 148 | + $"Line with wrong number of columns: '{line}'"); |
| 149 | + } |
| 150 | + foreach (var pair in pairs) |
| 151 | + { |
| 152 | + if (pair.Property.PropertyType == typeof(string)) |
| 153 | + { |
| 154 | + pair.Property.SetValue(rowItem, pair.Text); |
| 155 | + } |
| 156 | + else if (pair.Property.PropertyType == typeof(DateTime)) |
| 157 | + { |
| 158 | + pair.Property.SetValue(rowItem, DateTime.Parse(pair.Text)); |
| 159 | + } |
| 160 | + else if (pair.Property.PropertyType == typeof(Version)) |
| 161 | + { |
| 162 | + pair.Property.SetValue(rowItem, Version.Parse(pair.Text)); |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + throw new NotSupportedException( |
| 167 | + $"Type of property not supported for deserialization: " + |
| 168 | + $"'{pair.Property.PropertyType.Name}'"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return rowItem; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return null; |
116 | 177 | }
|
| 178 | + #endregion |
117 | 179 |
|
118 | 180 | private static IEnumerable<PropertyInfo> OrderByParent(
|
119 | 181 | Type parentType,
|
|
0 commit comments