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 pathGetBucketKeyProperty.cs
More file actions
105 lines (89 loc) · 3.72 KB
/
GetBucketKeyProperty.cs
File metadata and controls
105 lines (89 loc) · 3.72 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
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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;
#nullable enable
namespace NuGet.Insights
{
public class GetBucketKeyProperty : IPropertyVisitor
{
private const string AttributeName = "BucketKeyAttribute";
private const string MethodName = "GetBucketKey";
private readonly int _indent;
private string? _bucketKeyName;
public GetBucketKeyProperty(int indent)
{
_indent = indent;
}
public void OnProperty(SourceProductionContext context, CsvRecordModel model, CsvPropertyModel property)
{
if (!property.IsBucketKey)
{
return;
}
if (property.PrettyType != "string")
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: DiagnosticIds.BucketKeyNotString,
title: $"The property marked as {AttributeName} must be a string.",
messageFormat: $"The property marked as {AttributeName} must be a string.",
Constants.Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true),
property.Locations.FirstOrDefault() ?? Location.None));
return;
}
if (_bucketKeyName is not null)
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: DiagnosticIds.MultipleBucketKeys,
title: $"Multiple {AttributeName} attributes were defined on a single type.",
messageFormat: $"Multiple {AttributeName} attributes were defined on a single type.",
Constants.Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true),
property.Locations.FirstOrDefault() ?? Location.None));
return;
}
_bucketKeyName = property.Name;
}
public void Finish(SourceProductionContext context, CsvRecordModel model)
{
if (_bucketKeyName is null)
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
id: DiagnosticIds.NoBucketKeyDefined,
title: $"No {AttributeName} attribute was defined on the type.",
messageFormat: $"No {AttributeName} attribute was defined on the type.",
Constants.Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true),
model.Locations.FirstOrDefault() ?? Location.None));
}
}
public string GetResult()
{
if (_bucketKeyName is null)
{
return string.Empty;
}
var builder = new StringBuilder();
builder.AppendLine();
builder.AppendLine();
builder.Append(' ', _indent);
builder.AppendFormat("public string {0}()", MethodName);
builder.AppendLine();
builder.Append(' ', _indent);
builder.Append("{");
builder.AppendLine();
builder.Append(' ', _indent);
builder.AppendFormat(" return {0};", _bucketKeyName);
builder.AppendLine();
builder.Append(' ', _indent);
builder.Append("}");
return builder.ToString();
}
}
}