From 1c62ee0d37f74fbb2d4e54c78bac336f0bc12f04 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Tue, 18 Feb 2025 21:21:50 +0300 Subject: [PATCH 01/42] fix report format --- .../MetadataReportsGenerator.cs | 15 +++++++++------ .../GoldenReports/Basic.json | 8 ++++++-- .../GoldenReports/Inheritance.json | 8 ++++++-- .../GoldenReports/LogMethod.json | 8 ++++++-- .../MeterAttributedWithXmlDescriptions.json | 8 ++++++-- ...ributedWithXmlDescriptions_RecordProperty.json | 10 +++++++--- ...erDimensionsAttributedWithXmlDescriptions.json | 8 ++++++-- ...nsAttributedWithXmlDescriptions_LogMethod.json | 10 +++++++--- .../GoldenReports/RecordProperty.json | 8 ++++++-- 9 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 8471e9d1cc3..34749bd8131 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -96,13 +96,16 @@ public void Execute(GeneratorExecutionContext context) metadataReport.complianceReport = HandleComplianceReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); StringBuilder reportStringBuilder = new StringBuilder() - .Append("{ \"Name\": \"") - .Append(context.Compilation.AssemblyName!) - .Append("\", \"ComplianceReport\": ") + .Append("{\r\n") + .Append("\"Name\":") + .Append($"\"{context.Compilation.AssemblyName!}\"") + .Append(",\r\n") + .Append("\"ComplianceReport\": ") .Append((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)) - .Append(" ,") - .Append(" \"MetricReport\": ") - .Append((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport) + " }"); + .Append(",\r\n") + .Append("\"MetricReport\": ") + .Append((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)) + .Append("\r\n}\r\n"); #pragma warning disable RS1035 // Do not use APIs banned for analyzers File.WriteAllText(Path.Combine(path, _fileName), reportStringBuilder.ToString(), Encoding.UTF8); diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json index 2e9867dc658..83c41b2bc8a 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json @@ -1,4 +1,6 @@ -{ "Name": "test.dll", "ComplianceReport": +{ +"Name":"test.dll", +"ComplianceReport": { "Types": [ { @@ -115,4 +117,6 @@ ] } ] -} , "MetricReport": [] } \ No newline at end of file +}, +"MetricReport": [] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json index 084f52598d5..77cb62c90e1 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json @@ -1,4 +1,6 @@ -{ "Name": "test.dll", "ComplianceReport": +{ +"Name":"test.dll", +"ComplianceReport": { "Types": [ { @@ -59,4 +61,6 @@ ] } ] -} , "MetricReport": [] } \ No newline at end of file +}, +"MetricReport": [] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json index 8f14aa9c92f..576539b0e3e 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json @@ -1,4 +1,6 @@ -{ "Name": "test.dll", "ComplianceReport": +{ +"Name":"test.dll", +"ComplianceReport": { "Types": [ { @@ -30,4 +32,6 @@ ] } ] -} , "MetricReport": [] } \ No newline at end of file +}, +"MetricReport": [] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index c89592c2bf7..15da84dbedf 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,4 +1,7 @@ -{ "Name": "test.dll", "ComplianceReport": {} , "MetricReport": [ +{ +"Name":"test.dll", +"ComplianceReport": {}, +"MetricReport": [ { "TestClasses": [ @@ -28,4 +31,5 @@ } ] } -] } \ No newline at end of file +] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 270a4b1d979..8de54a2991f 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -1,4 +1,6 @@ -{ "Name": "test.dll", "ComplianceReport": +{ +"Name":"test.dll", +"ComplianceReport": { "Types": [ { @@ -197,7 +199,8 @@ ] } ] -} , "MetricReport": [ +}, +"MetricReport": [ { "TestClasses": [ @@ -227,4 +230,5 @@ } ] } -] } \ No newline at end of file +] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 3c58e7c0670..d844aa385e5 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,4 +1,7 @@ -{ "Name": "test.dll", "ComplianceReport": {} , "MetricReport": [ +{ +"Name":"test.dll", +"ComplianceReport": {}, +"MetricReport": [ { "TestClasses": [ @@ -36,4 +39,5 @@ } ] } -] } \ No newline at end of file +] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 021cf13caa6..a7f867e8936 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -1,4 +1,6 @@ -{ "Name": "test.dll", "ComplianceReport": +{ +"Name":"test.dll", +"ComplianceReport": { "Types": [ { @@ -30,7 +32,8 @@ ] } ] -} , "MetricReport": [ +}, +"MetricReport": [ { "TestClasses": [ @@ -68,4 +71,5 @@ } ] } -] } \ No newline at end of file +] +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json index 7e78ce5d2ad..4055d5d54fc 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json @@ -1,4 +1,6 @@ -{ "Name": "test.dll", "ComplianceReport": +{ +"Name":"test.dll", +"ComplianceReport": { "Types": [ { @@ -197,4 +199,6 @@ ] } ] -} , "MetricReport": [] } \ No newline at end of file +}, +"MetricReport": [] +} From e4ccb4805088f211e10c7b51fbab209b9c1a6aa9 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 08:57:08 +0300 Subject: [PATCH 02/42] adding normalize escapes in order to handle differences between windows and linux in tests --- .../MetadataReportsGenerator.cs | 8 ++++---- .../Unit/GeneratorTests.cs | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 34749bd8131..e29b602d557 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -96,16 +96,16 @@ public void Execute(GeneratorExecutionContext context) metadataReport.complianceReport = HandleComplianceReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); StringBuilder reportStringBuilder = new StringBuilder() - .Append("{\r\n") + .Append("{\n") .Append("\"Name\":") .Append($"\"{context.Compilation.AssemblyName!}\"") - .Append(",\r\n") + .Append(",\n") .Append("\"ComplianceReport\": ") .Append((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)) - .Append(",\r\n") + .Append(",\n") .Append("\"MetricReport\": ") .Append((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)) - .Append("\r\n}\r\n"); + .Append("\n}\n"); #pragma warning disable RS1035 // Do not use APIs banned for analyzers File.WriteAllText(Path.Combine(path, _fileName), reportStringBuilder.ToString(), Encoding.UTF8); diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs index 5e7c749d426..ce8e8cb3440 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -87,8 +88,8 @@ public async Task TestAll(bool useExplicitReportPath) var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); Assert.Empty(d); - var golden = await File.ReadAllTextAsync(goldenReportPath); - var generated = await File.ReadAllTextAsync(generatedReportPath); + var golden = await File.ReadAllTextAsync(goldenReportPath, Encoding.UTF8); + var generated = await File.ReadAllTextAsync(generatedReportPath, Encoding.UTF8); if (golden != generated) { @@ -104,7 +105,7 @@ public async Task TestAll(bool useExplicitReportPath) File.Delete(generatedReportPath); - Assert.Equal(golden, generated); + Assert.Equal(NormalizeEscapes(golden), NormalizeEscapes(generated)); } else { @@ -256,4 +257,5 @@ private sealed class OptionsProvider(Dictionary? analyzerOptions public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => throw new NotSupportedException(); public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => throw new NotSupportedException(); } + private string NormalizeEscapes(string input) => input.Replace("\r\n", "\n").Replace("\r", "\n"); } From 16be34f4958fd52e76f698ca7e1615b109ece19f Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 08:58:09 +0300 Subject: [PATCH 03/42] remove unneeded Encoding.UTF8 --- .../Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs index bd5b529bdbf..db65513eb27 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs @@ -88,8 +88,8 @@ public async Task TestAll(bool useExplicitReportPath) var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); Assert.Empty(d); - var golden = await File.ReadAllTextAsync(goldenReportPath, Encoding.UTF8); - var generated = await File.ReadAllTextAsync(generatedReportPath, Encoding.UTF8); + var golden = await File.ReadAllTextAsync(goldenReportPath); + var generated = await File.ReadAllTextAsync(generatedReportPath); if (golden != generated) { From caf50682db654500269e94265bce0dfdf284c475 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 09:16:23 +0300 Subject: [PATCH 04/42] adding comment to make azure piplines happy --- .../Unit/GeneratorTests.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs index db65513eb27..91bdf28ef7e 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs @@ -7,7 +7,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -257,5 +256,10 @@ private sealed class OptionsProvider(Dictionary? analyzerOptions public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => throw new NotSupportedException(); public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => throw new NotSupportedException(); } - private string NormalizeEscapes(string input) => input.Replace("\r\n", "\n").Replace("\r", "\n"); + + /// + /// Standardizes line endings by replacing \r\n with \n across different operating systems. + /// + private static string NormalizeEscapes(string input) => input.Replace("\r\n", "\n").Replace("\r", "\n"); + } From 496f9f180e77e96a82fcbf889cf0a617d21cbecd Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 12:22:41 +0300 Subject: [PATCH 05/42] use temp file isnted of static ReportFileName for metadata --- .../MetadataReportsGenerator.cs | 38 ++++++++++++------- .../MetricsReportsGenerator.cs | 28 +++++++++----- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index be3b72ad053..27210604ece 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -23,22 +23,30 @@ public sealed class MetadataReportsGenerator : ISourceGenerator private const string RootNamespace = "build_property.rootnamespace"; private const string FallbackFileName = "MetadataReport.json"; private readonly string _fileName; - + private string? _directory; /// /// Initializes a new instance of the class. /// public MetadataReportsGenerator() - : this(FallbackFileName) + : this(null) { } /// /// Initializes a new instance of the class. /// - /// The report file name. - public MetadataReportsGenerator(string reportFileName) + /// The report file name. + public MetadataReportsGenerator(string? filePath) { - _fileName = reportFileName; + if (filePath is not null) + { + _directory = Path.GetDirectoryName(filePath); + _fileName = Path.GetFileName(filePath); + } + else + { + _fileName = FallbackFileName; + } } /// @@ -72,10 +80,12 @@ public void Execute(GeneratorExecutionContext context) } var options = context.AnalyzerConfigOptions.GlobalOptions; - var path = GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPathMSBuildProperty, out var reportOutputPath) + _directory ??= GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPathMSBuildProperty, out var reportOutputPath) ? reportOutputPath! : GeneratorUtilities.GetDefaultReportOutputPath(options); - if (string.IsNullOrWhiteSpace(path)) + + + if (string.IsNullOrWhiteSpace(_directory)) { // Report diagnostic: var diagnostic = new DiagnosticDescriptor( @@ -96,21 +106,21 @@ public void Execute(GeneratorExecutionContext context) metadataReport.complianceReport = HandleComplianceReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); StringBuilder reportStringBuilder = new StringBuilder() - .Append("{\n") + .AppendLine("{") .Append("\"Name\":") .Append($"\"{context.Compilation.AssemblyName!}\"") - .Append(",\n") + .AppendLine(",") .Append("\"ComplianceReport\": ") .Append((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)) - .Append(",\n") + .AppendLine(",") .Append("\"MetricReport\": ") - .Append((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)) - .Append("\n}\n"); + .AppendLine((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)) + .AppendLine("}"); #pragma warning disable RS1035 // Do not use APIs banned for analyzers - _ = Directory.CreateDirectory(path); + _ = Directory.CreateDirectory(_directory); - File.WriteAllText(Path.Combine(path, _fileName), reportStringBuilder.ToString(), Encoding.UTF8); + File.WriteAllText(Path.Combine(_directory, _fileName), reportStringBuilder.ToString(), Encoding.UTF8); #pragma warning restore RS1035 // Do not use APIs banned for analyzers } diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs index 895db7ea5d5..ac156dfc1dd 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs @@ -16,17 +16,25 @@ public class MetricsReportsGenerator : ISourceGenerator private const string GenerateMetricDefinitionReport = "build_property.GenerateMetricsReport"; private const string RootNamespace = "build_property.rootnamespace"; private const string ReportOutputPath = "build_property.MetricsReportOutputPath"; - private const string FileName = "MetricsReport.json"; + private const string FallbackFileName = "MetricsReport.json"; private readonly string _fileName; - + private string? _directory; public MetricsReportsGenerator() - : this(FileName) + : this(null) { } - internal MetricsReportsGenerator(string reportFileName) + internal MetricsReportsGenerator(string? filePath) { - _fileName = reportFileName; + if (filePath is not null) + { + _directory = Path.GetDirectoryName(filePath); + _fileName = Path.GetFileName(filePath); + } + else + { + _fileName = FallbackFileName; + } } public void Initialize(GeneratorInitializationContext context) @@ -44,13 +52,13 @@ public void Execute(GeneratorExecutionContext context) return; } - var options = context.AnalyzerConfigOptions.GlobalOptions; - var path = GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPath, out var reportOutputPath) + var options = context.AnalyzerConfigOptions.GlobalOptions; + _directory ??= GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPath, out var reportOutputPath) ? reportOutputPath! : GeneratorUtilities.GetDefaultReportOutputPath(options); - if (string.IsNullOrWhiteSpace(path)) + if (string.IsNullOrWhiteSpace(_directory)) { // Report diagnostic: var diagnostic = new DiagnosticDescriptor( @@ -81,9 +89,9 @@ public void Execute(GeneratorExecutionContext context) // Suppressing until this issue is addressed in https://github.com/dotnet/extensions/issues/5390 #pragma warning disable RS1035 // Do not use APIs banned for analyzers - _ = Directory.CreateDirectory(path); + _ = Directory.CreateDirectory(_directory); - File.WriteAllText(Path.Combine(path, _fileName), report, Encoding.UTF8); + File.WriteAllText(Path.Combine(_directory, _fileName), report, Encoding.UTF8); #pragma warning restore RS1035 // Do not use APIs banned for analyzers } } From d9049fcc9972f05cb23d2d169f4fbc0f529f2f79 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 12:29:16 +0300 Subject: [PATCH 06/42] use temp file isnted of static ReportFileName for metrics report --- .../Unit/GeneratorTests.cs | 37 +++++++--------- .../Unit/GeneratorTests.cs | 42 ++++++++++--------- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs index 91bdf28ef7e..06711e6d4be 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs @@ -80,19 +80,18 @@ public async Task TestAll(bool useExplicitReportPath) var goldenFileName = Path.ChangeExtension(stem, ".json"); var goldenReportPath = Path.Combine("GoldenReports", goldenFileName); - var generatedReportPath = Path.Combine(Directory.GetCurrentDirectory(), ReportFilename); - if (File.Exists(goldenReportPath)) { - var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var tmp = Path.GetTempFileName(); + var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), tmp, options: options); Assert.Empty(d); var golden = await File.ReadAllTextAsync(goldenReportPath); - var generated = await File.ReadAllTextAsync(generatedReportPath); + var generated = await File.ReadAllTextAsync(tmp); if (golden != generated) { - output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {generatedReportPath}"); + output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {tmp}"); output.WriteLine("----"); output.WriteLine("golden:"); output.WriteLine(golden); @@ -102,7 +101,7 @@ public async Task TestAll(bool useExplicitReportPath) output.WriteLine("----"); } - File.Delete(generatedReportPath); + File.Delete(tmp); Assert.Equal(NormalizeEscapes(golden), NormalizeEscapes(generated)); } @@ -110,7 +109,7 @@ public async Task TestAll(bool useExplicitReportPath) { // generate the golden file if it doesn't already exist output.WriteLine($"Generating golden report: {goldenReportPath}"); - _ = await RunGenerator(await File.ReadAllTextAsync(inputFile), options, reportFileName: goldenFileName); + _ = await RunGenerator(await File.ReadAllTextAsync(inputFile), goldenFileName, options); } } } @@ -129,7 +128,7 @@ public async Task ShouldNot_Generate_WhenDisabledViaConfig() ["build_property.rootnamespace"] = "TestClasses" }; - var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options: options); Assert.Empty(d); Assert.False(File.Exists(Path.Combine(Path.GetTempPath(), ReportFilename))); } @@ -153,7 +152,7 @@ public async Task Should_EmitWarning_WhenPathUnavailable(bool isReportPathProvid options.Add("build_property.MetadataReportOutputPath", string.Empty); } - var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options: options); var diag = Assert.Single(diags); Assert.Equal("AUDREPGEN000", diag.Id); Assert.Equal(DiagnosticSeverity.Info, diag.Severity); @@ -179,7 +178,7 @@ public async Task Should_UseProjectDir_WhenOutputPathIsRelative() ["build_property.outputpath"] = outputPath }; - var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options: options); Assert.Empty(diags); Assert.True(File.Exists(Path.Combine(fullReportPath, ReportFilename))); } @@ -193,14 +192,14 @@ public async Task Should_UseProjectDir_WhenOutputPathIsRelative() /// Runs the generator on the given code. /// /// The coded that the generation will be based-on. - /// The analyzer options. + /// The output file. + /// The analyzer options. /// The cancellation Token. - /// The report file name. private static async Task> RunGenerator( string code, - Dictionary? analyzerOptions = null, - CancellationToken cancellationToken = default, - string? reportFileName = null) + string? outputFile = null, + Dictionary? options = null, + CancellationToken cancellationToken = default) { Assembly[] refs = [ @@ -213,15 +212,11 @@ private static async Task> RunGenerator( Assembly.GetAssembly(typeof(Extensions.Compliance.Classification.DataClassification))!, ]; - var generator = reportFileName is null - ? new MetadataReportsGenerator() - : new MetadataReportsGenerator(reportFileName); - var (d, _) = await RoslynTestUtils.RunGenerator( - generator, + new MetadataReportsGenerator(outputFile), refs, new[] { code, TestTaxonomy }, - new OptionsProvider(analyzerOptions), + new OptionsProvider(options), includeBaseReferences: true, cancellationToken: cancellationToken).ConfigureAwait(false); diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs index f432257b111..ac072519a6c 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs @@ -44,20 +44,18 @@ public async Task TestAll(bool useExplicitReportPath) var stem = Path.GetFileNameWithoutExtension(inputFile); var goldenFileName = Path.ChangeExtension(stem, ".json"); var goldenReportPath = Path.Combine("GoldenReports", goldenFileName); - - var generatedReportPath = Path.Combine(Directory.GetCurrentDirectory(), ReportFilename); - + var tmp = Path.GetTempFileName(); if (File.Exists(goldenReportPath)) { - var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), tmp, options); Assert.Empty(d); var golden = await File.ReadAllTextAsync(goldenReportPath); - var generated = await File.ReadAllTextAsync(generatedReportPath); + var generated = await File.ReadAllTextAsync(tmp); if (golden != generated) { - output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {generatedReportPath}"); + output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {tmp}"); output.WriteLine("----"); output.WriteLine("golden:"); output.WriteLine(golden); @@ -67,14 +65,14 @@ public async Task TestAll(bool useExplicitReportPath) output.WriteLine("----"); } - File.Delete(generatedReportPath); + File.Delete(tmp); Assert.Equal(golden, generated); } else { // generate the golden file if it doesn't already exist output.WriteLine($"Generating golden report: {goldenReportPath}"); - _ = await RunGenerator(await File.ReadAllTextAsync(inputFile), options, reportFileName: goldenFileName); + _ = await RunGenerator(await File.ReadAllTextAsync(inputFile), tmp, options); } } } @@ -89,7 +87,7 @@ public async Task ShouldNot_Generate_WhenDisabledViaConfig() ["build_property.MetricsReportOutputPath"] = Path.GetTempPath() }; - var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), options: options); Assert.Empty(d); Assert.False(File.Exists(Path.Combine(Path.GetTempPath(), ReportFilename))); } @@ -109,7 +107,7 @@ public async Task Should_EmitWarning_WhenPathUnavailable(bool isReportPathProvid options.Add("build_property.MetricsReportOutputPath", string.Empty); } - var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options: options); var diag = Assert.Single(diags); Assert.Equal("AUDREPGEN000", diag.Id); Assert.Equal(DiagnosticSeverity.Info, diag.Severity); @@ -132,7 +130,7 @@ public async Task Should_UseProjectDir_WhenOutputPathIsRelative() ["build_property.outputpath"] = outputPath }; - var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options); + var diags = await RunGenerator(await File.ReadAllTextAsync(inputFile), options: options); Assert.Empty(diags); Assert.True(File.Exists(Path.Combine(fullReportPath, ReportFilename))); } @@ -141,12 +139,18 @@ public async Task Should_UseProjectDir_WhenOutputPathIsRelative() Directory.Delete(fullReportPath, recursive: true); } } - + /// + /// Runs the generator on the given code. + /// + /// The coded that the generation will be based-on. + /// The output file. + /// The analyzer options. + /// The cancellation Token. private static async Task> RunGenerator( string code, - Dictionary? analyzerOptions = null, - CancellationToken cancellationToken = default, - string? reportFileName = null) + string? outputFile = null, + Dictionary? options = null, + CancellationToken cancellationToken = default) { Assembly[] refs = [ @@ -156,15 +160,13 @@ private static async Task> RunGenerator( Assembly.GetAssembly(typeof(GaugeAttribute))! ]; - var generator = reportFileName is null - ? new MetricsReportsGenerator() - : new MetricsReportsGenerator(reportFileName); + var (d, _) = await RoslynTestUtils.RunGenerator( - generator, + new MetricsReportsGenerator(outputFile), refs, new[] { code }, - new OptionsProvider(analyzerOptions), + new OptionsProvider(options), includeBaseReferences: true, cancellationToken: cancellationToken).ConfigureAwait(false); From e9f2cdd92facbc8f6f65350255559c228cc71e95 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 12:46:17 +0300 Subject: [PATCH 07/42] remove white space --- .../Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 27210604ece..850441cf4bf 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -84,7 +84,6 @@ public void Execute(GeneratorExecutionContext context) ? reportOutputPath! : GeneratorUtilities.GetDefaultReportOutputPath(options); - if (string.IsNullOrWhiteSpace(_directory)) { // Report diagnostic: From 82edd87afef127a63aa4ad058b04c49c9ed42a73 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 12:47:12 +0300 Subject: [PATCH 08/42] add space --- .../Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 850441cf4bf..13af788433c 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -24,6 +24,7 @@ public sealed class MetadataReportsGenerator : ISourceGenerator private const string FallbackFileName = "MetadataReport.json"; private readonly string _fileName; private string? _directory; + /// /// Initializes a new instance of the class. /// From 32bdb46d8f63d61c72b15978430ad328a695f4fa Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 13:01:43 +0300 Subject: [PATCH 09/42] remove multiple blanks --- .../Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs index ac156dfc1dd..6d9470acc60 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs @@ -52,7 +52,6 @@ public void Execute(GeneratorExecutionContext context) return; } - var options = context.AnalyzerConfigOptions.GlobalOptions; _directory ??= GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPath, out var reportOutputPath) ? reportOutputPath! From a6a520c217d49486f217d7f05a6e9a9183de1493 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Wed, 19 Feb 2025 13:15:55 +0300 Subject: [PATCH 10/42] fix checks --- .../Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs index ac072519a6c..065908bb4a1 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs @@ -139,6 +139,7 @@ public async Task Should_UseProjectDir_WhenOutputPathIsRelative() Directory.Delete(fullReportPath, recursive: true); } } + /// /// Runs the generator on the given code. /// @@ -160,8 +161,6 @@ private static async Task> RunGenerator( Assembly.GetAssembly(typeof(GaugeAttribute))! ]; - - var (d, _) = await RoslynTestUtils.RunGenerator( new MetricsReportsGenerator(outputFile), refs, From edfec34dd30e059e97498cd46ff8ef29b1a23492 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Thu, 20 Feb 2025 12:03:12 +0300 Subject: [PATCH 11/42] -adding parameter needs to parameterless constructors -fix spaces -fix documentation --- .../ComplianceReportsGenerator.cs | 9 ++++++++- .../MetadataReportsGenerator.cs | 4 ++-- .../MetricsReportsGenerator.cs | 10 +++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs index 8ff983b482c..454aa40fe3f 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs @@ -24,11 +24,18 @@ public sealed class ComplianceReportsGenerator : ISourceGenerator private readonly string _fileName; private string? _directory; + /// + /// Initializes a new instance of the class. + /// public ComplianceReportsGenerator() - : this(null) + : this(filePath: null) { } + /// + /// Initializes a new instance of the class. + /// + /// The report path and name. public ComplianceReportsGenerator(string? filePath) { if (filePath is not null) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 13af788433c..679b9784cc8 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -29,14 +29,14 @@ public sealed class MetadataReportsGenerator : ISourceGenerator /// Initializes a new instance of the class. /// public MetadataReportsGenerator() - : this(null) + : this(filePath: null) { } /// /// Initializes a new instance of the class. /// - /// The report file name. + /// The report path and name. public MetadataReportsGenerator(string? filePath) { if (filePath is not null) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs index 6d9470acc60..e35291b0010 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricsReportsGenerator.cs @@ -19,11 +19,19 @@ public class MetricsReportsGenerator : ISourceGenerator private const string FallbackFileName = "MetricsReport.json"; private readonly string _fileName; private string? _directory; + + /// + /// Initializes a new instance of the class. + /// public MetricsReportsGenerator() - : this(null) + : this(filePath: null) { } + /// + /// Initializes a new instance of the class. + /// + /// The report path and name. internal MetricsReportsGenerator(string? filePath) { if (filePath is not null) From 6d136e41116ba6637421dd38691d246819c2f966 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Thu, 20 Feb 2025 12:27:01 +0300 Subject: [PATCH 12/42] rename temp to temporaryReportFile & change var to actual type --- .../Unit/GeneratorTests.cs | 18 +++++++-------- .../Unit/GeneratorTests.cs | 20 ++++++++-------- .../Unit/GeneratorTests.cs | 23 ++++++++++--------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/test/Generators/Microsoft.Gen.ComplianceReports/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.ComplianceReports/Unit/GeneratorTests.cs index 28dbb80a36c..2a99a5ea4fe 100644 --- a/test/Generators/Microsoft.Gen.ComplianceReports/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.ComplianceReports/Unit/GeneratorTests.cs @@ -59,21 +59,21 @@ public async Task TestAll(bool useExplicitReportPath) foreach (var inputFile in Directory.GetFiles("TestClasses")) { - var stem = Path.GetFileNameWithoutExtension(inputFile); - var goldenReportFile = $"GoldenReports/{stem}.json"; + string stem = Path.GetFileNameWithoutExtension(inputFile); + string goldenReportFile = $"GoldenReports/{stem}.json"; if (File.Exists(goldenReportFile)) { - var tmp = Path.GetTempFileName(); - var d = await RunGenerator(File.ReadAllText(inputFile), tmp, options); - Assert.Empty(d); + string temporaryReportFile = Path.GetTempFileName(); + var diagnostic = await RunGenerator(File.ReadAllText(inputFile), temporaryReportFile, options); + Assert.Empty(diagnostic); - var golden = File.ReadAllText(goldenReportFile); - var generated = File.ReadAllText(tmp); + string golden = File.ReadAllText(goldenReportFile); + string generated = File.ReadAllText(temporaryReportFile); if (golden != generated) { - _output.WriteLine($"MISMATCH: golden report {goldenReportFile}, generated {tmp}"); + _output.WriteLine($"MISMATCH: golden report {goldenReportFile}, generated {temporaryReportFile}"); _output.WriteLine("----"); _output.WriteLine("golden:"); _output.WriteLine(golden); @@ -83,7 +83,7 @@ public async Task TestAll(bool useExplicitReportPath) _output.WriteLine("----"); } - File.Delete(tmp); + File.Delete(temporaryReportFile); Assert.Equal(golden, generated); } else diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs index 06711e6d4be..96227e493e6 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/Unit/GeneratorTests.cs @@ -74,24 +74,24 @@ public async Task TestAll(bool useExplicitReportPath) ? new() { ["build_property.MetadataReportOutputPath"] = Directory.GetCurrentDirectory() } : null; - foreach (var inputFile in Directory.GetFiles("TestClasses")) + foreach (string inputFile in Directory.GetFiles("TestClasses")) { - var stem = Path.GetFileNameWithoutExtension(inputFile); - var goldenFileName = Path.ChangeExtension(stem, ".json"); - var goldenReportPath = Path.Combine("GoldenReports", goldenFileName); + string stem = Path.GetFileNameWithoutExtension(inputFile); + string goldenFileName = Path.ChangeExtension(stem, ".json"); + string goldenReportPath = Path.Combine("GoldenReports", goldenFileName); if (File.Exists(goldenReportPath)) { - var tmp = Path.GetTempFileName(); - var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), tmp, options: options); - Assert.Empty(d); + string temporaryReportFile = Path.GetTempFileName(); + var diagnostic = await RunGenerator(await File.ReadAllTextAsync(inputFile), temporaryReportFile, options: options); + Assert.Empty(diagnostic); var golden = await File.ReadAllTextAsync(goldenReportPath); - var generated = await File.ReadAllTextAsync(tmp); + var generated = await File.ReadAllTextAsync(temporaryReportFile); if (golden != generated) { - output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {tmp}"); + output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {temporaryReportFile}"); output.WriteLine("----"); output.WriteLine("golden:"); output.WriteLine(golden); @@ -101,7 +101,7 @@ public async Task TestAll(bool useExplicitReportPath) output.WriteLine("----"); } - File.Delete(tmp); + File.Delete(temporaryReportFile); Assert.Equal(NormalizeEscapes(golden), NormalizeEscapes(generated)); } diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs index 065908bb4a1..b8fe17b6046 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/GeneratorTests.cs @@ -41,21 +41,22 @@ public async Task TestAll(bool useExplicitReportPath) foreach (var inputFile in Directory.GetFiles("TestClasses")) { - var stem = Path.GetFileNameWithoutExtension(inputFile); - var goldenFileName = Path.ChangeExtension(stem, ".json"); - var goldenReportPath = Path.Combine("GoldenReports", goldenFileName); - var tmp = Path.GetTempFileName(); + string stem = Path.GetFileNameWithoutExtension(inputFile); + string goldenFileName = Path.ChangeExtension(stem, ".json"); + string goldenReportPath = Path.Combine("GoldenReports", goldenFileName); + if (File.Exists(goldenReportPath)) { - var d = await RunGenerator(await File.ReadAllTextAsync(inputFile), tmp, options); - Assert.Empty(d); + string temporaryReportFile = Path.GetTempFileName(); + var diagnostic = await RunGenerator(await File.ReadAllTextAsync(inputFile), temporaryReportFile, options); + Assert.Empty(diagnostic); - var golden = await File.ReadAllTextAsync(goldenReportPath); - var generated = await File.ReadAllTextAsync(tmp); + string golden = await File.ReadAllTextAsync(goldenReportPath); + string generated = await File.ReadAllTextAsync(temporaryReportFile); if (golden != generated) { - output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {tmp}"); + output.WriteLine($"MISMATCH: golden report {goldenReportPath}, generated {temporaryReportFile}"); output.WriteLine("----"); output.WriteLine("golden:"); output.WriteLine(golden); @@ -65,14 +66,14 @@ public async Task TestAll(bool useExplicitReportPath) output.WriteLine("----"); } - File.Delete(tmp); + File.Delete(temporaryReportFile); Assert.Equal(golden, generated); } else { // generate the golden file if it doesn't already exist output.WriteLine($"Generating golden report: {goldenReportPath}"); - _ = await RunGenerator(await File.ReadAllTextAsync(inputFile), tmp, options); + _ = await RunGenerator(await File.ReadAllTextAsync(inputFile), goldenFileName, options); } } } From b3d9357a2a0747e221d5aae23142f852c8d8119c Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Thu, 20 Feb 2025 13:39:35 +0300 Subject: [PATCH 13/42] remove unneeded check --- .../MetadataReportsGenerator.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 679b9784cc8..7166e07b450 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -74,12 +74,6 @@ public void Execute(GeneratorExecutionContext context) return; } - if ((context.SyntaxReceiver is not TypeDeclarationSyntaxReceiver || ((TypeDeclarationSyntaxReceiver)context.SyntaxReceiver).TypeDeclarations.Count == 0)) - { - // nothing to do yet - return; - } - var options = context.AnalyzerConfigOptions.GlobalOptions; _directory ??= GeneratorUtilities.TryRetrieveOptionsValue(options, ReportOutputPathMSBuildProperty, out var reportOutputPath) ? reportOutputPath! From 48d0b6fa8118c53d9319c77632543dbebaa2a679 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Thu, 20 Feb 2025 13:40:41 +0300 Subject: [PATCH 14/42] adding comment --- .../Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 7166e07b450..06ec6529559 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -71,6 +71,7 @@ public void Execute(GeneratorExecutionContext context) ((TypeDeclarationSyntaxReceiver)context.SyntaxReceiver).TypeDeclarations.Count == 0 || !GeneratorUtilities.ShouldGenerateReport(context, GenerateMetadataMSBuildProperty)) { + // nothing to do yet return; } From ab59a4aedf31d567a247524f0f0aaaa245c7efe3 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 17:40:29 +0300 Subject: [PATCH 15/42] introduce MetadataEmitter --- ...{Emitter.cs => ComplianceReportEmitter.cs} | 4 +- .../ComplianceReportsGenerator.cs | 2 +- .../MetadataEmitter.cs | 94 +++++++++++++++++++ .../MetadataReportsGenerator.cs | 68 +------------- 4 files changed, 99 insertions(+), 69 deletions(-) rename src/Generators/Microsoft.Gen.ComplianceReports/{Emitter.cs => ComplianceReportEmitter.cs} (98%) create mode 100644 src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/Emitter.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs similarity index 98% rename from src/Generators/Microsoft.Gen.ComplianceReports/Emitter.cs rename to src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs index 2e8b63dcae7..012a1db7179 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/Emitter.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs @@ -10,12 +10,12 @@ namespace Microsoft.Gen.ComplianceReports; -internal sealed class Emitter : EmitterBase +internal sealed class ComplianceReportEmitter : EmitterBase { private readonly Stack _itemCounts = new(); private int _itemCount; - public Emitter() + public ComplianceReportEmitter() : base(false) { } diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs index 454aa40fe3f..c99455795d9 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportsGenerator.cs @@ -83,7 +83,7 @@ public void Execute(GeneratorExecutionContext context) return; } - var emitter = new Emitter(); + var emitter = new ComplianceReportEmitter(); string report = emitter.Emit(classifiedTypes, context.Compilation.AssemblyName!); context.CancellationToken.ThrowIfCancellationRequested(); diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs new file mode 100644 index 00000000000..04c06acb246 --- /dev/null +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -0,0 +1,94 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.Gen.ComplianceReports; +using Microsoft.Gen.MetricsReports; +using Microsoft.Gen.Shared; + +namespace Microsoft.Gen.MetadataExtractor; +internal sealed class MetadataEmitter : EmitterBase +{ + private readonly MetricDefinitionEmitter _metricDefinitionEmitter; + private readonly ComplianceReportEmitter _complianceReportEmitter; + private readonly string _rootNamespace; + + public MetadataEmitter(string rootNamespace) + : base(emitPreamble: false) + { + _metricDefinitionEmitter = new MetricDefinitionEmitter(); + _complianceReportEmitter = new ComplianceReportEmitter(); + _rootNamespace = rootNamespace; + } + + [SuppressMessage("Performance", "LA0002:Use 'Microsoft.Extensions.Text.NumericExtensions.ToInvariantString' for improved performance", Justification = "Can't use that in a generator")] + public string Emit(GeneratorExecutionContext context) + { + (string metricReport, string complianceReport) metadataReport = (string.Empty, string.Empty); + metadataReport.metricReport = HandleMetricReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); + metadataReport.complianceReport = HandleComplianceReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); + + OutLn("{"); + Out("\"Name\":" + $"\"{context.Compilation.AssemblyName!}\""); + OutLn(","); + OutIndent(); + Out("\"ComplianceReport\": "); + Out((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)); + OutLn(","); + Out("\"MetricReport\": "); + OutLn((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); + OutLn("}"); + + var xx = Capture(); + return xx; + } + + /// + /// used to generate the report for metrics annotations. + /// + /// The generator execution context. + /// The typeDeclaration syntax receiver. + /// string report as json or String.Empty. + private string HandleMetricReportGeneration(GeneratorExecutionContext context, TypeDeclarationSyntaxReceiver receiver) + { + var meteringParser = new Metrics.Parser(context.Compilation, context.ReportDiagnostic, context.CancellationToken); + var meteringClasses = meteringParser.GetMetricClasses(receiver.TypeDeclarations); + + if (meteringClasses.Count == 0) + { + return string.Empty; + } + + _ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(_rootNamespace, out var rootNamespace); + var reportedMetrics = MetricsReportsHelpers.MapToCommonModel(meteringClasses, rootNamespace); + var report = _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken); + return report; + } + + /// + /// used to generate the report for compliance annotations. + /// + /// The generator execution context. + /// The type declaration syntax receiver. + /// string report as json or String.Empty. + private string HandleComplianceReportGeneration(GeneratorExecutionContext context, TypeDeclarationSyntaxReceiver receiver) + { + if (!SymbolLoader.TryLoad(context.Compilation, out var symbolHolder)) + { + return string.Empty; + } + + var parser = new Parser(context.Compilation, symbolHolder!, context.CancellationToken); + var classifiedTypes = parser.GetClassifiedTypes(receiver.TypeDeclarations); + if (classifiedTypes.Count == 0) + { + // nothing to do + return string.Empty; + } + + string report = _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false); + return report; + } +} diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 06ec6529559..316b66c4719 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -96,77 +96,13 @@ public void Execute(GeneratorExecutionContext context) return; } - (string metricReport, string complianceReport) metadataReport = (string.Empty, string.Empty); - metadataReport.metricReport = HandleMetricReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); - metadataReport.complianceReport = HandleComplianceReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); - - StringBuilder reportStringBuilder = new StringBuilder() - .AppendLine("{") - .Append("\"Name\":") - .Append($"\"{context.Compilation.AssemblyName!}\"") - .AppendLine(",") - .Append("\"ComplianceReport\": ") - .Append((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)) - .AppendLine(",") - .Append("\"MetricReport\": ") - .AppendLine((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)) - .AppendLine("}"); + MetadataEmitter Emitter = new MetadataEmitter(RootNamespace); #pragma warning disable RS1035 // Do not use APIs banned for analyzers _ = Directory.CreateDirectory(_directory); - File.WriteAllText(Path.Combine(_directory, _fileName), reportStringBuilder.ToString(), Encoding.UTF8); + File.WriteAllText(Path.Combine(_directory, _fileName), Emitter.Emit(context), Encoding.UTF8); #pragma warning restore RS1035 // Do not use APIs banned for analyzers } - - /// - /// used to generate the report for metrics annotations. - /// - /// The generator execution context. - /// The typeDeclaration syntax receiver. - /// string report as json or String.Empty. - private static string HandleMetricReportGeneration(GeneratorExecutionContext context, TypeDeclarationSyntaxReceiver receiver) - { - var meteringParser = new Metrics.Parser(context.Compilation, context.ReportDiagnostic, context.CancellationToken); - var meteringClasses = meteringParser.GetMetricClasses(receiver.TypeDeclarations); - - if (meteringClasses.Count == 0) - { - return string.Empty; - } - - _ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(RootNamespace, out var rootNamespace); - var reportedMetrics = MetricsReportsHelpers.MapToCommonModel(meteringClasses, rootNamespace); - var emitter = new MetricDefinitionEmitter(); - var report = emitter.GenerateReport(reportedMetrics, context.CancellationToken); - return report; - } - - /// - /// used to generate the report for compliance annotations. - /// - /// The generator execution context. - /// The type declaration syntax receiver. - /// string report as json or String.Empty. - private static string HandleComplianceReportGeneration(GeneratorExecutionContext context, TypeDeclarationSyntaxReceiver receiver) - { - if (!SymbolLoader.TryLoad(context.Compilation, out var symbolHolder)) - { - return string.Empty; - } - - var parser = new Parser(context.Compilation, symbolHolder!, context.CancellationToken); - var classifiedTypes = parser.GetClassifiedTypes(receiver.TypeDeclarations); - if (classifiedTypes.Count == 0) - { - // nothing to do - return string.Empty; - } - - var emitter = new Emitter(); - string report = emitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false); - - return report; - } } From b49fc54f0ed53f6355cb958ad651d8a23820980c Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 17:41:00 +0300 Subject: [PATCH 16/42] a --- .../Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 04c06acb246..879e67cd62e 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; -using System.Text; using Microsoft.CodeAnalysis; using Microsoft.Gen.ComplianceReports; using Microsoft.Gen.MetricsReports; From 76c4007309969201309f5d4d9e50e835f7482991 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 18:22:48 +0300 Subject: [PATCH 17/42] Adding Indentation for complince --- .../ComplianceReportEmitter.cs | 13 ++++++++++++- .../MetadataEmitter.cs | 7 ++++--- .../MetadataReportsGenerator.cs | 4 ++-- src/Generators/Shared/EmitterBase.cs | 2 ++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs index 012a1db7179..ca8605122d7 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs @@ -20,9 +20,19 @@ public ComplianceReportEmitter() { } + /// + /// Generates JSON object containing the for compliance report. + /// + /// The classified types. + /// The assembly name. + /// Whether to include the assembly name in the report. Defaulted to true. + /// The number of indentations in case its nested in other reports like .Defaulted to zero. + /// string report as json or String.Empty. [SuppressMessage("Performance", "LA0002:Use 'Microsoft.Extensions.Text.NumericExtensions.ToInvariantString' for improved performance", Justification = "Can't use that in a generator")] - public string Emit(IReadOnlyCollection classifiedTypes, string assemblyName, bool includeName = true) // show or hide assemblyName in the report,defaulted to true. + public string Emit(IReadOnlyCollection classifiedTypes, string assemblyName, + bool includeName = true, int indentationLevel = 0) // show or hide assemblyName in the report,defaulted to true. { + IndentMany(indentationLevel); OutObject(() => { // this is only for not displaying a name as part of ComplianceReport properties,it should be at the root of the report, defaulted to true for beackward compatibility @@ -125,6 +135,7 @@ public string Emit(IReadOnlyCollection classifiedTypes, string a } }); }); + UnindentMany(indentationLevel); return Capture(); } diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 879e67cd62e..cb5f92d041e 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -37,7 +37,8 @@ public string Emit(GeneratorExecutionContext context) Out((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)); OutLn(","); Out("\"MetricReport\": "); - OutLn((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); + Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); + OutLn(); OutLn("}"); var xx = Capture(); @@ -62,7 +63,7 @@ private string HandleMetricReportGeneration(GeneratorExecutionContext context, T _ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(_rootNamespace, out var rootNamespace); var reportedMetrics = MetricsReportsHelpers.MapToCommonModel(meteringClasses, rootNamespace); - var report = _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken); + var report = _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken, indentationLevel: 4); return report; } @@ -87,7 +88,7 @@ private string HandleComplianceReportGeneration(GeneratorExecutionContext contex return string.Empty; } - string report = _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false); + string report = _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false, indentationLevel: 4); return report; } } diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 316b66c4719..339b7f55f04 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -96,12 +96,12 @@ public void Execute(GeneratorExecutionContext context) return; } - MetadataEmitter Emitter = new MetadataEmitter(RootNamespace); + MetadataEmitter emitter = new MetadataEmitter(RootNamespace); #pragma warning disable RS1035 // Do not use APIs banned for analyzers _ = Directory.CreateDirectory(_directory); - File.WriteAllText(Path.Combine(_directory, _fileName), Emitter.Emit(context), Encoding.UTF8); + File.WriteAllText(Path.Combine(_directory, _fileName), emitter.Emit(context), Encoding.UTF8); #pragma warning restore RS1035 // Do not use APIs banned for analyzers } diff --git a/src/Generators/Shared/EmitterBase.cs b/src/Generators/Shared/EmitterBase.cs index c220026ad69..7eb6ed695bb 100644 --- a/src/Generators/Shared/EmitterBase.cs +++ b/src/Generators/Shared/EmitterBase.cs @@ -100,6 +100,8 @@ protected void OutEnumeration(IEnumerable e) protected void Out(char ch) => _ = _sb.Append(ch); protected void Indent() => _indent++; protected void Unindent() => _indent--; + protected void IndentMany(int times) => _indent += times; + protected void UnindentMany(int times) => _indent -= times; protected void OutGeneratedCodeAttribute() => OutLn($"[{GeneratorUtilities.GeneratedCodeAttribute}]"); protected string Capture() => _sb.ToString(); } From 98fceae50bc307088f33cf23c13798db2c2b6914 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 20:15:13 +0300 Subject: [PATCH 18/42] fix indentation on metrics --- .../MetadataEmitter.cs | 3 +- .../MetricDefinitionEmitter.cs | 78 +++++++++++-------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index cb5f92d041e..32851269f63 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -37,8 +37,7 @@ public string Emit(GeneratorExecutionContext context) Out((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)); OutLn(","); Out("\"MetricReport\": "); - Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); - OutLn(); + OutLn((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); OutLn("}"); var xx = Capture(); diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index e6c23ae7f9a..482452e0cae 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -18,62 +18,81 @@ internal MetricDefinitionEmitter() { } - public string GenerateReport(IReadOnlyList metricClasses, CancellationToken cancellationToken) + /// + /// Generates JSON object containing the for metrics report. + /// + /// The reported metric classes. + /// The cancellation token. + /// The number of indentations in case its nested in other reports like .Defaulted to zero. + /// string report as json or String.Empty. + public string GenerateReport(IReadOnlyList metricClasses, CancellationToken cancellationToken = default, int indentationLevel = 0) { if (metricClasses == null || metricClasses.Count == 0) { return string.Empty; } + if (indentationLevel > 0) + { + OutLn(); + } + + IndentMany(indentationLevel); OutLn("["); for (int i = 0; i < metricClasses.Count; i++) { cancellationToken.ThrowIfCancellationRequested(); var metricClass = metricClasses[i]; + GenMetricClassDefinition(metricClass, cancellationToken); if (i < metricClasses.Count - 1) { - Out(","); + OutLn(","); } - - OutLn(); } - Out("]"); + OutLn("]"); + UnindentMany(indentationLevel); + return Capture(); } private void GenMetricClassDefinition(ReportedMetricClass metricClass, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - OutLn(" {"); + Indent(); + OutLn("{"); - OutLn($" \"{metricClass.RootNamespace}\":"); + OutLn($" \"{metricClass.RootNamespace}\":"); if (metricClass.Methods.Length > 0) { - OutLn(" ["); + IndentMany(2); + OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) { + Indent(); var metricMethod = metricClass.Methods[j]; GenMetricMethodDefinition(metricMethod, cancellationToken); if (j < metricClass.Methods.Length - 1) { - Out(","); + OutLn(","); } - OutLn(); + Unindent(); } - OutLn(" ]"); + OutLn("]"); + UnindentMany(2); } - Out(" }"); + OutLn("}"); + Unindent(); } private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, CancellationToken cancellationToken) @@ -86,8 +105,8 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel try { cancellationToken.ThrowIfCancellationRequested(); - - OutLn(" {"); + //Indent(); + OutLn("{"); OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); @@ -96,46 +115,41 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel OutLn($" \"MetricDescription\": \"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); } - Out($" \"InstrumentName\": \"{metricMethod.Kind}\""); + if (metricMethod.Dimensions.Count == 0) + { + OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\""); + } if (metricMethod.Dimensions.Count > 0) { - OutLn(","); - - Out(" \"Dimensions\": {"); - + OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); + OutLn(" \"Dimensions\": {"); + Indent(); int k = 0; - foreach (var dimension in metricMethod.Dimensions) { - OutLn(); + bool commaCondition = k < metricMethod.Dimensions.Count - 1; if (metricMethod.DimensionsDescriptions.TryGetValue(dimension, out var description)) { - Out($" \"{dimension}\": \"{description.Replace("\\", "\\\\").Replace("\"", "\\\"")}\""); + OutLn($"\"{dimension}\": \"{description.Replace("\\", "\\\\").Replace("\"", "\\\"")}\"{(commaCondition ? "," : string.Empty)}"); } else { - Out($" \"{dimension}\": \"\""); - } - - if (k < metricMethod.Dimensions.Count - 1) - { - Out(","); + OutLn($"\"{dimension}\": \"\"{(commaCondition ? "," : string.Empty)}"); } k++; } - OutLn(); - Out(" }"); - OutLn(); + Unindent(); + OutLn("}"); } else { OutLn(); } - Out(" }"); + OutLn("}"); } catch (Exception e) { From ed829dea37234423fa7d28ec3c1a8edee35ac462 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 20:22:47 +0300 Subject: [PATCH 19/42] fix form,at --- .../MetricDefinitionEmitter.cs | 5 +- .../GoldenReports/Basic.json | 216 +++++++++--------- .../GoldenReports/Inheritance.json | 110 ++++----- .../GoldenReports/LogMethod.json | 50 ++-- 4 files changed, 192 insertions(+), 189 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 482452e0cae..f2452bc713a 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -123,7 +123,9 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel if (metricMethod.Dimensions.Count > 0) { OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); - OutLn(" \"Dimensions\": {"); + OutLn(" \"Dimensions\":"); + IndentMany(3); + OutLn("{"); Indent(); int k = 0; foreach (var dimension in metricMethod.Dimensions) @@ -143,6 +145,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel Unindent(); OutLn("}"); + UnindentMany(3); } else { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json index 83c41b2bc8a..00cb01ae695 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json @@ -1,122 +1,122 @@ { "Name":"test.dll", "ComplianceReport": -{ - "Types": [ - { - "Name": "Test.Basic", - "Members": [ - { - "Name": "F0", - "Type": "int", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, - { - "Name": "F1", - "Type": "int", - "File": "src-0.cs", - "Line": "21", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2" - } - ] - }, - { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C3", - "Notes": "Note 2" - }, - { - "Name": "C4" - } - ] - }, - { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "27", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C3" - } - ] - } - ], - "Logging Methods": [ { - "Name": "LogHello", - "Parameters": [ + "Types": [ { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "30", - "Classifications": [ + "Name": "Test.Basic", + "Members": [ + { + "Name": "F0", + "Type": "int", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F1", + "Type": "int", + "File": "src-0.cs", + "Line": "21", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C2" + } + ] + }, { - "Name": "C2", - "Notes": "Note 3" + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C3", + "Notes": "Note 2" + }, + { + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "27", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C3" + } + ] } - ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "30" - } - ] - }, - { - "Name": "LogWorld", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "33", - "Classifications": [ + ], + "Logging Methods": [ + { + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "30", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 3" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "30" + } + ] + }, { - "Name": "C2" + "Name": "LogWorld", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "33", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "33" + } + ] } ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "33" } ] - } - ] - } - ] -}, + }, "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json index 77cb62c90e1..dcea34ac316 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json @@ -1,66 +1,66 @@ { "Name":"test.dll", "ComplianceReport": -{ - "Types": [ - { - "Name": "Test.Base", - "Members": [ - { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C1" - } - ] - }, - { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C2" - } - ] - } - ] - }, - { - "Name": "Test.Inherited", - "Members": [ - { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C1" - } - ] - }, { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ + "Types": [ { - "Name": "C2" + "Name": "Test.Base", + "Members": [ + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C1" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C2" + } + ] + } + ] }, { - "Name": "C3" + "Name": "Test.Inherited", + "Members": [ + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C1" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C2" + }, + { + "Name": "C3" + } + ] + } + ] } ] - } - ] - } - ] -}, + }, "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json index 576539b0e3e..20b58adf0de 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json @@ -1,37 +1,37 @@ { "Name":"test.dll", "ComplianceReport": -{ - "Types": [ - { - "Name": "Test.LogMethod", - "Logging Methods": [ { - "Name": "LogHello", - "Parameters": [ + "Types": [ { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ + "Name": "Test.LogMethod", + "Logging Methods": [ { - "Name": "C2", - "Notes": "Note 3" + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 3" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "11" + } + ] } ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "11" } ] - } - ] - } - ] -}, + }, "MetricReport": [] } From 04e0c101343477cc96ac7aa424169bab60a3eef9 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 20:44:26 +0300 Subject: [PATCH 20/42] Fix format for unit testing --- .../MeterAttributedWithXmlDescriptions.json | 71 +-- ...tedWithXmlDescriptions_RecordProperty.json | 445 +++++++++--------- ...mensionsAttributedWithXmlDescriptions.json | 85 ++-- ...tributedWithXmlDescriptions_LogMethod.json | 135 +++--- .../GoldenReports/RecordProperty.json | 374 +++++++-------- 5 files changed, 571 insertions(+), 539 deletions(-) diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index 15da84dbedf..e9e3c95a95c 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,35 +1,44 @@ { "Name":"test.dll", "ComplianceReport": {}, -"MetricReport": [ - { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] - } -] +"MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + + } + , + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + + } + , + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + + } + , + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] + } + ] + } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 8de54a2991f..7809d1165a5 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -1,234 +1,243 @@ { "Name":"test.dll", "ComplianceReport": -{ - "Types": [ - { - "Name": "Test.DerivedRecordProperty", - "Members": [ - { - "Name": "EqualityContract", - "Type": "System.Type", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C1" - } - ] - }, - { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "21", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, - { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "24", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "27", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2", - "Notes": "Note 2" - }, - { - "Name": "C3", - "Notes": "Note 3" - }, - { - "Name": "C4" - } - ] - }, - { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "30", - "Classifications": [ - { - "Name": "C3" - } - ] - } - ] - }, - { - "Name": "Test.RecordProperty", - "Members": [ - { - "Name": "F0", - "Type": "string", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "F2", - "Type": "int", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C3" - } - ] - }, - { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "21", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, - { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "24", - "Classifications": [ - { - "Name": "C2" - } - ] - }, { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "15", - "Classifications": [ + "Types": [ { - "Name": "C3", - "Notes": "Note 3" - }, - { - "Name": "C4" - } - ] - }, - { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "30", - "Classifications": [ - { - "Name": "C3" - } - ] - } - ], - "Logging Methods": [ - { - "Name": "LogHello", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "33", - "Classifications": [ - { - "Name": "C3", - "Notes": "Note 3" + "Name": "Test.DerivedRecordProperty", + "Members": [ + { + "Name": "EqualityContract", + "Type": "System.Type", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ + { + "Name": "C1" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "21", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "24", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "27", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C2", + "Notes": "Note 2" + }, + { + "Name": "C3", + "Notes": "Note 3" + }, + { + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "30", + "Classifications": [ + { + "Name": "C3" + } + ] } ] }, { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "33" + "Name": "Test.RecordProperty", + "Members": [ + { + "Name": "F0", + "Type": "string", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "F2", + "Type": "int", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ + { + "Name": "C3" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "21", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "24", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "15", + "Classifications": [ + { + "Name": "C3", + "Notes": "Note 3" + }, + { + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "30", + "Classifications": [ + { + "Name": "C3" + } + ] + } + ], + "Logging Methods": [ + { + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "33", + "Classifications": [ + { + "Name": "C3", + "Notes": "Note 3" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "33" + } + ] + }, + { + "Name": "LogWorld", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "36", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "36" + } + ] + } + ] } ] }, - { - "Name": "LogWorld", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "36", - "Classifications": [ +"MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + + } + , { - "Name": "C2" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + + } + , + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + + } + , + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "36" - } - ] - } - ] - } - ] -}, -"MetricReport": [ - { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] - } -] + } + ] + } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index d844aa385e5..ab691865396 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,43 +1,50 @@ { "Name":"test.dll", "ComplianceReport": {}, -"MetricReport": [ - { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - }, - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } - } - ] - } -] +"MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } + } + , + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + } + , + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } + } + ] + } + ] + } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index a7f867e8936..1fc5c511a1b 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -1,75 +1,82 @@ { "Name":"test.dll", "ComplianceReport": -{ - "Types": [ - { - "Name": "Test.LogMethod", - "Logging Methods": [ { - "Name": "LogHello", - "Parameters": [ + "Types": [ { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ + "Name": "Test.LogMethod", + "Logging Methods": [ { - "Name": "C2", - "Notes": "Note 3" + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 3" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "14" + } + ] } ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "14" } ] - } - ] - } - ] -}, -"MetricReport": [ - { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - }, - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "MetricEnum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "SomeDim": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5Struct": "Gets or sets Dim5Struct." - } - } - ] - } -] + }, +"MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } + } + , + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + } + , + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "MetricEnum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "SomeDim": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5Struct": "Gets or sets Dim5Struct." + } + } + ] + } + ] + } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json index 4055d5d54fc..0852ab0c072 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json @@ -1,204 +1,204 @@ { "Name":"test.dll", "ComplianceReport": -{ - "Types": [ - { - "Name": "Test.DerivedRecordProperty", - "Members": [ - { - "Name": "EqualityContract", - "Type": "System.Type", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C1" - } - ] - }, - { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "17", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, - { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "20", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "23", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2", - "Notes": "Note 2" - }, - { - "Name": "C3", - "Notes": "Note 3" - }, - { - "Name": "C4" - } - ] - }, - { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "26", - "Classifications": [ - { - "Name": "C3" - } - ] - } - ] - }, - { - "Name": "Test.RecordProperty", - "Members": [ - { - "Name": "F0", - "Type": "string", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "F2", - "Type": "int", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C3" - } - ] - }, { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "17", - "Classifications": [ + "Types": [ { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, - { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "20", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C3", - "Notes": "Note 3" - }, - { - "Name": "C4" - } - ] - }, - { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "26", - "Classifications": [ - { - "Name": "C3" - } - ] - } - ], - "Logging Methods": [ - { - "Name": "LogHello", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "29", - "Classifications": [ + "Name": "Test.DerivedRecordProperty", + "Members": [ { - "Name": "C3", - "Notes": "Note 3" + "Name": "EqualityContract", + "Type": "System.Type", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C1" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "17", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "20", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "23", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C2", + "Notes": "Note 2" + }, + { + "Name": "C3", + "Notes": "Note 3" + }, + { + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "26", + "Classifications": [ + { + "Name": "C3" + } + ] } ] }, { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "29" - } - ] - }, - { - "Name": "LogWorld", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "32", - "Classifications": [ + "Name": "Test.RecordProperty", + "Members": [ + { + "Name": "F0", + "Type": "string", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C2" + } + ] + }, { - "Name": "C2" + "Name": "F2", + "Type": "int", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C3" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "17", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "20", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C3", + "Notes": "Note 3" + }, + { + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "26", + "Classifications": [ + { + "Name": "C3" + } + ] + } + ], + "Logging Methods": [ + { + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "29", + "Classifications": [ + { + "Name": "C3", + "Notes": "Note 3" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "29" + } + ] + }, + { + "Name": "LogWorld", + "Parameters": [ + { + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "32", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "32" + } + ] } ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "32" } ] - } - ] - } - ] -}, + }, "MetricReport": [] } From 4e0962a80e1f2a102884fda97800fbf771965498 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 21:20:58 +0300 Subject: [PATCH 21/42] fixing metrics formats --- .../MeterAttributedWithXmlDescriptions.json | 63 ++++++----- ...mensionsAttributedWithXmlDescriptions.json | 77 +++++++------ .../Unit/EmitterTests.cs | 104 ++++++++++-------- 3 files changed, 132 insertions(+), 112 deletions(-) diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index fc05721109c..4222c3d822a 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,31 +1,38 @@ [ - { - "TestClasses": - [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + + } + , + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + + } + , + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + + } + , + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] } - ] - } -] \ No newline at end of file +] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index aec09194f8a..ab20d639591 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,39 +1,44 @@ [ - { - "TestClasses": - [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - }, - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } + } + , + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + } + , + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } + } + ] } - ] - } -] \ No newline at end of file +] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index f3207d39570..89a7e822cfc 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -128,54 +128,62 @@ public void GetMetricClassDefinition_GivenMetricTypeIsUnknown_ThrowsNotSupported [Fact] public void EmitterShouldOutputInJSONFormat() { - string newLine = Environment.NewLine; - string expected = - "[" + - newLine + " {" + - newLine + " \"MetricContainingAssembly1\":" + - newLine + " [" + - newLine + " {" + - newLine + " \"MetricName\": \"Requests\"," + - newLine + " \"MetricDescription\": \"Requests summary.\"," + - newLine + " \"InstrumentName\": \"Counter\"," + - newLine + " \"Dimensions\": {" + - newLine + " \"StatusCode\": \"Status code for request.\"," + - newLine + " \"ErrorCode\": \"Error code for request.\"" + - newLine + " }" + - newLine + " }," + - newLine + " {" + - newLine + " \"MetricName\": \"Latency\"," + - newLine + " \"MetricDescription\": \"Latency summary.\"," + - newLine + " \"InstrumentName\": \"Histogram\"," + - newLine + " \"Dimensions\": {" + - newLine + " \"Dim1\": \"\"" + - newLine + " }" + - newLine + " }," + - newLine + " {" + - newLine + " \"MetricName\": \"MemoryUsage\"," + - newLine + " \"InstrumentName\": \"Gauge\"" + - newLine + " }" + - newLine + " ]" + - newLine + " }," + - newLine + " {" + - newLine + " \"MetricContainingAssembly2\":" + - newLine + " [" + - newLine + " {" + - newLine + " \"MetricName\": \"Counter\"," + - newLine + " \"MetricDescription\": \"Counter summary.\"," + - newLine + " \"InstrumentName\": \"Counter\"" + - newLine + " }," + - newLine + " {" + - newLine + " \"MetricName\": \"Test\\\\MemoryUsage\"," + - newLine + " \"MetricDescription\": \"MemoryUsage summary.\"," + - newLine + " \"InstrumentName\": \"Gauge\"," + - newLine + " \"Dimensions\": {" + - newLine + " \"Path\": \"Test\\\\Description\\\\Path\"" + - newLine + " }" + - newLine + " }" + - newLine + " ]" + - newLine + " }" + - newLine + "]"; + string expected = @"[ + { + ""MetricContainingAssembly1"": + [ + { + ""MetricName"": ""Requests"", + ""MetricDescription"": ""Requests summary."", + ""InstrumentName"": ""Counter"", + ""Dimensions"": + { + ""StatusCode"": ""Status code for request."", + ""ErrorCode"": ""Error code for request."" + } + } + , + { + ""MetricName"": ""Latency"", + ""MetricDescription"": ""Latency summary."", + ""InstrumentName"": ""Histogram"", + ""Dimensions"": + { + ""Dim1"": """" + } + } + , + { + ""MetricName"": ""MemoryUsage"", + ""InstrumentName"": ""Gauge"" + + } + ] + } +, + { + ""MetricContainingAssembly2"": + [ + { + ""MetricName"": ""Counter"", + ""MetricDescription"": ""Counter summary."", + ""InstrumentName"": ""Counter"" + + } + , + { + ""MetricName"": ""Test\\MemoryUsage"", + ""MetricDescription"": ""MemoryUsage summary."", + ""InstrumentName"": ""Gauge"", + ""Dimensions"": + { + ""Path"": ""Test\\Description\\Path"" + } + } + ] + } +] +"; var emitter = new MetricDefinitionEmitter(); string json = emitter.GenerateReport(_metricClasses, CancellationToken.None); From 2da5f0a032bd58b88e10311cfcbd88eb0dbe1f1e Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 21:38:55 +0300 Subject: [PATCH 22/42] fix checker --- .../Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs | 9 ++++----- .../MetricDefinitionEmitter.cs | 7 ++++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 32851269f63..f2d6b9db699 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -13,7 +13,7 @@ internal sealed class MetadataEmitter : EmitterBase private readonly MetricDefinitionEmitter _metricDefinitionEmitter; private readonly ComplianceReportEmitter _complianceReportEmitter; private readonly string _rootNamespace; - + private readonly int _indentationLevel = 4; public MetadataEmitter(string rootNamespace) : base(emitPreamble: false) { @@ -40,8 +40,7 @@ public string Emit(GeneratorExecutionContext context) OutLn((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); OutLn("}"); - var xx = Capture(); - return xx; + return Capture(); } /// @@ -62,7 +61,7 @@ private string HandleMetricReportGeneration(GeneratorExecutionContext context, T _ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(_rootNamespace, out var rootNamespace); var reportedMetrics = MetricsReportsHelpers.MapToCommonModel(meteringClasses, rootNamespace); - var report = _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken, indentationLevel: 4); + var report = _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken, _indentationLevel); return report; } @@ -87,7 +86,7 @@ private string HandleComplianceReportGeneration(GeneratorExecutionContext contex return string.Empty; } - string report = _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false, indentationLevel: 4); + string report = _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false, _indentationLevel); return report; } } diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index f2452bc713a..e8df7a2d592 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -18,6 +18,8 @@ internal MetricDefinitionEmitter() { } + private readonly int _indentLevel = 2; + /// /// Generates JSON object containing the for metrics report. /// @@ -69,7 +71,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat if (metricClass.Methods.Length > 0) { - IndentMany(2); + IndentMany(_indentLevel); OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) @@ -88,7 +90,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat } OutLn("]"); - UnindentMany(2); + UnindentMany(_indentLevel); } OutLn("}"); @@ -105,7 +107,6 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel try { cancellationToken.ThrowIfCancellationRequested(); - //Indent(); OutLn("{"); OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); From b8d598cbc092c3eaa62da9169b30391d8696b9c4 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Mon, 24 Feb 2025 22:11:54 +0300 Subject: [PATCH 23/42] fix checks --- .../Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs | 10 +++++++--- .../MetadataReportsGenerator.cs | 2 -- .../MetricDefinitionEmitter.cs | 5 +++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index f2d6b9db699..7b7d8e9d4b4 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -1,6 +1,5 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. - using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; using Microsoft.Gen.ComplianceReports; @@ -26,8 +25,13 @@ public MetadataEmitter(string rootNamespace) public string Emit(GeneratorExecutionContext context) { (string metricReport, string complianceReport) metadataReport = (string.Empty, string.Empty); - metadataReport.metricReport = HandleMetricReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); - metadataReport.complianceReport = HandleComplianceReportGeneration(context, (TypeDeclarationSyntaxReceiver)context.SyntaxReceiver); + + var receiver = context.SyntaxReceiver as TypeDeclarationSyntaxReceiver; + if (receiver != null) + { + metadataReport.metricReport = HandleMetricReportGeneration(context, receiver); + metadataReport.complianceReport = HandleComplianceReportGeneration(context, receiver); + } OutLn("{"); Out("\"Name\":" + $"\"{context.Compilation.AssemblyName!}\""); diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs index 339b7f55f04..5526e6296b3 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataReportsGenerator.cs @@ -5,8 +5,6 @@ using System.IO; using System.Text; using Microsoft.CodeAnalysis; -using Microsoft.Gen.ComplianceReports; -using Microsoft.Gen.MetricsReports; using Microsoft.Gen.Shared; using Microsoft.Shared.DiagnosticIds; diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index e8df7a2d592..b6c1db92371 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -123,9 +123,10 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel if (metricMethod.Dimensions.Count > 0) { + const int IndentLevel = 3; OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); OutLn(" \"Dimensions\":"); - IndentMany(3); + IndentMany(IndentLevel); OutLn("{"); Indent(); int k = 0; @@ -146,7 +147,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel Unindent(); OutLn("}"); - UnindentMany(3); + UnindentMany(IndentLevel); } else { From 44b512fb90eb34db05cd589bac048b9b0367a932 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Tue, 25 Feb 2025 07:38:18 +0300 Subject: [PATCH 24/42] adding overloads for indent --- .../ComplianceReportEmitter.cs | 4 ++-- .../MetricDefinitionEmitter.cs | 12 ++++++------ src/Generators/Shared/EmitterBase.cs | 6 ++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs index ca8605122d7..0cee7cc98f5 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs @@ -32,7 +32,7 @@ public ComplianceReportEmitter() public string Emit(IReadOnlyCollection classifiedTypes, string assemblyName, bool includeName = true, int indentationLevel = 0) // show or hide assemblyName in the report,defaulted to true. { - IndentMany(indentationLevel); + Indent(indentationLevel); OutObject(() => { // this is only for not displaying a name as part of ComplianceReport properties,it should be at the root of the report, defaulted to true for beackward compatibility @@ -135,7 +135,7 @@ public string Emit(IReadOnlyCollection classifiedTypes, string a } }); }); - UnindentMany(indentationLevel); + Unindent(indentationLevel); return Capture(); } diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index b6c1db92371..8a9f7992a51 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -39,7 +39,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C OutLn(); } - IndentMany(indentationLevel); + Indent(indentationLevel); OutLn("["); for (int i = 0; i < metricClasses.Count; i++) @@ -56,7 +56,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C } OutLn("]"); - UnindentMany(indentationLevel); + Unindent(indentationLevel); return Capture(); } @@ -71,7 +71,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat if (metricClass.Methods.Length > 0) { - IndentMany(_indentLevel); + Indent(_indentLevel); OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) @@ -90,7 +90,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat } OutLn("]"); - UnindentMany(_indentLevel); + Unindent(_indentLevel); } OutLn("}"); @@ -126,7 +126,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel const int IndentLevel = 3; OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); OutLn(" \"Dimensions\":"); - IndentMany(IndentLevel); + Indent(IndentLevel); OutLn("{"); Indent(); int k = 0; @@ -147,7 +147,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel Unindent(); OutLn("}"); - UnindentMany(IndentLevel); + Unindent(IndentLevel); } else { diff --git a/src/Generators/Shared/EmitterBase.cs b/src/Generators/Shared/EmitterBase.cs index 7eb6ed695bb..92c9e7f2e2c 100644 --- a/src/Generators/Shared/EmitterBase.cs +++ b/src/Generators/Shared/EmitterBase.cs @@ -98,10 +98,8 @@ protected void OutEnumeration(IEnumerable e) protected void Out(string text) => _ = _sb.Append(text); protected void Out(char ch) => _ = _sb.Append(ch); - protected void Indent() => _indent++; - protected void Unindent() => _indent--; - protected void IndentMany(int times) => _indent += times; - protected void UnindentMany(int times) => _indent -= times; + protected void Indent(int times = 1) => _indent += times; + protected void Unindent(int times = 1) => _indent -= times; protected void OutGeneratedCodeAttribute() => OutLn($"[{GeneratorUtilities.GeneratedCodeAttribute}]"); protected string Capture() => _sb.ToString(); } From 1df3627a7462e73ee4fba01c0d58b46a518eaee9 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Tue, 25 Feb 2025 10:11:30 +0300 Subject: [PATCH 25/42] enhance indentation --- .../MetadataEmitter.cs | 11 +- .../GoldenReports/Basic.json | 186 ++++----- .../GoldenReports/Inheritance.json | 98 ++--- .../GoldenReports/LogMethod.json | 50 +-- .../MeterAttributedWithXmlDescriptions.json | 76 ++-- ...tedWithXmlDescriptions_RecordProperty.json | 390 +++++++++--------- ...mensionsAttributedWithXmlDescriptions.json | 94 ++--- ...tributedWithXmlDescriptions_LogMethod.json | 138 +++---- .../GoldenReports/RecordProperty.json | 320 +++++++------- 9 files changed, 683 insertions(+), 680 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 7b7d8e9d4b4..86f27fea63c 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -12,7 +12,7 @@ internal sealed class MetadataEmitter : EmitterBase private readonly MetricDefinitionEmitter _metricDefinitionEmitter; private readonly ComplianceReportEmitter _complianceReportEmitter; private readonly string _rootNamespace; - private readonly int _indentationLevel = 4; + private readonly int _indentationLevel = 2; public MetadataEmitter(string rootNamespace) : base(emitPreamble: false) { @@ -34,14 +34,17 @@ public string Emit(GeneratorExecutionContext context) } OutLn("{"); - Out("\"Name\":" + $"\"{context.Compilation.AssemblyName!}\""); - OutLn(","); + Indent(); + OutLn("\"Name\":" + $"\"{context.Compilation.AssemblyName!}\","); OutIndent(); Out("\"ComplianceReport\": "); Out((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)); OutLn(","); + OutIndent(); Out("\"MetricReport\": "); - OutLn((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); + Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); + OutLn(); + Unindent(); OutLn("}"); return Capture(); diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json index 00cb01ae695..6d10cfd1af4 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json @@ -1,122 +1,122 @@ { -"Name":"test.dll", -"ComplianceReport": + "Name":"test.dll", + "ComplianceReport": + { + "Types": [ { - "Types": [ + "Name": "Test.Basic", + "Members": [ { - "Name": "Test.Basic", - "Members": [ + "Name": "F0", + "Type": "int", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ { - "Name": "F0", - "Type": "int", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2", - "Notes": "Note 1" - } - ] + "Name": "C1" }, { - "Name": "F1", - "Type": "int", - "File": "src-0.cs", - "Line": "21", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2" - } - ] + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F1", + "Type": "int", + "File": "src-0.cs", + "Line": "21", + "Classifications": [ + { + "Name": "C1" }, { - "Name": "P0", - "Type": "int", + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C3", + "Notes": "Note 2" + }, + { + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "27", + "Classifications": [ + { + "Name": "C1" + }, + { + "Name": "C3" + } + ] + } + ], + "Logging Methods": [ + { + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", "File": "src-0.cs", - "Line": "11", + "Line": "30", "Classifications": [ { - "Name": "C1" - }, - { - "Name": "C3", - "Notes": "Note 2" - }, - { - "Name": "C4" + "Name": "C2", + "Notes": "Note 3" } ] }, { - "Name": "P1", + "Name": "port", "Type": "int", "File": "src-0.cs", - "Line": "27", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C3" - } - ] + "Line": "30" } - ], - "Logging Methods": [ + ] + }, + { + "Name": "LogWorld", + "Parameters": [ { - "Name": "LogHello", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "30", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 3" - } - ] - }, + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "33", + "Classifications": [ { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "30" + "Name": "C2" } ] }, { - "Name": "LogWorld", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "33", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "33" - } - ] + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "33" } ] } ] - }, -"MetricReport": [] + } + ] + } , + "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json index dcea34ac316..5ef619c51c4 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json @@ -1,66 +1,66 @@ { -"Name":"test.dll", -"ComplianceReport": + "Name":"test.dll", + "ComplianceReport": + { + "Types": [ { - "Types": [ + "Name": "Test.Base", + "Members": [ { - "Name": "Test.Base", - "Members": [ + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C1" - } - ] - }, + "Name": "C1" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ + { + "Name": "C2" + } + ] + } + ] + }, + { + "Name": "Test.Inherited", + "Members": [ + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C2" - } - ] + "Name": "C1" } ] }, { - "Name": "Test.Inherited", - "Members": [ + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C1" - } - ] + "Name": "C2" }, { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C2" - }, - { - "Name": "C3" - } - ] + "Name": "C3" } ] } ] - }, -"MetricReport": [] + } + ] + } , + "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json index 20b58adf0de..3830eb49fb5 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json @@ -1,37 +1,37 @@ { -"Name":"test.dll", -"ComplianceReport": + "Name":"test.dll", + "ComplianceReport": + { + "Types": [ { - "Types": [ + "Name": "Test.LogMethod", + "Logging Methods": [ { - "Name": "Test.LogMethod", - "Logging Methods": [ + "Name": "LogHello", + "Parameters": [ { - "Name": "LogHello", - "Parameters": [ + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "11", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 3" - } - ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "11" + "Name": "C2", + "Notes": "Note 3" } ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "11" } ] } ] - }, -"MetricReport": [] + } + ] + } , + "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index e9e3c95a95c..fe75ee79c1d 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,44 +1,44 @@ { -"Name":"test.dll", -"ComplianceReport": {}, -"MetricReport": - [ - { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "Name":"test.dll", + "ComplianceReport": {} , + "MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" - } - , - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + } + , + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" - } - , - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + } + , + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" - } - , - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] - } - ] + } + , + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] + } + ] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 7809d1165a5..1849f76ed4b 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -1,243 +1,243 @@ { -"Name":"test.dll", -"ComplianceReport": + "Name":"test.dll", + "ComplianceReport": + { + "Types": [ { - "Types": [ + "Name": "Test.DerivedRecordProperty", + "Members": [ { - "Name": "Test.DerivedRecordProperty", - "Members": [ + "Name": "EqualityContract", + "Type": "System.Type", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ { - "Name": "EqualityContract", - "Type": "System.Type", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C1" - } - ] - }, + "Name": "C1" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "21", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "24", + "Classifications": [ { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "21", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "27", + "Classifications": [ + { + "Name": "C1" }, { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "24", - "Classifications": [ - { - "Name": "C2" - } - ] + "Name": "C2", + "Notes": "Note 2" }, { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "27", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2", - "Notes": "Note 2" - }, - { - "Name": "C3", - "Notes": "Note 3" - }, - { - "Name": "C4" - } - ] + "Name": "C3", + "Notes": "Note 3" }, { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "30", - "Classifications": [ - { - "Name": "C3" - } - ] + "Name": "C4" } ] }, { - "Name": "Test.RecordProperty", - "Members": [ + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "30", + "Classifications": [ { - "Name": "F0", - "Type": "string", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C2" - } - ] - }, + "Name": "C3" + } + ] + } + ] + }, + { + "Name": "Test.RecordProperty", + "Members": [ + { + "Name": "F0", + "Type": "string", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ { - "Name": "F2", - "Type": "int", - "File": "src-0.cs", - "Line": "18", - "Classifications": [ - { - "Name": "C3" - } - ] - }, + "Name": "C2" + } + ] + }, + { + "Name": "F2", + "Type": "int", + "File": "src-0.cs", + "Line": "18", + "Classifications": [ { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "21", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, + "Name": "C3" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "21", + "Classifications": [ + { + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "24", + "Classifications": [ { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "24", - "Classifications": [ - { - "Name": "C2" - } - ] + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "15", + "Classifications": [ + { + "Name": "C3", + "Notes": "Note 3" }, { - "Name": "P0", - "Type": "int", + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "30", + "Classifications": [ + { + "Name": "C3" + } + ] + } + ], + "Logging Methods": [ + { + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", "File": "src-0.cs", - "Line": "15", + "Line": "33", "Classifications": [ { "Name": "C3", "Notes": "Note 3" - }, - { - "Name": "C4" } ] }, { - "Name": "P1", + "Name": "port", "Type": "int", "File": "src-0.cs", - "Line": "30", - "Classifications": [ - { - "Name": "C3" - } - ] + "Line": "33" } - ], - "Logging Methods": [ + ] + }, + { + "Name": "LogWorld", + "Parameters": [ { - "Name": "LogHello", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "33", - "Classifications": [ - { - "Name": "C3", - "Notes": "Note 3" - } - ] - }, + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "36", + "Classifications": [ { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "33" + "Name": "C2" } ] }, { - "Name": "LogWorld", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "36", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "36" - } - ] + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "36" } ] } ] - }, -"MetricReport": - [ - { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + } + ] + } , + "MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" - } - , - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + } + , + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" - } - , - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + } + , + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" - } - , - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] - } - ] + } + , + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] + } + ] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index ab691865396..25f011af40d 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,50 +1,50 @@ { -"Name":"test.dll", -"ComplianceReport": {}, -"MetricReport": - [ - { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - } - , - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - } - , - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } - } - ] - } - ] + "Name":"test.dll", + "ComplianceReport": {} , + "MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } + } + , + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + } + , + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } + } + ] + } + ] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 1fc5c511a1b..37b22a98906 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -1,82 +1,82 @@ { -"Name":"test.dll", -"ComplianceReport": + "Name":"test.dll", + "ComplianceReport": + { + "Types": [ { - "Types": [ + "Name": "Test.LogMethod", + "Logging Methods": [ { - "Name": "Test.LogMethod", - "Logging Methods": [ + "Name": "LogHello", + "Parameters": [ { - "Name": "LogHello", - "Parameters": [ + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 3" - } - ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "14" + "Name": "C2", + "Notes": "Note 3" } ] + }, + { + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "14" } ] } ] - }, -"MetricReport": - [ - { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - } - , - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - } - , - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "MetricEnum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "SomeDim": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5Struct": "Gets or sets Dim5Struct." - } - } - ] - } - ] + } + ] + } , + "MetricReport": + [ + { + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } + } + , + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + } + , + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "MetricEnum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "SomeDim": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5Struct": "Gets or sets Dim5Struct." + } + } + ] + } + ] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json index 0852ab0c072..2e48886d13b 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json @@ -1,204 +1,204 @@ { -"Name":"test.dll", -"ComplianceReport": + "Name":"test.dll", + "ComplianceReport": + { + "Types": [ { - "Types": [ + "Name": "Test.DerivedRecordProperty", + "Members": [ { - "Name": "Test.DerivedRecordProperty", - "Members": [ + "Name": "EqualityContract", + "Type": "System.Type", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ { - "Name": "EqualityContract", - "Type": "System.Type", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C1" - } - ] - }, + "Name": "C1" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "17", + "Classifications": [ { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "17", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "20", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "23", + "Classifications": [ + { + "Name": "C1" }, { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "20", - "Classifications": [ - { - "Name": "C2" - } - ] + "Name": "C2", + "Notes": "Note 2" }, { - "Name": "P0", - "Type": "int", - "File": "src-0.cs", - "Line": "23", - "Classifications": [ - { - "Name": "C1" - }, - { - "Name": "C2", - "Notes": "Note 2" - }, - { - "Name": "C3", - "Notes": "Note 3" - }, - { - "Name": "C4" - } - ] + "Name": "C3", + "Notes": "Note 3" }, { - "Name": "P1", - "Type": "int", - "File": "src-0.cs", - "Line": "26", - "Classifications": [ - { - "Name": "C3" - } - ] + "Name": "C4" } ] }, { - "Name": "Test.RecordProperty", - "Members": [ + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "26", + "Classifications": [ { - "Name": "F0", - "Type": "string", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C2" - } - ] - }, + "Name": "C3" + } + ] + } + ] + }, + { + "Name": "Test.RecordProperty", + "Members": [ + { + "Name": "F0", + "Type": "string", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ { - "Name": "F2", - "Type": "int", - "File": "src-0.cs", - "Line": "14", - "Classifications": [ - { - "Name": "C3" - } - ] - }, + "Name": "C2" + } + ] + }, + { + "Name": "F2", + "Type": "int", + "File": "src-0.cs", + "Line": "14", + "Classifications": [ { - "Name": "F3", - "Type": "int", - "File": "src-0.cs", - "Line": "17", - "Classifications": [ - { - "Name": "C2", - "Notes": "Note 1" - } - ] - }, + "Name": "C3" + } + ] + }, + { + "Name": "F3", + "Type": "int", + "File": "src-0.cs", + "Line": "17", + "Classifications": [ { - "Name": "F4", - "Type": "int", - "File": "src-0.cs", - "Line": "20", - "Classifications": [ - { - "Name": "C2" - } - ] + "Name": "C2", + "Notes": "Note 1" + } + ] + }, + { + "Name": "F4", + "Type": "int", + "File": "src-0.cs", + "Line": "20", + "Classifications": [ + { + "Name": "C2" + } + ] + }, + { + "Name": "P0", + "Type": "int", + "File": "src-0.cs", + "Line": "11", + "Classifications": [ + { + "Name": "C3", + "Notes": "Note 3" }, { - "Name": "P0", - "Type": "int", + "Name": "C4" + } + ] + }, + { + "Name": "P1", + "Type": "int", + "File": "src-0.cs", + "Line": "26", + "Classifications": [ + { + "Name": "C3" + } + ] + } + ], + "Logging Methods": [ + { + "Name": "LogHello", + "Parameters": [ + { + "Name": "user", + "Type": "string", "File": "src-0.cs", - "Line": "11", + "Line": "29", "Classifications": [ { "Name": "C3", "Notes": "Note 3" - }, - { - "Name": "C4" } ] }, { - "Name": "P1", + "Name": "port", "Type": "int", "File": "src-0.cs", - "Line": "26", - "Classifications": [ - { - "Name": "C3" - } - ] + "Line": "29" } - ], - "Logging Methods": [ + ] + }, + { + "Name": "LogWorld", + "Parameters": [ { - "Name": "LogHello", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "29", - "Classifications": [ - { - "Name": "C3", - "Notes": "Note 3" - } - ] - }, + "Name": "user", + "Type": "string", + "File": "src-0.cs", + "Line": "32", + "Classifications": [ { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "29" + "Name": "C2" } ] }, { - "Name": "LogWorld", - "Parameters": [ - { - "Name": "user", - "Type": "string", - "File": "src-0.cs", - "Line": "32", - "Classifications": [ - { - "Name": "C2" - } - ] - }, - { - "Name": "port", - "Type": "int", - "File": "src-0.cs", - "Line": "32" - } - ] + "Name": "port", + "Type": "int", + "File": "src-0.cs", + "Line": "32" } ] } ] - }, -"MetricReport": [] + } + ] + } , + "MetricReport": [] } From a454fed62b2d5cebda0f685234a07e5b6fc1c7db Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Wed, 26 Feb 2025 22:22:57 +0300 Subject: [PATCH 26/42] fix namings --- .../MetadataEmitter.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 86f27fea63c..0f93d50b9bb 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -1,18 +1,21 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; using Microsoft.Gen.ComplianceReports; +using Microsoft.Gen.Metrics.Model; using Microsoft.Gen.MetricsReports; using Microsoft.Gen.Shared; namespace Microsoft.Gen.MetadataExtractor; internal sealed class MetadataEmitter : EmitterBase { + private const int IndentationLevel = 2; private readonly MetricDefinitionEmitter _metricDefinitionEmitter; private readonly ComplianceReportEmitter _complianceReportEmitter; private readonly string _rootNamespace; - private readonly int _indentationLevel = 2; + public MetadataEmitter(string rootNamespace) : base(emitPreamble: false) { @@ -27,7 +30,7 @@ public string Emit(GeneratorExecutionContext context) (string metricReport, string complianceReport) metadataReport = (string.Empty, string.Empty); var receiver = context.SyntaxReceiver as TypeDeclarationSyntaxReceiver; - if (receiver != null) + if (receiver is not null) { metadataReport.metricReport = HandleMetricReportGeneration(context, receiver); metadataReport.complianceReport = HandleComplianceReportGeneration(context, receiver); @@ -35,7 +38,7 @@ public string Emit(GeneratorExecutionContext context) OutLn("{"); Indent(); - OutLn("\"Name\":" + $"\"{context.Compilation.AssemblyName!}\","); + OutLn($"\"Name\":\"{context.Compilation.AssemblyName!}\","); OutIndent(); Out("\"ComplianceReport\": "); Out((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)); @@ -58,8 +61,8 @@ public string Emit(GeneratorExecutionContext context) /// string report as json or String.Empty. private string HandleMetricReportGeneration(GeneratorExecutionContext context, TypeDeclarationSyntaxReceiver receiver) { - var meteringParser = new Metrics.Parser(context.Compilation, context.ReportDiagnostic, context.CancellationToken); - var meteringClasses = meteringParser.GetMetricClasses(receiver.TypeDeclarations); + Metrics.Parser meteringParser = new Metrics.Parser(context.Compilation, context.ReportDiagnostic, context.CancellationToken); + IReadOnlyList meteringClasses = meteringParser.GetMetricClasses(receiver.TypeDeclarations); if (meteringClasses.Count == 0) { @@ -67,9 +70,8 @@ private string HandleMetricReportGeneration(GeneratorExecutionContext context, T } _ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(_rootNamespace, out var rootNamespace); - var reportedMetrics = MetricsReportsHelpers.MapToCommonModel(meteringClasses, rootNamespace); - var report = _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken, _indentationLevel); - return report; + ReportedMetricClass[] reportedMetrics = MetricsReportsHelpers.MapToCommonModel(meteringClasses, rootNamespace); + return _metricDefinitionEmitter.GenerateReport(reportedMetrics, context.CancellationToken, IndentationLevel); } /// @@ -85,15 +87,14 @@ private string HandleComplianceReportGeneration(GeneratorExecutionContext contex return string.Empty; } - var parser = new Parser(context.Compilation, symbolHolder!, context.CancellationToken); - var classifiedTypes = parser.GetClassifiedTypes(receiver.TypeDeclarations); + Parser parser = new Parser(context.Compilation, symbolHolder!, context.CancellationToken); + IReadOnlyList classifiedTypes = parser.GetClassifiedTypes(receiver.TypeDeclarations); if (classifiedTypes.Count == 0) { // nothing to do return string.Empty; } - string report = _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false, _indentationLevel); - return report; + return _complianceReportEmitter.Emit(classifiedTypes, context.Compilation.AssemblyName!, false, IndentationLevel); } } From 6edc0e3a0cc67c0fcc380ab8e00ef4a8c32e8cea Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Wed, 26 Feb 2025 22:33:08 +0300 Subject: [PATCH 27/42] remove white space before comma"ComplianceReport": {}, --- .../Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs | 4 ++-- .../Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json | 2 +- .../GoldenReports/Inheritance.json | 2 +- .../GoldenReports/LogMethod.json | 2 +- .../GoldenReports/MeterAttributedWithXmlDescriptions.json | 2 +- .../MeterAttributedWithXmlDescriptions_RecordProperty.json | 2 +- .../MeterDimensionsAttributedWithXmlDescriptions.json | 2 +- ...eterDimensionsAttributedWithXmlDescriptions_LogMethod.json | 2 +- .../GoldenReports/RecordProperty.json | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 0f93d50b9bb..d14e5625fd2 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -41,8 +41,8 @@ public string Emit(GeneratorExecutionContext context) OutLn($"\"Name\":\"{context.Compilation.AssemblyName!}\","); OutIndent(); Out("\"ComplianceReport\": "); - Out((string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)); - OutLn(","); + Out($"{(string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)},"); + OutLn(); OutIndent(); Out("\"MetricReport\": "); Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json index 6d10cfd1af4..9d7d8881191 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json @@ -117,6 +117,6 @@ ] } ] - } , + }, "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json index 5ef619c51c4..324dabadfde 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json @@ -61,6 +61,6 @@ ] } ] - } , + }, "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json index 3830eb49fb5..252b6e318d4 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json @@ -32,6 +32,6 @@ ] } ] - } , + }, "MetricReport": [] } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index fe75ee79c1d..5ac9bb0e292 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,6 +1,6 @@ { "Name":"test.dll", - "ComplianceReport": {} , + "ComplianceReport": {}, "MetricReport": [ { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 1849f76ed4b..9eb5a8da31a 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -199,7 +199,7 @@ ] } ] - } , + }, "MetricReport": [ { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 25f011af40d..034b18c76df 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,6 +1,6 @@ { "Name":"test.dll", - "ComplianceReport": {} , + "ComplianceReport": {}, "MetricReport": [ { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 37b22a98906..5f592bf9269 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -32,7 +32,7 @@ ] } ] - } , + }, "MetricReport": [ { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json index 2e48886d13b..eed66520827 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json @@ -199,6 +199,6 @@ ] } ] - } , + }, "MetricReport": [] } From 9d414817f571f993d6990753964c6d4b33ab85f0 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Wed, 26 Feb 2025 22:43:15 +0300 Subject: [PATCH 28/42] rename const --- .../MetricDefinitionEmitter.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 8a9f7992a51..366d80e71e7 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -13,13 +13,13 @@ namespace Microsoft.Gen.MetricsReports; internal sealed class MetricDefinitionEmitter : EmitterBase { + private const int IndentLevel = 2; + internal MetricDefinitionEmitter() : base(false) { } - private readonly int _indentLevel = 2; - /// /// Generates JSON object containing the for metrics report. /// @@ -71,7 +71,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat if (metricClass.Methods.Length > 0) { - Indent(_indentLevel); + Indent(IndentLevel); OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) @@ -90,7 +90,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat } OutLn("]"); - Unindent(_indentLevel); + Unindent(IndentLevel); } OutLn("}"); From b893bdcb69966272ab22db121063c1d9c1f1f5c1 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Thu, 27 Feb 2025 07:41:54 +0300 Subject: [PATCH 29/42] fix namings --- .../MetricDefinitionEmitter.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 366d80e71e7..d8ea5ef01bb 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -13,7 +13,8 @@ namespace Microsoft.Gen.MetricsReports; internal sealed class MetricDefinitionEmitter : EmitterBase { - private const int IndentLevel = 2; + private const int RootIndentLevel = 2; + private const int DimensionsIndentLevel = 3; internal MetricDefinitionEmitter() : base(false) @@ -71,7 +72,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat if (metricClass.Methods.Length > 0) { - Indent(IndentLevel); + Indent(RootIndentLevel); OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) @@ -90,7 +91,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat } OutLn("]"); - Unindent(IndentLevel); + Unindent(RootIndentLevel); } OutLn("}"); @@ -123,10 +124,9 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel if (metricMethod.Dimensions.Count > 0) { - const int IndentLevel = 3; OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); OutLn(" \"Dimensions\":"); - Indent(IndentLevel); + Indent(DimensionsIndentLevel); OutLn("{"); Indent(); int k = 0; @@ -147,7 +147,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel Unindent(); OutLn("}"); - Unindent(IndentLevel); + Unindent(DimensionsIndentLevel); } else { From 89c99dc90bcea67cdc7f8b94473bb3eace1f86bd Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Thu, 27 Feb 2025 08:37:52 +0300 Subject: [PATCH 30/42] fix comma on new line --- .../MetricDefinitionEmitter.cs | 23 ++++++++++++++----- .../MeterAttributedWithXmlDescriptions.json | 9 +++----- ...tedWithXmlDescriptions_RecordProperty.json | 9 +++----- ...mensionsAttributedWithXmlDescriptions.json | 6 ++--- ...tributedWithXmlDescriptions_LogMethod.json | 6 ++--- .../MeterAttributedWithXmlDescriptions.json | 9 +++----- ...mensionsAttributedWithXmlDescriptions.json | 6 ++--- .../Unit/EmitterTests.cs | 9 +++----- 8 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index d8ea5ef01bb..0e2d0cf88ae 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -78,13 +78,15 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat for (int j = 0; j < metricClass.Methods.Length; j++) { Indent(); - var metricMethod = metricClass.Methods[j]; + ReportedMetricMethod metricMethod = metricClass.Methods[j]; + bool isLastReportedMetricMethod = (j == metricClass.Methods.Length - 1); - GenMetricMethodDefinition(metricMethod, cancellationToken); + GenMetricMethodDefinition(metricMethod, isLastReportedMetricMethod, cancellationToken); - if (j < metricClass.Methods.Length - 1) + if (!isLastReportedMetricMethod) { - OutLn(","); + Out(","); + OutLn(); } Unindent(); @@ -98,7 +100,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat Unindent(); } - private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, CancellationToken cancellationToken) + private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool isLastReportedMetricMethod, CancellationToken cancellationToken) { switch (metricMethod.Kind) { @@ -154,7 +156,16 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel OutLn(); } - OutLn("}"); + if (isLastReportedMetricMethod) + { + OutLn("}"); + } + else + { + OutIndent(); + Out("}"); + } + } catch (Exception e) { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index 5ac9bb0e292..0422acb301c 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -11,22 +11,19 @@ "MetricDescription": "CounterWithDescription description.", "InstrumentName": "Counter" - } - , + }, { "MetricName": "HistogramWithDescription", "MetricDescription": "HistogramWithDescription description.", "InstrumentName": "Histogram" - } - , + }, { "MetricName": "HistogramWithWrongDescription", "MetricDescription": "(Missing Summary)", "InstrumentName": "Histogram" - } - , + }, { "MetricName": "ConstDescribedCounter", "MetricDescription": "CreateConstDescribedCounter description.", diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 9eb5a8da31a..f7e2be43503 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -210,22 +210,19 @@ "MetricDescription": "CounterWithDescription description.", "InstrumentName": "Counter" - } - , + }, { "MetricName": "HistogramWithDescription", "MetricDescription": "HistogramWithDescription description.", "InstrumentName": "Histogram" - } - , + }, { "MetricName": "HistogramWithWrongDescription", "MetricDescription": "(Missing Summary)", "InstrumentName": "Histogram" - } - , + }, { "MetricName": "ConstDescribedCounter", "MetricDescription": "CreateConstDescribedCounter description.", diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 034b18c76df..e42bdea48f1 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -15,8 +15,7 @@ "Dimension1": "Dimension1 description.", "Dim1": "" } - } - , + }, { "MetricName": "DescribedDimensionHistogram", "MetricDescription": "(Missing Summary)", @@ -26,8 +25,7 @@ "Dimension2": "Dimension2 description.", "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." } - } - , + }, { "MetricName": "MyStrongTypeMetricWithDescription", "MetricDescription": "(Missing Summary)", diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 5f592bf9269..543871129a1 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -47,8 +47,7 @@ "Dimension1": "Dimension1 description.", "Dim1": "" } - } - , + }, { "MetricName": "DescribedDimensionHistogram", "MetricDescription": "(Missing Summary)", @@ -58,8 +57,7 @@ "Dimension2": "Dimension2 description.", "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." } - } - , + }, { "MetricName": "MyStrongTypeMetricWithDescription", "MetricDescription": "(Missing Summary)", diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index 4222c3d822a..49c5d61a2c3 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -7,22 +7,19 @@ "MetricDescription": "CounterWithDescription description.", "InstrumentName": "Counter" - } - , + }, { "MetricName": "HistogramWithDescription", "MetricDescription": "HistogramWithDescription description.", "InstrumentName": "Histogram" - } - , + }, { "MetricName": "HistogramWithWrongDescription", "MetricDescription": "(Missing Summary)", "InstrumentName": "Histogram" - } - , + }, { "MetricName": "ConstDescribedCounter", "MetricDescription": "CreateConstDescribedCounter description.", diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index ab20d639591..ea111376bdf 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -11,8 +11,7 @@ "Dimension1": "Dimension1 description.", "Dim1": "" } - } - , + }, { "MetricName": "DescribedDimensionHistogram", "MetricDescription": "(Missing Summary)", @@ -22,8 +21,7 @@ "Dimension2": "Dimension2 description.", "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." } - } - , + }, { "MetricName": "MyStrongTypeMetricWithDescription", "MetricDescription": "(Missing Summary)", diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index 89a7e822cfc..5ea002ddc76 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -141,8 +141,7 @@ public void EmitterShouldOutputInJSONFormat() ""StatusCode"": ""Status code for request."", ""ErrorCode"": ""Error code for request."" } - } - , + }, { ""MetricName"": ""Latency"", ""MetricDescription"": ""Latency summary."", @@ -151,8 +150,7 @@ public void EmitterShouldOutputInJSONFormat() { ""Dim1"": """" } - } - , + }, { ""MetricName"": ""MemoryUsage"", ""InstrumentName"": ""Gauge"" @@ -169,8 +167,7 @@ public void EmitterShouldOutputInJSONFormat() ""MetricDescription"": ""Counter summary."", ""InstrumentName"": ""Counter"" - } - , + }, { ""MetricName"": ""Test\\MemoryUsage"", ""MetricDescription"": ""MemoryUsage summary."", From 54587822d63cc522f6d900352bddbaa2c55299ae Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Sat, 1 Mar 2025 11:13:43 +0300 Subject: [PATCH 31/42] fix idnentation --- .../MetricDefinitionEmitter.cs | 22 +++---- .../MeterAttributedWithXmlDescriptions.json | 38 ++++++------ ...tedWithXmlDescriptions_RecordProperty.json | 38 ++++++------ ...mensionsAttributedWithXmlDescriptions.json | 62 +++++++++---------- ...tributedWithXmlDescriptions_LogMethod.json | 62 +++++++++---------- .../MeterAttributedWithXmlDescriptions.json | 38 ++++++------ ...mensionsAttributedWithXmlDescriptions.json | 62 +++++++++---------- .../Unit/EmitterTests.cs | 62 +++++++++---------- 8 files changed, 192 insertions(+), 192 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 0e2d0cf88ae..04c666f8e48 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -13,8 +13,7 @@ namespace Microsoft.Gen.MetricsReports; internal sealed class MetricDefinitionEmitter : EmitterBase { - private const int RootIndentLevel = 2; - private const int DimensionsIndentLevel = 3; + private const int DimensionsIndentLevel = 2; internal MetricDefinitionEmitter() : base(false) @@ -67,12 +66,12 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat cancellationToken.ThrowIfCancellationRequested(); Indent(); OutLn("{"); - - OutLn($" \"{metricClass.RootNamespace}\":"); + Indent(); + OutLn($"\"{metricClass.RootNamespace}\":"); if (metricClass.Methods.Length > 0) { - Indent(RootIndentLevel); + Indent(); OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) @@ -92,8 +91,9 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat Unindent(); } + Unindent(); OutLn("]"); - Unindent(RootIndentLevel); + Unindent(); } OutLn("}"); @@ -112,22 +112,22 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool i cancellationToken.ThrowIfCancellationRequested(); OutLn("{"); - OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); + OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); if (!string.IsNullOrEmpty(metricMethod.Summary)) { - OutLn($" \"MetricDescription\": \"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); + OutLn($" \"MetricDescription\": \"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); } if (metricMethod.Dimensions.Count == 0) { - OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\""); + OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\""); } if (metricMethod.Dimensions.Count > 0) { - OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); - OutLn(" \"Dimensions\":"); + OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); + OutLn(" \"Dimensions\":"); Indent(DimensionsIndentLevel); OutLn("{"); Indent(); diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index 0422acb301c..9918d9ce279 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -4,37 +4,37 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" }, { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" }, { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" }, { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index f7e2be43503..8e86e5f622d 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -203,37 +203,37 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" }, { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" }, { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" }, { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index e42bdea48f1..22ccace08bd 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -4,44 +4,44 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } }, { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 543871129a1..0fa8986cdc5 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -36,44 +36,44 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } }, { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "MetricEnum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "SomeDim": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5Struct": "Gets or sets Dim5Struct." - } + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "MetricEnum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "SomeDim": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5Struct": "Gets or sets Dim5Struct." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index 49c5d61a2c3..71875c4515c 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,35 +1,35 @@ [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" }, { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" }, { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" }, { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index ea111376bdf..22dc75b62eb 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,42 +1,42 @@ [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } }, { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index 5ea002ddc76..36c5103cfa5 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -130,54 +130,54 @@ public void EmitterShouldOutputInJSONFormat() { string expected = @"[ { - ""MetricContainingAssembly1"": + ""MetricContainingAssembly1"": [ { - ""MetricName"": ""Requests"", - ""MetricDescription"": ""Requests summary."", - ""InstrumentName"": ""Counter"", - ""Dimensions"": - { - ""StatusCode"": ""Status code for request."", - ""ErrorCode"": ""Error code for request."" - } + ""MetricName"": ""Requests"", + ""MetricDescription"": ""Requests summary."", + ""InstrumentName"": ""Counter"", + ""Dimensions"": + { + ""StatusCode"": ""Status code for request."", + ""ErrorCode"": ""Error code for request."" + } }, { - ""MetricName"": ""Latency"", - ""MetricDescription"": ""Latency summary."", - ""InstrumentName"": ""Histogram"", - ""Dimensions"": - { - ""Dim1"": """" - } + ""MetricName"": ""Latency"", + ""MetricDescription"": ""Latency summary."", + ""InstrumentName"": ""Histogram"", + ""Dimensions"": + { + ""Dim1"": """" + } }, { - ""MetricName"": ""MemoryUsage"", - ""InstrumentName"": ""Gauge"" + ""MetricName"": ""MemoryUsage"", + ""InstrumentName"": ""Gauge"" } - ] + ] } , { - ""MetricContainingAssembly2"": + ""MetricContainingAssembly2"": [ { - ""MetricName"": ""Counter"", - ""MetricDescription"": ""Counter summary."", - ""InstrumentName"": ""Counter"" + ""MetricName"": ""Counter"", + ""MetricDescription"": ""Counter summary."", + ""InstrumentName"": ""Counter"" }, { - ""MetricName"": ""Test\\MemoryUsage"", - ""MetricDescription"": ""MemoryUsage summary."", - ""InstrumentName"": ""Gauge"", - ""Dimensions"": - { - ""Path"": ""Test\\Description\\Path"" - } + ""MetricName"": ""Test\\MemoryUsage"", + ""MetricDescription"": ""MemoryUsage summary."", + ""InstrumentName"": ""Gauge"", + ""Dimensions"": + { + ""Path"": ""Test\\Description\\Path"" + } } - ] + ] } ] "; From cc4c394101dc9978acd2a39ab5b3afddea1a9f72 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Sun, 2 Mar 2025 12:59:54 +0300 Subject: [PATCH 32/42] introduce JsonEmitterBase --- .../ComplianceReportEmitter.cs | 64 +------------ .../Microsoft.Gen.ComplianceReports.csproj | 1 + .../MetadataEmitter.cs | 13 +-- .../Microsoft.Gen.MetadataExtractor.csproj | 1 + .../MetricDefinitionEmitter.cs | 3 +- .../Microsoft.Gen.MetricsReports.csproj | 1 + src/Generators/Shared/EmitterBase.cs | 27 +++++- src/Generators/Shared/JsonEmitterBase.cs | 92 +++++++++++++++++++ .../GoldenReports/Basic.json | 4 +- .../GoldenReports/Inheritance.json | 4 +- .../GoldenReports/LogMethod.json | 4 +- .../MeterAttributedWithXmlDescriptions.json | 4 +- ...tedWithXmlDescriptions_RecordProperty.json | 4 +- ...mensionsAttributedWithXmlDescriptions.json | 4 +- ...tributedWithXmlDescriptions_LogMethod.json | 4 +- .../GoldenReports/RecordProperty.json | 4 +- 16 files changed, 144 insertions(+), 90 deletions(-) create mode 100644 src/Generators/Shared/JsonEmitterBase.cs diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs index 0cee7cc98f5..d6aecf207ae 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs @@ -10,10 +10,9 @@ namespace Microsoft.Gen.ComplianceReports; -internal sealed class ComplianceReportEmitter : EmitterBase +internal sealed class ComplianceReportEmitter : JsonEmitterBase { - private readonly Stack _itemCounts = new(); - private int _itemCount; + public ComplianceReportEmitter() : base(false) @@ -35,7 +34,7 @@ public string Emit(IReadOnlyCollection classifiedTypes, string a Indent(indentationLevel); OutObject(() => { - // this is only for not displaying a name as part of ComplianceReport properties,it should be at the root of the report, defaulted to true for beackward compatibility + // this is only for not displaying a name as part of ComplianceReport properties,it should be at the root of the report, defaulted to true for backward compatibility if (includeName) { OutNameValue("Name", assemblyName); @@ -140,61 +139,4 @@ public string Emit(IReadOnlyCollection classifiedTypes, string a return Capture(); } - private void NewItem() - { - if (_itemCount > 0) - { - Out(","); - } - - OutLn(); - _itemCount++; - } - - private void OutObject(Action action) - { - NewItem(); - _itemCounts.Push(_itemCount); - _itemCount = 0; - - OutIndent(); - Out("{"); - Indent(); - action(); - OutLn(); - Unindent(); - OutIndent(); - Out("}"); - - _itemCount = _itemCounts.Pop(); - } - - private void OutArray(string name, Action action) - { - NewItem(); - _itemCounts.Push(_itemCount); - _itemCount = 0; - - OutIndent(); - Out($"\"{name}\": ["); - Indent(); - action(); - OutLn(); - Unindent(); - OutIndent(); - Out("]"); - - _itemCount = _itemCounts.Pop(); - } - - private void OutNameValue(string name, string value) - { - value = value - .Replace("\\", "\\\\") - .Replace("\"", "\\\""); - - NewItem(); - OutIndent(); - Out($"\"{name}\": \"{value}\""); - } } diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/Microsoft.Gen.ComplianceReports.csproj b/src/Generators/Microsoft.Gen.ComplianceReports/Microsoft.Gen.ComplianceReports.csproj index 0ef9b3d55a6..d5c13b53821 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/Microsoft.Gen.ComplianceReports.csproj +++ b/src/Generators/Microsoft.Gen.ComplianceReports/Microsoft.Gen.ComplianceReports.csproj @@ -21,6 +21,7 @@ + diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index d14e5625fd2..2f0be23b9b6 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Xml.Linq; using Microsoft.CodeAnalysis; using Microsoft.Gen.ComplianceReports; using Microsoft.Gen.Metrics.Model; @@ -9,7 +10,7 @@ using Microsoft.Gen.Shared; namespace Microsoft.Gen.MetadataExtractor; -internal sealed class MetadataEmitter : EmitterBase +internal sealed class MetadataEmitter : JsonEmitterBase { private const int IndentationLevel = 2; private readonly MetricDefinitionEmitter _metricDefinitionEmitter; @@ -36,9 +37,10 @@ public string Emit(GeneratorExecutionContext context) metadataReport.complianceReport = HandleComplianceReportGeneration(context, receiver); } - OutLn("{"); - Indent(); - OutLn($"\"Name\":\"{context.Compilation.AssemblyName!}\","); + OutOpenBrace(isRoot: true); + OutNameValue("Name", context.Compilation.AssemblyName!); + Out(","); + OutLn(); OutIndent(); Out("\"ComplianceReport\": "); Out($"{(string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)},"); @@ -47,8 +49,7 @@ public string Emit(GeneratorExecutionContext context) Out("\"MetricReport\": "); Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); OutLn(); - Unindent(); - OutLn("}"); + OutCloseBrace(isRoot: true); return Capture(); } diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/Microsoft.Gen.MetadataExtractor.csproj b/src/Generators/Microsoft.Gen.MetadataExtractor/Microsoft.Gen.MetadataExtractor.csproj index 667b8383399..1567769b753 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/Microsoft.Gen.MetadataExtractor.csproj +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/Microsoft.Gen.MetadataExtractor.csproj @@ -20,6 +20,7 @@ + diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 04c666f8e48..347fd2fa221 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -11,10 +11,9 @@ namespace Microsoft.Gen.MetricsReports; // Stryker disable all -internal sealed class MetricDefinitionEmitter : EmitterBase +internal sealed class MetricDefinitionEmitter : JsonEmitterBase { private const int DimensionsIndentLevel = 2; - internal MetricDefinitionEmitter() : base(false) { diff --git a/src/Generators/Microsoft.Gen.MetricsReports/Microsoft.Gen.MetricsReports.csproj b/src/Generators/Microsoft.Gen.MetricsReports/Microsoft.Gen.MetricsReports.csproj index ecfe0d4059a..60cf58363ce 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/Microsoft.Gen.MetricsReports.csproj +++ b/src/Generators/Microsoft.Gen.MetricsReports/Microsoft.Gen.MetricsReports.csproj @@ -22,6 +22,7 @@ + diff --git a/src/Generators/Shared/EmitterBase.cs b/src/Generators/Shared/EmitterBase.cs index 92c9e7f2e2c..0da53fad414 100644 --- a/src/Generators/Shared/EmitterBase.cs +++ b/src/Generators/Shared/EmitterBase.cs @@ -34,16 +34,33 @@ public EmitterBase(bool emitPreamble = true) } } - protected void OutOpenBrace() + + protected void OutOpenBrace(bool isRoot = false) // isRoot is used to neglect any extra indentation before the brace, root has no indentation, defaulted to false for backward compatibility. { - OutLn("{"); + if (isRoot) + { + Out("{"); + } + else + { + OutLn("{"); + } + Indent(); } - protected void OutCloseBrace() + protected void OutCloseBrace(bool isRoot = false)// isRoot is used to neglect any extra indentation before the brace, root has no indentation, defaulted to false for backward compatibility. { - Unindent(); - OutLn("}"); + if (isRoot) + { + Out("}"); + } + else + { + OutLn("}"); + } + + Indent(); } protected void OutCloseBraceWithExtra(string extra) diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs new file mode 100644 index 00000000000..c5704574879 --- /dev/null +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -0,0 +1,92 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Text; + +#pragma warning disable CA1716 +namespace Microsoft.Gen.Shared; +#pragma warning restore CA1716 + +#if !SHARED_PROJECT +[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] +#endif +internal class JsonEmitterBase : EmitterBase +{ + private readonly Stack _itemCounts = new(); + private int _itemCount; + public JsonEmitterBase(bool emitPreamble = true) + : base(emitPreamble) + { + } + + protected void OutObject(Action action) + { + NewItem(); + _itemCounts.Push(_itemCount); + _itemCount = 0; + + OutIndent(); + Out("{"); + Indent(); + action(); + OutLn(); + Unindent(); + OutIndent(); + Out("}"); + + _itemCount = _itemCounts.Pop(); + } + + protected void OutArray(string name, Action action) + { + NewItem(); + _itemCounts.Push(_itemCount); + _itemCount = 0; + + OutIndent(); + Out($"\"{name}\": ["); + Indent(); + action(); + OutLn(); + Unindent(); + OutIndent(); + Out("]"); + + _itemCount = _itemCounts.Pop(); + } + + protected void OutNameValue(string name, string value) + { + value = value + .Replace("\\", "\\\\") + .Replace("\"", "\\\""); + + NewItem(); + OutIndent(); + Out($"\"{name}\": \"{value}\""); + } + + protected void OutEmptyObject() + { + Out("{}"); + } + + protected void OutEmptyArray() + { + Out("[]"); + } + + private void NewItem() + { + if (_itemCount > 0) + { + Out(","); + } + + OutLn(); + _itemCount++; + } + +} diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json index 9d7d8881191..e89e01aea80 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": { "Types": [ @@ -119,4 +119,4 @@ ] }, "MetricReport": [] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json index 324dabadfde..30cec2b8abe 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": { "Types": [ @@ -63,4 +63,4 @@ ] }, "MetricReport": [] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json index 252b6e318d4..7bbfe055143 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": { "Types": [ @@ -34,4 +34,4 @@ ] }, "MetricReport": [] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index 9918d9ce279..f379b204604 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": {}, "MetricReport": [ @@ -38,4 +38,4 @@ } ] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 8e86e5f622d..afcc98eda51 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": { "Types": [ @@ -237,4 +237,4 @@ } ] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 22ccace08bd..b67b435088e 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": {}, "MetricReport": [ @@ -45,4 +45,4 @@ } ] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 0fa8986cdc5..2ebffddb1ca 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": { "Types": [ @@ -77,4 +77,4 @@ } ] -} +} \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json index eed66520827..94b41856925 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json @@ -1,5 +1,5 @@ { - "Name":"test.dll", + "Name": "test.dll", "ComplianceReport": { "Types": [ @@ -201,4 +201,4 @@ ] }, "MetricReport": [] -} +} \ No newline at end of file From bf7af23bf22040218ee6439da861a5022bc242ee Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Sun, 2 Mar 2025 13:12:23 +0300 Subject: [PATCH 33/42] remove blanks --- .../Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs | 2 -- src/Generators/Shared/EmitterBase.cs | 1 - src/Generators/Shared/JsonEmitterBase.cs | 1 - 3 files changed, 4 deletions(-) diff --git a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs index d6aecf207ae..bd891e22301 100644 --- a/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs +++ b/src/Generators/Microsoft.Gen.ComplianceReports/ComplianceReportEmitter.cs @@ -12,8 +12,6 @@ namespace Microsoft.Gen.ComplianceReports; internal sealed class ComplianceReportEmitter : JsonEmitterBase { - - public ComplianceReportEmitter() : base(false) { diff --git a/src/Generators/Shared/EmitterBase.cs b/src/Generators/Shared/EmitterBase.cs index 0da53fad414..ab3eff8467b 100644 --- a/src/Generators/Shared/EmitterBase.cs +++ b/src/Generators/Shared/EmitterBase.cs @@ -34,7 +34,6 @@ public EmitterBase(bool emitPreamble = true) } } - protected void OutOpenBrace(bool isRoot = false) // isRoot is used to neglect any extra indentation before the brace, root has no indentation, defaulted to false for backward compatibility. { if (isRoot) diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index c5704574879..e984e95c546 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Text; #pragma warning disable CA1716 namespace Microsoft.Gen.Shared; From 0d4c1021386e329bf3cdd71d4d07b2f07e210509 Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Sun, 2 Mar 2025 13:43:05 +0300 Subject: [PATCH 34/42] overload OutNameValue --- .../MetricDefinitionEmitter.cs | 25 ++++---- .../MeterAttributedWithXmlDescriptions.json | 38 ++++++------ ...tedWithXmlDescriptions_RecordProperty.json | 38 ++++++------ ...mensionsAttributedWithXmlDescriptions.json | 62 +++++++++---------- ...tributedWithXmlDescriptions_LogMethod.json | 62 +++++++++---------- .../MeterAttributedWithXmlDescriptions.json | 38 ++++++------ ...mensionsAttributedWithXmlDescriptions.json | 62 +++++++++---------- .../Unit/EmitterTests.cs | 62 +++++++++---------- 8 files changed, 194 insertions(+), 193 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 347fd2fa221..0e2d0cf88ae 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -11,9 +11,11 @@ namespace Microsoft.Gen.MetricsReports; // Stryker disable all -internal sealed class MetricDefinitionEmitter : JsonEmitterBase +internal sealed class MetricDefinitionEmitter : EmitterBase { - private const int DimensionsIndentLevel = 2; + private const int RootIndentLevel = 2; + private const int DimensionsIndentLevel = 3; + internal MetricDefinitionEmitter() : base(false) { @@ -65,12 +67,12 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat cancellationToken.ThrowIfCancellationRequested(); Indent(); OutLn("{"); - Indent(); - OutLn($"\"{metricClass.RootNamespace}\":"); + + OutLn($" \"{metricClass.RootNamespace}\":"); if (metricClass.Methods.Length > 0) { - Indent(); + Indent(RootIndentLevel); OutLn("["); for (int j = 0; j < metricClass.Methods.Length; j++) @@ -90,9 +92,8 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat Unindent(); } - Unindent(); OutLn("]"); - Unindent(); + Unindent(RootIndentLevel); } OutLn("}"); @@ -111,22 +112,22 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool i cancellationToken.ThrowIfCancellationRequested(); OutLn("{"); - OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); + OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); if (!string.IsNullOrEmpty(metricMethod.Summary)) { - OutLn($" \"MetricDescription\": \"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); + OutLn($" \"MetricDescription\": \"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); } if (metricMethod.Dimensions.Count == 0) { - OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\""); + OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\""); } if (metricMethod.Dimensions.Count > 0) { - OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); - OutLn(" \"Dimensions\":"); + OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); + OutLn(" \"Dimensions\":"); Indent(DimensionsIndentLevel); OutLn("{"); Indent(); diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index f379b204604..1416459a5ed 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -4,37 +4,37 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" }, { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" }, { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" }, { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index afcc98eda51..5e96efa383f 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -203,37 +203,37 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" }, { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" }, { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" }, { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index b67b435088e..5387981de31 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -4,44 +4,44 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } }, { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 2ebffddb1ca..210807adffa 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -36,44 +36,44 @@ "MetricReport": [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } }, { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "MetricEnum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "SomeDim": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5Struct": "Gets or sets Dim5Struct." - } + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "MetricEnum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "SomeDim": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5Struct": "Gets or sets Dim5Struct." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index 71875c4515c..49c5d61a2c3 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,35 +1,35 @@ [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" }, { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" }, { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" }, { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "InstrumentName": "Counter", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 22dc75b62eb..ea111376bdf 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,42 +1,42 @@ [ { - "TestClasses": + "TestClasses": [ { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "Dimension1": "Dimension1 description.", + "Dim1": "" + } }, { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Counter", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } } - ] + ] } ] diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index 36c5103cfa5..5ea002ddc76 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -130,54 +130,54 @@ public void EmitterShouldOutputInJSONFormat() { string expected = @"[ { - ""MetricContainingAssembly1"": + ""MetricContainingAssembly1"": [ { - ""MetricName"": ""Requests"", - ""MetricDescription"": ""Requests summary."", - ""InstrumentName"": ""Counter"", - ""Dimensions"": - { - ""StatusCode"": ""Status code for request."", - ""ErrorCode"": ""Error code for request."" - } + ""MetricName"": ""Requests"", + ""MetricDescription"": ""Requests summary."", + ""InstrumentName"": ""Counter"", + ""Dimensions"": + { + ""StatusCode"": ""Status code for request."", + ""ErrorCode"": ""Error code for request."" + } }, { - ""MetricName"": ""Latency"", - ""MetricDescription"": ""Latency summary."", - ""InstrumentName"": ""Histogram"", - ""Dimensions"": - { - ""Dim1"": """" - } + ""MetricName"": ""Latency"", + ""MetricDescription"": ""Latency summary."", + ""InstrumentName"": ""Histogram"", + ""Dimensions"": + { + ""Dim1"": """" + } }, { - ""MetricName"": ""MemoryUsage"", - ""InstrumentName"": ""Gauge"" + ""MetricName"": ""MemoryUsage"", + ""InstrumentName"": ""Gauge"" } - ] + ] } , { - ""MetricContainingAssembly2"": + ""MetricContainingAssembly2"": [ { - ""MetricName"": ""Counter"", - ""MetricDescription"": ""Counter summary."", - ""InstrumentName"": ""Counter"" + ""MetricName"": ""Counter"", + ""MetricDescription"": ""Counter summary."", + ""InstrumentName"": ""Counter"" }, { - ""MetricName"": ""Test\\MemoryUsage"", - ""MetricDescription"": ""MemoryUsage summary."", - ""InstrumentName"": ""Gauge"", - ""Dimensions"": - { - ""Path"": ""Test\\Description\\Path"" - } + ""MetricName"": ""Test\\MemoryUsage"", + ""MetricDescription"": ""MemoryUsage summary."", + ""InstrumentName"": ""Gauge"", + ""Dimensions"": + { + ""Path"": ""Test\\Description\\Path"" + } } - ] + ] } ] "; From 53eff192b5f3447c2b8de12b7108b5123c409b07 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Sun, 2 Mar 2025 19:51:53 +0300 Subject: [PATCH 35/42] rerun --- .../Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs | 4 +--- src/Generators/Shared/JsonEmitterBase.cs | 9 +++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 2f0be23b9b6..54f24004fdc 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -38,9 +38,7 @@ public string Emit(GeneratorExecutionContext context) } OutOpenBrace(isRoot: true); - OutNameValue("Name", context.Compilation.AssemblyName!); - Out(","); - OutLn(); + OutNameValue("Name", context.Compilation.AssemblyName!, isSingle: true); OutIndent(); Out("\"ComplianceReport\": "); Out($"{(string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)},"); diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index e984e95c546..61f060275b5 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -56,7 +56,7 @@ protected void OutArray(string name, Action action) _itemCount = _itemCounts.Pop(); } - protected void OutNameValue(string name, string value) + protected void OutNameValue(string name, string value, bool isSingle = false) { value = value .Replace("\\", "\\\\") @@ -64,7 +64,12 @@ protected void OutNameValue(string name, string value) NewItem(); OutIndent(); - Out($"\"{name}\": \"{value}\""); + Out($"\"{name}\": \"{value}\"{(isSingle ? "," : string.Empty)}"); + + if (isSingle) + { + OutLn(); + } } protected void OutEmptyObject() From ac2996d3e434067e863fc54965b8bf283a51a071 Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Sun, 2 Mar 2025 20:13:15 +0300 Subject: [PATCH 36/42] made metadata consume OutObject --- .../MetadataEmitter.cs | 23 +++++++++---------- src/Generators/Shared/JsonEmitterBase.cs | 6 ++--- .../GoldenReports/Basic.json | 1 + .../GoldenReports/Inheritance.json | 1 + .../GoldenReports/LogMethod.json | 1 + .../MeterAttributedWithXmlDescriptions.json | 1 + ...tedWithXmlDescriptions_RecordProperty.json | 1 + ...mensionsAttributedWithXmlDescriptions.json | 1 + ...tributedWithXmlDescriptions_LogMethod.json | 1 + .../GoldenReports/RecordProperty.json | 1 + 10 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs index 54f24004fdc..db3fcd25a54 100644 --- a/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetadataExtractor/MetadataEmitter.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Xml.Linq; using Microsoft.CodeAnalysis; using Microsoft.Gen.ComplianceReports; using Microsoft.Gen.Metrics.Model; @@ -37,17 +36,17 @@ public string Emit(GeneratorExecutionContext context) metadataReport.complianceReport = HandleComplianceReportGeneration(context, receiver); } - OutOpenBrace(isRoot: true); - OutNameValue("Name", context.Compilation.AssemblyName!, isSingle: true); - OutIndent(); - Out("\"ComplianceReport\": "); - Out($"{(string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)},"); - OutLn(); - OutIndent(); - Out("\"MetricReport\": "); - Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); - OutLn(); - OutCloseBrace(isRoot: true); + OutObject(() => + { + OutNameValue("Name", context.Compilation.AssemblyName!, isSingle: true); + OutIndent(); + Out("\"ComplianceReport\": "); + Out($"{(string.IsNullOrEmpty(metadataReport.complianceReport) ? "{}" : metadataReport.complianceReport)},"); + OutLn(); + OutIndent(); + Out("\"MetricReport\": "); + Out((string.IsNullOrEmpty(metadataReport.metricReport) ? "[]" : metadataReport.metricReport)); + }); return Capture(); } diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index 61f060275b5..62594d255a4 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -62,7 +62,7 @@ protected void OutNameValue(string name, string value, bool isSingle = false) .Replace("\\", "\\\\") .Replace("\"", "\\\""); - NewItem(); + NewItem(preAppendComma: !isSingle); OutIndent(); Out($"\"{name}\": \"{value}\"{(isSingle ? "," : string.Empty)}"); @@ -82,9 +82,9 @@ protected void OutEmptyArray() Out("[]"); } - private void NewItem() + private void NewItem(bool preAppendComma = true) { - if (_itemCount > 0) + if (preAppendComma && _itemCount > 0) { Out(","); } diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json index e89e01aea80..fdbddc7eab1 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Basic.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json index 30cec2b8abe..dd524d8ba03 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/Inheritance.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json index 7bbfe055143..46b49b8569f 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/LogMethod.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index 1416459a5ed..fd91eea42ff 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": {}, diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 5e96efa383f..1c003aeea82 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 5387981de31..204555f39b2 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": {}, diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 210807adffa..586b0823eab 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json index 94b41856925..00db417a23b 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/RecordProperty.json @@ -1,3 +1,4 @@ + { "Name": "test.dll", "ComplianceReport": From befb637ba3892892f8887277c5aaa7b2bad2378d Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Sun, 2 Mar 2025 20:16:27 +0300 Subject: [PATCH 37/42] rerun --- src/Generators/Shared/JsonEmitterBase.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index 62594d255a4..eb947c44c2c 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -11,6 +11,7 @@ namespace Microsoft.Gen.Shared; #if !SHARED_PROJECT [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] #endif + internal class JsonEmitterBase : EmitterBase { private readonly Stack _itemCounts = new(); From 7317e694ef342130242ac8d39dc34cd56ba432ad Mon Sep 17 00:00:00 2001 From: Ibrahim Nada Date: Sun, 2 Mar 2025 20:52:42 +0300 Subject: [PATCH 38/42] 1 --- .../MetricDefinitionEmitter.cs | 30 +++++++++---------- src/Generators/Shared/JsonEmitterBase.cs | 9 ++++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 0e2d0cf88ae..80e020e1a68 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -11,7 +11,7 @@ namespace Microsoft.Gen.MetricsReports; // Stryker disable all -internal sealed class MetricDefinitionEmitter : EmitterBase +internal sealed class MetricDefinitionEmitter : JsonEmitterBase { private const int RootIndentLevel = 2; private const int DimensionsIndentLevel = 3; @@ -40,24 +40,22 @@ public string GenerateReport(IReadOnlyList metricClasses, C OutLn(); } - Indent(indentationLevel); - OutLn("["); - - for (int i = 0; i < metricClasses.Count; i++) + OutArray(string.Empty, () => { - cancellationToken.ThrowIfCancellationRequested(); - var metricClass = metricClasses[i]; + for (int i = 0; i < metricClasses.Count; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + var metricClass = metricClasses[i]; - GenMetricClassDefinition(metricClass, cancellationToken); + GenMetricClassDefinition(metricClass, cancellationToken); - if (i < metricClasses.Count - 1) - { - OutLn(","); + if (i < metricClasses.Count - 1) + { + OutLn(","); + } } - } - OutLn("]"); - Unindent(indentationLevel); + }); return Capture(); } @@ -65,7 +63,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C private void GenMetricClassDefinition(ReportedMetricClass metricClass, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - Indent(); + //Indent(); OutLn("{"); OutLn($" \"{metricClass.RootNamespace}\":"); @@ -97,7 +95,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat } OutLn("}"); - Unindent(); + // Unindent(); } private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool isLastReportedMetricMethod, CancellationToken cancellationToken) diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index eb947c44c2c..6bb1b52da27 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -45,8 +45,13 @@ protected void OutArray(string name, Action action) _itemCounts.Push(_itemCount); _itemCount = 0; - OutIndent(); - Out($"\"{name}\": ["); + if (!string.IsNullOrEmpty(name)) + { + OutIndent(); + } + + Out($"{(string.IsNullOrEmpty(name) ? "[" : "\"{name}\": [)")}"); + OutLn(); Indent(); action(); OutLn(); From 5eb1f9b8078e9297c537cdfa752fbd94f0c70e0b Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Mon, 3 Mar 2025 11:13:46 +0300 Subject: [PATCH 39/42] metric reports --- .../MetricDefinitionEmitter.cs | 126 +++++++----------- src/Generators/Shared/JsonEmitterBase.cs | 12 +- .../MeterAttributedWithXmlDescriptions.json | 62 +++++---- ...mensionsAttributedWithXmlDescriptions.json | 76 +++++------ .../Unit/EmitterTests.cs | 88 ++++++------ 5 files changed, 164 insertions(+), 200 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 80e020e1a68..b342eb8f35b 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -13,8 +13,7 @@ namespace Microsoft.Gen.MetricsReports; internal sealed class MetricDefinitionEmitter : JsonEmitterBase { - private const int RootIndentLevel = 2; - private const int DimensionsIndentLevel = 3; + private const int IndentLevel = 2; internal MetricDefinitionEmitter() : base(false) @@ -37,7 +36,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C if (indentationLevel > 0) { - OutLn(); + Indent(indentationLevel); } OutArray(string.Empty, () => @@ -46,9 +45,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C { cancellationToken.ThrowIfCancellationRequested(); var metricClass = metricClasses[i]; - GenMetricClassDefinition(metricClass, cancellationToken); - if (i < metricClasses.Count - 1) { OutLn(","); @@ -57,45 +54,45 @@ public string GenerateReport(IReadOnlyList metricClasses, C }); + if (indentationLevel > 0) + { + Unindent(indentationLevel); + } + return Capture(); } private void GenMetricClassDefinition(ReportedMetricClass metricClass, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); - //Indent(); - OutLn("{"); - OutLn($" \"{metricClass.RootNamespace}\":"); - - if (metricClass.Methods.Length > 0) + OutObject(() => { - Indent(RootIndentLevel); - OutLn("["); + OutLn(); + OutIndent(); + Out($"\"{metricClass.RootNamespace}\":"); - for (int j = 0; j < metricClass.Methods.Length; j++) + if (metricClass.Methods.Length > 0) { - Indent(); - ReportedMetricMethod metricMethod = metricClass.Methods[j]; - bool isLastReportedMetricMethod = (j == metricClass.Methods.Length - 1); - - GenMetricMethodDefinition(metricMethod, isLastReportedMetricMethod, cancellationToken); - - if (!isLastReportedMetricMethod) + OutIndent(); + OutArray(string.Empty, () => { - Out(","); - OutLn(); - } + for (int j = 0; j < metricClass.Methods.Length; j++) + { + ReportedMetricMethod metricMethod = metricClass.Methods[j]; + bool isLastReportedMetricMethod = (j == metricClass.Methods.Length - 1); + Indent(IndentLevel); + GenMetricMethodDefinition(metricMethod, isLastReportedMetricMethod, cancellationToken); + Unindent(IndentLevel); + } - Unindent(); + Indent(IndentLevel); + }); + Unindent(IndentLevel); } - OutLn("]"); - Unindent(RootIndentLevel); - } + }); - OutLn("}"); - // Unindent(); } private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool isLastReportedMetricMethod, CancellationToken cancellationToken) @@ -108,61 +105,36 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool i try { cancellationToken.ThrowIfCancellationRequested(); - OutLn("{"); - OutLn($" \"MetricName\": \"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); - - if (!string.IsNullOrEmpty(metricMethod.Summary)) + OutObject(() => { - OutLn($" \"MetricDescription\": \"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}\","); - } + OutNameValue("MetricName", $"{metricMethod.MetricName.Replace("\\", "\\\\").Replace("\"", "\\\"")}"); - if (metricMethod.Dimensions.Count == 0) - { - OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\""); - } + if (!string.IsNullOrEmpty(metricMethod.Summary)) + { + OutNameValue("MetricDescription", $"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}"); + } + if (metricMethod.Dimensions.Count == 0) + { + OutNameValue($"InstrumentName", $"{metricMethod.Kind}"); + } - if (metricMethod.Dimensions.Count > 0) - { - OutLn($" \"InstrumentName\": \"{metricMethod.Kind}\","); - OutLn(" \"Dimensions\":"); - Indent(DimensionsIndentLevel); - OutLn("{"); - Indent(); - int k = 0; - foreach (var dimension in metricMethod.Dimensions) + if (metricMethod.Dimensions.Count > 0) { - bool commaCondition = k < metricMethod.Dimensions.Count - 1; - if (metricMethod.DimensionsDescriptions.TryGetValue(dimension, out var description)) + Out("\"Dimensions\":"); + OutIndent(); + OutObject(() => { - OutLn($"\"{dimension}\": \"{description.Replace("\\", "\\\\").Replace("\"", "\\\"")}\"{(commaCondition ? "," : string.Empty)}"); - } - else - { - OutLn($"\"{dimension}\": \"\"{(commaCondition ? "," : string.Empty)}"); - } - - k++; + foreach (var dimension in metricMethod.Dimensions) + { + if (metricMethod.DimensionsDescriptions.TryGetValue(dimension, out var description)) + { + OutNameValue($"{dimension}", $"{description.Replace("\\", "\\\\").Replace("\"", "\\\"")}"); + } + } + }); } - - Unindent(); - OutLn("}"); - Unindent(DimensionsIndentLevel); - } - else - { - OutLn(); - } - - if (isLastReportedMetricMethod) - { - OutLn("}"); - } - else - { - OutIndent(); - Out("}"); - } + }); } catch (Exception e) diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index 6bb1b52da27..6ff1c992234 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -45,13 +45,17 @@ protected void OutArray(string name, Action action) _itemCounts.Push(_itemCount); _itemCount = 0; - if (!string.IsNullOrEmpty(name)) + OutIndent(); + + if (string.IsNullOrEmpty(name)) { - OutIndent(); + OutLn("["); + } + else + { + Out($"\"{name}\": ["); } - Out($"{(string.IsNullOrEmpty(name) ? "[" : "\"{name}\": [)")}"); - OutLn(); Indent(); action(); OutLn(); diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index 49c5d61a2c3..9d699e73526 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,35 +1,33 @@ -[ - { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" + +[ - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" + { + "TestClasses": + [ - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + }, + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + }, + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + }, + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.""Dimensions": , + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] } -] +] \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index ea111376bdf..938c357dd49 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,42 +1,38 @@ -[ + +[ + { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - }, - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } - } - ] + "TestClasses": + [ + + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)""Dimensions": , + { + "Dimension1": "Dimension1 description." + } + }, + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)""Dimensions": , + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + }, + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)""Dimensions": , + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } + } + ] } -] +] \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index 5ea002ddc76..dc71b1e4d35 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -128,59 +128,53 @@ public void GetMetricClassDefinition_GivenMetricTypeIsUnknown_ThrowsNotSupported [Fact] public void EmitterShouldOutputInJSONFormat() { - string expected = @"[ + string expected = @" +[ + { - ""MetricContainingAssembly1"": - [ - { - ""MetricName"": ""Requests"", - ""MetricDescription"": ""Requests summary."", - ""InstrumentName"": ""Counter"", - ""Dimensions"": - { - ""StatusCode"": ""Status code for request."", - ""ErrorCode"": ""Error code for request."" - } - }, - { - ""MetricName"": ""Latency"", - ""MetricDescription"": ""Latency summary."", - ""InstrumentName"": ""Histogram"", - ""Dimensions"": - { - ""Dim1"": """" - } - }, - { - ""MetricName"": ""MemoryUsage"", - ""InstrumentName"": ""Gauge"" + ""MetricContainingAssembly1"": + [ - } - ] - } + { + ""MetricName"": ""Requests"", + ""MetricDescription"": ""Requests summary.""""Dimensions"": , + { + ""StatusCode"": ""Status code for request."", + ""ErrorCode"": ""Error code for request."" + } + }, + { + ""MetricName"": ""Latency"", + ""MetricDescription"": ""Latency summary.""""Dimensions"": , + { + } + }, + { + ""MetricName"": ""MemoryUsage"", + ""InstrumentName"": ""Gauge"" + } + ] + } , , { - ""MetricContainingAssembly2"": - [ - { - ""MetricName"": ""Counter"", - ""MetricDescription"": ""Counter summary."", - ""InstrumentName"": ""Counter"" + ""MetricContainingAssembly2"": + [ - }, - { - ""MetricName"": ""Test\\MemoryUsage"", - ""MetricDescription"": ""MemoryUsage summary."", - ""InstrumentName"": ""Gauge"", - ""Dimensions"": - { - ""Path"": ""Test\\Description\\Path"" - } - } - ] + { + ""MetricName"": ""Counter"", + ""MetricDescription"": ""Counter summary."", + ""InstrumentName"": ""Counter"" + }, + { + ""MetricName"": ""Test\\\\MemoryUsage"", + ""MetricDescription"": ""MemoryUsage summary.""""Dimensions"": , + { + ""Path"": ""Test\\\\Description\\\\Path"" + } + } + ] } -] -"; +]"; var emitter = new MetricDefinitionEmitter(); string json = emitter.GenerateReport(_metricClasses, CancellationToken.None); From 48a06477d44fe270b93dde055f7b3b4508c8978b Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Mon, 3 Mar 2025 11:52:10 +0300 Subject: [PATCH 40/42] metrics --- .../MetricDefinitionEmitter.cs | 14 ++++--- src/Generators/Shared/JsonEmitterBase.cs | 4 +- .../MeterAttributedWithXmlDescriptions.json | 11 ++--- ...mensionsAttributedWithXmlDescriptions.json | 41 ++++++++++--------- .../Unit/EmitterTests.cs | 27 ++++++------ 5 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index b342eb8f35b..15ad36d7858 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -80,9 +80,8 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat for (int j = 0; j < metricClass.Methods.Length; j++) { ReportedMetricMethod metricMethod = metricClass.Methods[j]; - bool isLastReportedMetricMethod = (j == metricClass.Methods.Length - 1); Indent(IndentLevel); - GenMetricMethodDefinition(metricMethod, isLastReportedMetricMethod, cancellationToken); + GenMetricMethodDefinition(metricMethod, cancellationToken); Unindent(IndentLevel); } @@ -95,7 +94,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, Cancellat } - private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool isLastReportedMetricMethod, CancellationToken cancellationToken) + private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, CancellationToken cancellationToken) { switch (metricMethod.Kind) { @@ -114,6 +113,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool i { OutNameValue("MetricDescription", $"{metricMethod.Summary.Replace("\\", "\\\\").Replace("\"", "\\\"")}"); } + if (metricMethod.Dimensions.Count == 0) { OutNameValue($"InstrumentName", $"{metricMethod.Kind}"); @@ -121,8 +121,11 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool i if (metricMethod.Dimensions.Count > 0) { - Out("\"Dimensions\":"); + Out(","); + OutLn(); OutIndent(); + Out("\"Dimensions\":"); + Indent(); OutObject(() => { foreach (var dimension in metricMethod.Dimensions) @@ -132,7 +135,8 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, bool i OutNameValue($"{dimension}", $"{description.Replace("\\", "\\\\").Replace("\"", "\\\"")}"); } } - }); + }, true); + Unindent(); } }); diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index 6ff1c992234..63501d89102 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -21,9 +21,9 @@ public JsonEmitterBase(bool emitPreamble = true) { } - protected void OutObject(Action action) + protected void OutObject(Action action, bool proprietyObject = false) { - NewItem(); + NewItem(!proprietyObject); _itemCounts.Push(_itemCount); _itemCount = 0; diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index 9d699e73526..0ffa0238857 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -22,11 +22,12 @@ }, { "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.""Dimensions": , - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "MetricDescription": "CreateConstDescribedCounter description.", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } } ] } diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 938c357dd49..c338dfeb40a 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -7,31 +7,34 @@ { "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)""Dimensions": , - { - "Dimension1": "Dimension1 description." - } + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "Dimension1": "Dimension1 description." + } }, { "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)""Dimensions": , - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } }, { "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)""Dimensions": , - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } } ] } diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index dc71b1e4d35..630a9a86fbb 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -137,17 +137,19 @@ public void EmitterShouldOutputInJSONFormat() { ""MetricName"": ""Requests"", - ""MetricDescription"": ""Requests summary.""""Dimensions"": , - { - ""StatusCode"": ""Status code for request."", - ""ErrorCode"": ""Error code for request."" - } + ""MetricDescription"": ""Requests summary."", + ""Dimensions"": + { + ""StatusCode"": ""Status code for request."", + ""ErrorCode"": ""Error code for request."" + } }, { ""MetricName"": ""Latency"", - ""MetricDescription"": ""Latency summary.""""Dimensions"": , - { - } + ""MetricDescription"": ""Latency summary."", + ""Dimensions"": + { + } }, { ""MetricName"": ""MemoryUsage"", @@ -167,10 +169,11 @@ public void EmitterShouldOutputInJSONFormat() }, { ""MetricName"": ""Test\\\\MemoryUsage"", - ""MetricDescription"": ""MemoryUsage summary.""""Dimensions"": , - { - ""Path"": ""Test\\\\Description\\\\Path"" - } + ""MetricDescription"": ""MemoryUsage summary."", + ""Dimensions"": + { + ""Path"": ""Test\\\\Description\\\\Path"" + } } ] } From bb917df9c91e65653935a2cbcf3356d39aae62eb Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Mon, 3 Mar 2025 13:28:44 +0300 Subject: [PATCH 41/42] apply json emitter --- .../MetricDefinitionEmitter.cs | 23 +----- src/Generators/Shared/JsonEmitterBase.cs | 10 +-- .../MeterAttributedWithXmlDescriptions.json | 59 +++++++-------- ...tedWithXmlDescriptions_RecordProperty.json | 59 +++++++-------- ...mensionsAttributedWithXmlDescriptions.json | 73 +++++++++---------- ...tributedWithXmlDescriptions_LogMethod.json | 73 +++++++++---------- .../MeterAttributedWithXmlDescriptions.json | 52 +++++++------ ...mensionsAttributedWithXmlDescriptions.json | 62 ++++++++-------- .../Unit/EmitterTests.cs | 71 +++++++++--------- 9 files changed, 219 insertions(+), 263 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 15ad36d7858..8e0b9a640f7 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -13,8 +13,6 @@ namespace Microsoft.Gen.MetricsReports; internal sealed class MetricDefinitionEmitter : JsonEmitterBase { - private const int IndentLevel = 2; - internal MetricDefinitionEmitter() : base(false) { @@ -45,53 +43,38 @@ public string GenerateReport(IReadOnlyList metricClasses, C { cancellationToken.ThrowIfCancellationRequested(); var metricClass = metricClasses[i]; - GenMetricClassDefinition(metricClass, cancellationToken); + GenMetricClassDefinition(metricClass, indentationLevel, cancellationToken); if (i < metricClasses.Count - 1) { OutLn(","); } } - }); - if (indentationLevel > 0) - { - Unindent(indentationLevel); - } - return Capture(); } - private void GenMetricClassDefinition(ReportedMetricClass metricClass, CancellationToken cancellationToken) + private void GenMetricClassDefinition(ReportedMetricClass metricClass, int indentationLevel = 0, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); - OutObject(() => { OutLn(); OutIndent(); Out($"\"{metricClass.RootNamespace}\":"); - if (metricClass.Methods.Length > 0) { - OutIndent(); OutArray(string.Empty, () => { for (int j = 0; j < metricClass.Methods.Length; j++) { ReportedMetricMethod metricMethod = metricClass.Methods[j]; - Indent(IndentLevel); GenMetricMethodDefinition(metricMethod, cancellationToken); - Unindent(IndentLevel); } - Indent(IndentLevel); - }); - Unindent(IndentLevel); + }, proprietyDependent: true); } - }); - } private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, CancellationToken cancellationToken) diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index 63501d89102..e8f536664b3 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -21,9 +21,9 @@ public JsonEmitterBase(bool emitPreamble = true) { } - protected void OutObject(Action action, bool proprietyObject = false) + protected void OutObject(Action action, bool proprietyDependent = false) { - NewItem(!proprietyObject); + NewItem(!proprietyDependent); _itemCounts.Push(_itemCount); _itemCount = 0; @@ -39,9 +39,9 @@ protected void OutObject(Action action, bool proprietyObject = false) _itemCount = _itemCounts.Pop(); } - protected void OutArray(string name, Action action) + protected void OutArray(string name, Action action, bool proprietyDependent = false) { - NewItem(); + NewItem(!proprietyDependent); _itemCounts.Push(_itemCount); _itemCount = 0; @@ -49,7 +49,7 @@ protected void OutArray(string name, Action action) if (string.IsNullOrEmpty(name)) { - OutLn("["); + Out("["); } else { diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json index fd91eea42ff..43e133eb80f 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -5,38 +5,33 @@ "MetricReport": [ { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" - - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" - - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + }, + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + }, + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + }, + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] } ] - } \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json index 1c003aeea82..6f1cae5548b 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterAttributedWithXmlDescriptions_RecordProperty.json @@ -204,38 +204,33 @@ "MetricReport": [ { - "TestClasses": - [ - { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" - - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" - - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "InstrumentName": "Counter", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } - } - ] + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + }, + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + }, + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + }, + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "Dimensions": + { + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." + } + } + ] } ] - } \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index 204555f39b2..4fb3652da9b 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -5,45 +5,40 @@ "MetricReport": [ { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - }, - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } - } - ] + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "Dimension1": "Dimension1 description." + } + }, + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + }, + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." + } + } + ] } ] - } \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json index 586b0823eab..227127c3b0f 100644 --- a/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json +++ b/test/Generators/Microsoft.Gen.MetadataExtractor/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions_LogMethod.json @@ -37,45 +37,40 @@ "MetricReport": [ { - "TestClasses": - [ - { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "Dimension1": "Dimension1 description.", - "Dim1": "" - } - }, - { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, - { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Counter", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "MetricEnum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "SomeDim": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5Struct": "Gets or sets Dim5Struct." - } - } - ] + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "Dimension1": "Dimension1 description." + } + }, + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + }, + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "Dimensions": + { + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "MetricEnum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "SomeDim": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5Struct": "Gets or sets Dim5Struct." + } + } + ] } ] - } \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json index 0ffa0238857..9e114dc8120 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterAttributedWithXmlDescriptions.json @@ -1,34 +1,32 @@  [ - { - "TestClasses": - [ - + "TestClasses": + [ + { + "MetricName": "CounterWithDescription", + "MetricDescription": "CounterWithDescription description.", + "InstrumentName": "Counter" + }, + { + "MetricName": "HistogramWithDescription", + "MetricDescription": "HistogramWithDescription description.", + "InstrumentName": "Histogram" + }, + { + "MetricName": "HistogramWithWrongDescription", + "MetricDescription": "(Missing Summary)", + "InstrumentName": "Histogram" + }, + { + "MetricName": "ConstDescribedCounter", + "MetricDescription": "CreateConstDescribedCounter description.", + "Dimensions": { - "MetricName": "CounterWithDescription", - "MetricDescription": "CounterWithDescription description.", - "InstrumentName": "Counter" - }, - { - "MetricName": "HistogramWithDescription", - "MetricDescription": "HistogramWithDescription description.", - "InstrumentName": "Histogram" - }, - { - "MetricName": "HistogramWithWrongDescription", - "MetricDescription": "(Missing Summary)", - "InstrumentName": "Histogram" - }, - { - "MetricName": "ConstDescribedCounter", - "MetricDescription": "CreateConstDescribedCounter description.", - "Dimensions": - { - "Dim4": "Dim4 description.", - "InClassDim": "InClassDim description." - } + "Dim4": "Dim4 description.", + "InClassDim": "InClassDim description." } - ] + } + ] } ] \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json index c338dfeb40a..8ce840dc38b 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json +++ b/test/Generators/Microsoft.Gen.MetricsReports/GoldenReports/MeterDimensionsAttributedWithXmlDescriptions.json @@ -1,41 +1,39 @@  [ - { - "TestClasses": - [ - + "TestClasses": + [ + { + "MetricName": "DescribedDimensionCounter", + "MetricDescription": "(Missing Summary)", + "Dimensions": { - "MetricName": "DescribedDimensionCounter", - "MetricDescription": "(Missing Summary)", - "Dimensions": - { - "Dimension1": "Dimension1 description." - } - }, + "Dimension1": "Dimension1 description." + } + }, + { + "MetricName": "DescribedDimensionHistogram", + "MetricDescription": "(Missing Summary)", + "Dimensions": { - "MetricName": "DescribedDimensionHistogram", - "MetricDescription": "(Missing Summary)", - "Dimensions": - { - "Dimension2": "Dimension2 description.", - "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." - } - }, + "Dimension2": "Dimension2 description.", + "DimensionDefinedInMetricClass": "DimensionDefinedInMetricClass description." + } + }, + { + "MetricName": "MyStrongTypeMetricWithDescription", + "MetricDescription": "(Missing Summary)", + "Dimensions": { - "MetricName": "MyStrongTypeMetricWithDescription", - "MetricDescription": "(Missing Summary)", - "Dimensions": - { - "AnotherDimension": "Gets or sets anotherDimension.", - "MetricEnum": "Gets or sets MetricEnum.", - "Enum2": "Gets or sets MetricEnum2.", - "Dim2": "Gets or sets Dim2.", - "dim2FromAttribute": "Gets or sets SomeDim.", - "Dim4Struct": "Gets or sets Dim4Struct.", - "Dim5FromAttribute": "Gets or sets Dim5Struct." - } + "AnotherDimension": "Gets or sets anotherDimension.", + "MetricEnum": "Gets or sets MetricEnum.", + "Enum2": "Gets or sets MetricEnum2.", + "Dim2": "Gets or sets Dim2.", + "dim2FromAttribute": "Gets or sets SomeDim.", + "Dim4Struct": "Gets or sets Dim4Struct.", + "Dim5FromAttribute": "Gets or sets Dim5Struct." } - ] + } + ] } ] \ No newline at end of file diff --git a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs index 630a9a86fbb..5beba806643 100644 --- a/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs +++ b/test/Generators/Microsoft.Gen.MetricsReports/Unit/EmitterTests.cs @@ -130,52 +130,49 @@ public void EmitterShouldOutputInJSONFormat() { string expected = @" [ - { - ""MetricContainingAssembly1"": - [ - - { - ""MetricName"": ""Requests"", - ""MetricDescription"": ""Requests summary."", - ""Dimensions"": - { - ""StatusCode"": ""Status code for request."", - ""ErrorCode"": ""Error code for request."" - } - }, + ""MetricContainingAssembly1"": + [ + { + ""MetricName"": ""Requests"", + ""MetricDescription"": ""Requests summary."", + ""Dimensions"": { - ""MetricName"": ""Latency"", - ""MetricDescription"": ""Latency summary."", - ""Dimensions"": - { - } - }, + ""StatusCode"": ""Status code for request."", + ""ErrorCode"": ""Error code for request."" + } + }, + { + ""MetricName"": ""Latency"", + ""MetricDescription"": ""Latency summary."", + ""Dimensions"": { - ""MetricName"": ""MemoryUsage"", - ""InstrumentName"": ""Gauge"" } - ] + }, + { + ""MetricName"": ""MemoryUsage"", + ""InstrumentName"": ""Gauge"" + } + ] } , , { - ""MetricContainingAssembly2"": - [ - - { - ""MetricName"": ""Counter"", - ""MetricDescription"": ""Counter summary."", - ""InstrumentName"": ""Counter"" - }, + ""MetricContainingAssembly2"": + [ + { + ""MetricName"": ""Counter"", + ""MetricDescription"": ""Counter summary."", + ""InstrumentName"": ""Counter"" + }, + { + ""MetricName"": ""Test\\\\MemoryUsage"", + ""MetricDescription"": ""MemoryUsage summary."", + ""Dimensions"": { - ""MetricName"": ""Test\\\\MemoryUsage"", - ""MetricDescription"": ""MemoryUsage summary."", - ""Dimensions"": - { - ""Path"": ""Test\\\\Description\\\\Path"" - } + ""Path"": ""Test\\\\Description\\\\Path"" } - ] + } + ] } ]"; From c8eb0df53581d3daeebed15bc4f8ab591209f20c Mon Sep 17 00:00:00 2001 From: IBRAHIM NADA Date: Mon, 3 Mar 2025 13:42:09 +0300 Subject: [PATCH 42/42] fixing spaces --- .../MetricDefinitionEmitter.cs | 8 +++--- src/Generators/Shared/EmitterBase.cs | 3 +++ src/Generators/Shared/JsonEmitterBase.cs | 26 +++++++++---------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs index 8e0b9a640f7..e2faa5b3e2f 100644 --- a/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs +++ b/src/Generators/Microsoft.Gen.MetricsReports/MetricDefinitionEmitter.cs @@ -43,7 +43,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C { cancellationToken.ThrowIfCancellationRequested(); var metricClass = metricClasses[i]; - GenMetricClassDefinition(metricClass, indentationLevel, cancellationToken); + GenMetricClassDefinition(metricClass, cancellationToken); if (i < metricClasses.Count - 1) { OutLn(","); @@ -54,7 +54,7 @@ public string GenerateReport(IReadOnlyList metricClasses, C return Capture(); } - private void GenMetricClassDefinition(ReportedMetricClass metricClass, int indentationLevel = 0, CancellationToken cancellationToken = default) + private void GenMetricClassDefinition(ReportedMetricClass metricClass, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); OutObject(() => @@ -72,7 +72,7 @@ private void GenMetricClassDefinition(ReportedMetricClass metricClass, int inden GenMetricMethodDefinition(metricMethod, cancellationToken); } - }, proprietyDependent: true); + }, isProprietyDependent: true); } }); } @@ -118,7 +118,7 @@ private void GenMetricMethodDefinition(ReportedMetricMethod metricMethod, Cancel OutNameValue($"{dimension}", $"{description.Replace("\\", "\\\\").Replace("\"", "\\\"")}"); } } - }, true); + }, isProprietyDependent: true); Unindent(); } }); diff --git a/src/Generators/Shared/EmitterBase.cs b/src/Generators/Shared/EmitterBase.cs index ab3eff8467b..ad7ca65bc10 100644 --- a/src/Generators/Shared/EmitterBase.cs +++ b/src/Generators/Shared/EmitterBase.cs @@ -13,6 +13,9 @@ namespace Microsoft.Gen.Shared; #endif internal class EmitterBase { + protected readonly Stack ItemCounts = new(); + protected int ItemCount; + private const int DefaultStringBuilderCapacity = 1024; private const int IndentChars = 4; diff --git a/src/Generators/Shared/JsonEmitterBase.cs b/src/Generators/Shared/JsonEmitterBase.cs index e8f536664b3..6de2f5f6287 100644 --- a/src/Generators/Shared/JsonEmitterBase.cs +++ b/src/Generators/Shared/JsonEmitterBase.cs @@ -14,18 +14,16 @@ namespace Microsoft.Gen.Shared; internal class JsonEmitterBase : EmitterBase { - private readonly Stack _itemCounts = new(); - private int _itemCount; public JsonEmitterBase(bool emitPreamble = true) : base(emitPreamble) { } - protected void OutObject(Action action, bool proprietyDependent = false) + protected void OutObject(Action action, bool isProprietyDependent = false) { - NewItem(!proprietyDependent); - _itemCounts.Push(_itemCount); - _itemCount = 0; + NewItem(!isProprietyDependent); + ItemCounts.Push(ItemCount); + ItemCount = 0; OutIndent(); Out("{"); @@ -36,14 +34,14 @@ protected void OutObject(Action action, bool proprietyDependent = false) OutIndent(); Out("}"); - _itemCount = _itemCounts.Pop(); + ItemCount = ItemCounts.Pop(); } - protected void OutArray(string name, Action action, bool proprietyDependent = false) + protected void OutArray(string name, Action action, bool isProprietyDependent = false) { - NewItem(!proprietyDependent); - _itemCounts.Push(_itemCount); - _itemCount = 0; + NewItem(!isProprietyDependent); + ItemCounts.Push(ItemCount); + ItemCount = 0; OutIndent(); @@ -63,7 +61,7 @@ protected void OutArray(string name, Action action, bool proprietyDependent = fa OutIndent(); Out("]"); - _itemCount = _itemCounts.Pop(); + ItemCount = ItemCounts.Pop(); } protected void OutNameValue(string name, string value, bool isSingle = false) @@ -94,13 +92,13 @@ protected void OutEmptyArray() private void NewItem(bool preAppendComma = true) { - if (preAppendComma && _itemCount > 0) + if (preAppendComma && ItemCount > 0) { Out(","); } OutLn(); - _itemCount++; + ItemCount++; } }