Skip to content

Commit 9b35cc5

Browse files
committed
.
1 parent 3700339 commit 9b35cc5

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

code/KustoCopyConsole/Entity/RowItems/RowItemSerializer.cs

+63-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Text;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89

910
namespace KustoCopyConsole.Entity.RowItems
1011
{
1112
internal class RowItemSerializer
1213
{
14+
private static readonly Regex _csvRegex = new Regex(
15+
@"(?<=^|,)(?:""((?:[^""]|"""")*)""|([^,\r\n]*))",
16+
RegexOptions.Compiled);
17+
1318
private readonly IImmutableDictionary<RowType, Func<RowItemBase>> _factoryIndex;
1419
private readonly IImmutableDictionary<Type, RowType> _typeIndex;
1520
private readonly IImmutableDictionary<Type, IImmutableList<PropertyInfo>>
@@ -110,10 +115,67 @@ private static void WriteEnum<T>(TextWriter writer, T enumValue)
110115
}
111116
#endregion
112117

118+
#region Deserialize
113119
public RowItemBase? Deserialize(TextReader reader)
114120
{
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;
116177
}
178+
#endregion
117179

118180
private static IEnumerable<PropertyInfo> OrderByParent(
119181
Type parentType,

code/KustoCopyConsole/Storage/RowItemGateway.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private static IEnumerable<RowItemBase> DeserializeBuffer(byte[] readBuffer)
100100
{
101101
throw new InvalidDataException("First row is expected to be a version row");
102102
}
103-
if (version.FileVersion == CURRENT_FILE_VERSION)
103+
if (version.FileVersion != CURRENT_FILE_VERSION)
104104
{
105105
throw new NotSupportedException(
106106
$"Only support version is {CURRENT_FILE_VERSION}");

0 commit comments

Comments
 (0)