Skip to content

Commit 16b2c75

Browse files
committed
Report duplicate enums via log and prevent rendering them twice
1 parent dfb5d0a commit 16b2c75

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/WebApiToTypeScript/Enums/EnumsService.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Mono.Cecil;
1+
using System.Linq;
2+
using Mono.Cecil;
23
using System.Collections.Generic;
34
using System.Linq;
45
using WebApiToTypeScript.Block;
@@ -10,6 +11,9 @@ public class EnumsService : ServiceAware
1011
private List<TypeDefinition> Enums { get; }
1112
= new List<TypeDefinition>();
1213

14+
private Dictionary<string, string> LogMessages { get; }
15+
= new Dictionary<string, string>();
16+
1317
public TypeScriptBlock CreateEnumsBlock()
1418
{
1519
return Config.GenerateEnums
@@ -19,19 +23,28 @@ public TypeScriptBlock CreateEnumsBlock()
1923

2024
public void AddEnum(TypeDefinition thingType)
2125
{
22-
// todo-balki duplicate enums detection
23-
// fullname might be the issue here, since we are actually going by final namespaced name
24-
// but we might want to detect that AFTER adding all unique types so we can complain correctly what conflicted
25-
2626
if (Enums.All(e => e.FullName != thingType.FullName))
27-
Enums.Add(thingType);
27+
{
28+
var conflictEnum = Enums.SingleOrDefault(e => e.Name == thingType.Name);
29+
if (conflictEnum == null)
30+
{
31+
Enums.Add(thingType);
32+
}
33+
else
34+
{
35+
LogMessages[thingType.Name] = $"Enum [{thingType.Name}] of type [{thingType.FullName}] conflicts with [{conflictEnum.FullName}]";
36+
}
37+
}
2838
}
2939

3040
public TypeScriptBlock WriteEnumsToBlock(TypeScriptBlock enumsBlock)
3141
{
3242
foreach (var typeDefinition in Enums)
3343
CreateEnumForType(enumsBlock, typeDefinition);
3444

45+
foreach (var logMessage in LogMessages)
46+
LogMessage(logMessage.Value);
47+
3548
return enumsBlock;
3649
}
3750

0 commit comments

Comments
 (0)