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 pathWriteTextWriterBuilder.cs
More file actions
87 lines (79 loc) · 2.95 KB
/
WriteTextWriterBuilder.cs
File metadata and controls
87 lines (79 loc) · 2.95 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
// 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 WriteTextWriterBuilder : IPropertyVisitor
{
private readonly int _indent;
private readonly StringBuilder _builder;
public WriteTextWriterBuilder(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);
_builder.AppendLine("writer.Write(',');");
}
_builder.Append(' ', _indent);
switch (property.Type)
{
case "ushort":
case "ushort?":
case "uint":
case "uint?":
case "int":
case "int?":
case "float":
case "float?":
case "double":
case "double?":
case "long":
case "long?":
case "System.Guid":
case "System.Guid?":
case "System.TimeSpan":
case "System.TimeSpan?":
case "System.Version":
_builder.AppendFormat("writer.Write({0});", property.Name);
break;
case "bool":
case "bool?":
_builder.AppendFormat("writer.Write(CsvUtility.FormatBool({0}));", property.Name);
break;
case "System.DateTimeOffset":
case "System.DateTimeOffset?":
_builder.AppendFormat("writer.Write(CsvUtility.FormatDateTimeOffset({0}));", property.Name);
break;
case "string":
_builder.AppendFormat("CsvUtility.WriteWithQuotes(writer, {0});", property.Name);
break;
default:
if (property.IsEnum || property.IsNullableEnum)
{
_builder.AppendFormat("CsvUtility.WriteWithQuotes(writer, {0}.ToString());", property.Name);
}
else
{
_builder.AppendFormat("CsvUtility.WriteWithQuotes(writer, {0}?.ToString());", property.Name);
}
break;
}
}
public void Finish(SourceProductionContext context, CsvRecordModel model)
{
_builder.AppendLine();
_builder.Append(' ', _indent);
_builder.Append("writer.WriteLine();");
}
public string GetResult()
{
return _builder.ToString();
}
}
}