This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathReadBuilder.cs
More file actions
92 lines (84 loc) · 3.35 KB
/
ReadBuilder.cs
File metadata and controls
92 lines (84 loc) · 3.35 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis;
namespace NuGet.Insights
{
public class ReadBuilder : IPropertyVisitor
{
private readonly StringBuilder _builder;
private readonly int _indent;
public ReadBuilder(int indent)
{
_indent = indent;
_builder = new StringBuilder();
}
public void OnProperty(SourceProductionContext context, CsvRecordModel model, CsvPropertyModel property)
{
if (_builder.Length > 0)
{
_builder.AppendLine();
}
_builder.Append(' ', _indent);
switch (property.Type)
{
case "bool":
case "ushort":
case "short":
case "uint":
case "int":
case "float":
case "double":
case "long":
case "System.Guid":
case "System.TimeSpan":
_builder.AppendFormat("{0} = {1}.Parse(getNextField()),", property.Name, property.PrettyType);
break;
case "bool?":
case "ushort?":
case "short?":
case "uint?":
case "int?":
case "float?":
case "double?":
case "long?":
case "System.Guid?":
case "System.TimeSpan?":
_builder.AppendFormat("{0} = CsvUtility.ParseNullable(getNextField(), {1}.Parse),", property.Name, property.PrettyType);
break;
case "System.Version":
_builder.AppendFormat("{0} = CsvUtility.ParseReference(getNextField(), {1}.Parse),", property.Name, property.PrettyType);
break;
case "string":
_builder.AppendFormat("{0} = getNextField(),", property.Name);
break;
case "System.DateTimeOffset":
_builder.AppendFormat("{0} = CsvUtility.ParseDateTimeOffset(getNextField()),", property.Name);
break;
case "System.DateTimeOffset?":
_builder.AppendFormat("{0} = CsvUtility.ParseNullable(getNextField(), CsvUtility.ParseDateTimeOffset),", property.Name);
break;
default:
if (property.IsEnum)
{
_builder.AppendFormat("{0} = Enum.Parse<{1}>(getNextField()),", property.Name, property.PrettyType);
}
else if (property.IsNullableEnum)
{
_builder.AppendFormat("{0} = CsvUtility.ParseNullable(getNextField(), Enum.Parse<{1}>),", property.Name, property.PrettyType);
}
else
{
_builder.AppendFormat("{0} = Parse{0}(getNextField()),", property.Name);
}
break;
}
}
public void Finish(SourceProductionContext context, CsvRecordModel model)
{
}
public string GetResult()
{
return _builder.ToString();
}
}
}