From 754fce9f436739562f23a8f93f6cf325f5e12c1f Mon Sep 17 00:00:00 2001 From: Nils Henrik Hals <3185998+Strepto@users.noreply.github.com> Date: Thu, 12 Jun 2025 15:14:41 +0200 Subject: [PATCH] Better diagnostics for duplicate headers --- .../Attributes/ScaffoldingAttributeParser.cs | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/CadRevealFbxProvider/Attributes/ScaffoldingAttributeParser.cs b/CadRevealFbxProvider/Attributes/ScaffoldingAttributeParser.cs index 0e4458d5..a4c164da 100644 --- a/CadRevealFbxProvider/Attributes/ScaffoldingAttributeParser.cs +++ b/CadRevealFbxProvider/Attributes/ScaffoldingAttributeParser.cs @@ -192,19 +192,45 @@ private static string[] RemoveCsvNonDescriptionHeaderInfo(string[] fileLines) private static ICsvLine[] ConvertToCsvLines(string[] fileLines) { - return CsvReader - .ReadFromText( - String.Join(Environment.NewLine, fileLines), - new CsvOptions() - { - HeaderMode = HeaderMode.HeaderPresent, - RowsToSkip = 0, - SkipRow = (ReadOnlyMemory row, int idx) => row.Span.IsEmpty || row.Span[0] == '#' || idx == 2, - TrimData = true, - Separator = ';', - } - ) - .ToArray(); + const char expectedSeparator = ';'; + try + { + return CsvReader + .ReadFromText( + String.Join(Environment.NewLine, fileLines), + new CsvOptions() + { + HeaderMode = HeaderMode.HeaderPresent, + RowsToSkip = 0, + SkipRow = (ReadOnlyMemory row, int idx) => + row.Span.IsEmpty || row.Span[0] == '#' || idx == 2, + TrimData = true, + Separator = expectedSeparator, + } + ) + .ToArray(); + } + catch (Exception e) + { + if (e.Message.Contains("Duplicate headers detected in HeaderPresent mode.")) + { + var headerRow = fileLines.First(); + Console.WriteLine( + "The first row of the CSV file is expected to contain unique headers. Current headers: \n " + + headerRow + ); + + var duplicateHeaders = headerRow + .Split(expectedSeparator) + .GroupBy(h => h.Trim()) + .Where(g => g.Count() > 1) + .Select(g => g.Key) + .ToList(); + Console.WriteLine("Duplicate headers found: " + string.Join(", ", duplicateHeaders)); + Console.WriteLine("This should not happen, the scaffolding file is invalid."); + } + throw; + } } private static int RetrieveKeyAttributeColumnIndex(ICsvLine[] attributeRawData)