Skip to content

Commit

Permalink
Use a hashset to prevent attributes from being added twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Oct 23, 2024
1 parent bb86d8a commit 6f7ecec
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ public void parameter_sets()
null, KeyValuePair.Create("Author", (object?)"Qowaiv"))
.Should().HaveContent("[NUnit.Framework.Test(Author = \"Qowaiv\")]\r\n");
}

public class Equaly
{
[Test]
public void by_value()
{
var attr = new AttributeInfo(typeof(TestAttribute),
null, KeyValuePair.Create("Author", (object?)"Qowaiv"));

var ottr = new AttributeInfo(typeof(TestAttribute),
null, KeyValuePair.Create("Author", (object?)"Qowaiv"));

attr.Equals(ottr).Should().BeTrue();
}
}
3 changes: 3 additions & 0 deletions src/Qowaiv.CodeGeneration.OpenApi/Collections/Empty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ internal static class Empty
{
[Pure]
public static List<T> List<T>() => [];

[Pure]
public static HashSet<T> HashSet<T>() => [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ internal static class TypeInfoExtensions
{
public static void AddAttributes(this TypeInfo info, IEnumerable<AttributeInfo> attributes)
{
var list = (List<AttributeInfo>)info.Attributes;
list.AddRange(attributes);
var hashset = (HashSet<AttributeInfo>)info.Attributes;
foreach (var attribute in attributes)
{
hashset.Add(attribute);
}
}

[Diagnostics.Contracts.CollectionMutation]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static bool IsNone(EnumerationField field)
IsPartial = Settings.Partial,
Properties = Empty.List<PropertyInfo>(),
DerivedTypes = Empty.List<Type>(),
Attributes = Empty.List<AttributeInfo>(),
Attributes = Empty.HashSet<AttributeInfo>(),
Visibility = ResolveVisibility(schema),
Documentation = new XmlDocumentation() { Summary = schema.Description },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.0.1-alpha-021</Version>
<Version>0.0.1-alpha-022</Version>
<PackageId>Qowaiv.CodeGeneration.OpenApi</PackageId>
<PackageReleaseNotes>
<![CDATA[
Expand Down
2 changes: 1 addition & 1 deletion src/Qowaiv.CodeGeneration/Qowaiv.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.0.1-alpha-021</Version>
<Version>0.0.1-alpha-022</Version>
<PackageId>Qowaiv.CodeGeneration</PackageId>
<PackageReleaseNotes>
<![CDATA[
Expand Down
8 changes: 8 additions & 0 deletions src/Qowaiv.CodeGeneration/Syntax/AttributeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using DotNetProjectFile.MsBuild;

namespace Qowaiv.CodeGeneration.Syntax;

/// <summary>Represents an attribute decoration.</summary>
Expand Down Expand Up @@ -114,4 +116,10 @@ public override int GetHashCode()

return hash;
}

/// <summary>Returns true if both have the same values.</summary>
public static bool operator ==(AttributeInfo? l, AttributeInfo? r) => l is null ? r is null : l.Equals(r);

/// <summary>Returns false if both have the same values.</summary>
public static bool operator !=(AttributeInfo? l, AttributeInfo? r) => !(l == r);
}

0 comments on commit 6f7ecec

Please sign in to comment.