Skip to content

Commit

Permalink
Don't fail if an invalid duplicate attribute is present
Browse files Browse the repository at this point in the history
Even if the compilation will fail, the generator can still be invoked with duplicate assembly-level attributes.

Fixes #78
  • Loading branch information
kzu committed Oct 21, 2021
1 parent a66c0c4 commit c86881a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ThisAssembly.AssemblyInfo/AssemblyInfoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void Execute(GeneratorExecutionContext context)
.Where(x => !string.IsNullOrEmpty(x.AttributeClass?.Name) && attributes.Contains(x.AttributeClass!.Name))
.Where(x => x.ConstructorArguments.Length == 1)
.Select(x => new KeyValuePair<string, string?>(x.AttributeClass!.Name.Substring(8).Replace("Attribute", ""), (string?)x.ConstructorArguments[0].Value))
.Distinct(new KeyPairComparer())
.ToDictionary(x => x.Key, x => x.Value ?? "");

var model = new Model(metadata);
Expand All @@ -43,5 +44,12 @@ public void Execute(GeneratorExecutionContext context)

context.AddSource("ThisAssembly.Info", SourceText.From(output, Encoding.UTF8));
}

class KeyPairComparer : IEqualityComparer<KeyValuePair<string, string?>>
{
public bool Equals(KeyValuePair<string, string?> x, KeyValuePair<string, string?> y) => x.Key == y.Key;

public int GetHashCode(KeyValuePair<string, string?> obj) => obj.Key.GetHashCode();
}
}
}

0 comments on commit c86881a

Please sign in to comment.