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 pathKustoMappingBuilder.cs
More file actions
87 lines (71 loc) · 2.33 KB
/
KustoMappingBuilder.cs
File metadata and controls
87 lines (71 loc) · 2.33 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 KustoMappingBuilder : IPropertyVisitor
{
private readonly int _indent;
private readonly bool _escapeQuotes;
private readonly StringBuilder _builder;
private int _nextOrdinal;
public KustoMappingBuilder(int indent, bool escapeQuotes)
{
_indent = indent;
_escapeQuotes = escapeQuotes;
_builder = new StringBuilder();
_nextOrdinal = 0;
}
public void OnProperty(SourceProductionContext context, CsvRecordModel model, CsvPropertyModel property)
{
var field = new DataMapping
{
Column = property.Name,
DataType = PropertyHelper.GetKustoDataType(property),
Properties = new CsvProperties
{
Ordinal = _nextOrdinal,
}
};
_nextOrdinal++;
if (property.IsKustoIgnore)
{
return;
}
if (_builder.Length > 1)
{
_builder.Append(",'");
_builder.AppendLine();
}
_builder.Append(' ', _indent);
_builder.Append("'");
var json = JsonSerializer.Serialize(field).Replace("'", "\\'");
if (_escapeQuotes)
{
json = json.Replace("\"", "\"\"");
}
_builder.Append(json);
}
public void Finish(SourceProductionContext context, CsvRecordModel model)
{
_builder.Append("'");
}
public string GetResult()
{
return _builder.ToString();
}
/// <summary>
/// Source: https://docs.microsoft.com/en-us/azure/data-explorer/kusto/management/mappings
/// </summary>
private class DataMapping
{
public string Column { get; set; }
public string DataType { get; set; }
public CsvProperties Properties { get; set; }
}
private class CsvProperties
{
public int Ordinal { get; set; }
}
}
}