-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace MasterMemory; | ||
|
||
internal sealed class DiagnosticReporter : IEquatable<DiagnosticReporter> | ||
{ | ||
List<Diagnostic>? diagnostics; | ||
|
||
public bool HasDiagnostics => diagnostics != null && diagnostics.Count != 0; | ||
|
||
public void ReportDiagnostic(DiagnosticDescriptor diagnosticDescriptor, Location location, params object?[]? messageArgs) | ||
{ | ||
var diagnostic = Diagnostic.Create(diagnosticDescriptor, location, messageArgs); | ||
if (diagnostics == null) | ||
{ | ||
diagnostics = new(); | ||
} | ||
diagnostics.Add(diagnostic); | ||
} | ||
|
||
public void ReportToContext(SourceProductionContext context) | ||
{ | ||
if (diagnostics != null) | ||
{ | ||
foreach (var item in diagnostics) | ||
{ | ||
context.ReportDiagnostic(item); | ||
} | ||
} | ||
} | ||
|
||
public bool Equals(DiagnosticReporter other) | ||
{ | ||
// if error, always false and otherwise ignore | ||
if (diagnostics == null && other.diagnostics == null) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
internal static class DiagnosticDescriptors | ||
{ | ||
const string Category = "GenerateMasterMemory"; | ||
|
||
public static void ReportDiagnostic(this SourceProductionContext context, DiagnosticDescriptor diagnosticDescriptor, Location location, params object?[]? messageArgs) | ||
{ | ||
var diagnostic = Diagnostic.Create(diagnosticDescriptor, location, messageArgs); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
|
||
public static DiagnosticDescriptor Create(int id, string message) | ||
{ | ||
return Create(id, message, message); | ||
} | ||
|
||
public static DiagnosticDescriptor Create(int id, string title, string messageFormat) | ||
{ | ||
return new DiagnosticDescriptor( | ||
id: "MAM" + id.ToString("000"), | ||
title: title, | ||
messageFormat: messageFormat, | ||
category: Category, | ||
defaultSeverity: DiagnosticSeverity.Error, | ||
isEnabledByDefault: true); | ||
} | ||
|
||
public static DiagnosticDescriptor RequirePrimaryKey { get; } = Create( | ||
1, | ||
"MemoryTable does not found PrimaryKey property, Type:{0}."); | ||
|
||
public static DiagnosticDescriptor DuplicatePrimaryKey { get; } = Create( | ||
2, | ||
"Duplicate PrimaryKey:{0}.{1}"); | ||
|
||
public static DiagnosticDescriptor DuplicateSecondaryKey { get; } = Create( | ||
3, | ||
"Duplicate SecondaryKey, doesn't allow to add multiple attribute in same attribute list:{0}.{1}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
tests/MasterMemory.SourceGenerator.Tests/DiagnosticsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MasterMemory.SourceGenerator.Tests; | ||
|
||
public class DiagnosticsTest(ITestOutputHelper outputHelper) : TestBase(outputHelper) | ||
{ | ||
[Fact] | ||
public void RequirePrimaryKey() | ||
{ | ||
Helper.Verify(1, """ | ||
[MemoryTable("item")] | ||
public class Item | ||
{ | ||
// [PrimaryKey] // No PrimaryKey | ||
public int ItemId { get; set; } | ||
} | ||
""", "Item"); | ||
} | ||
|
||
[Fact] | ||
public void DuplicateSecondaryKey() | ||
{ | ||
Helper.Verify(3, """ | ||
[MemoryTable("item")] | ||
public class Item | ||
{ | ||
[PrimaryKey] | ||
public int ItemId1 { get; set; } | ||
[SecondaryKey(0), SecondaryKey(1)] | ||
public int ItemId2 { get; set; } | ||
} | ||
""", "ItemId2"); | ||
} | ||
} |