Skip to content

Commit

Permalink
Add AttributeCollection.AddOrReplace
Browse files Browse the repository at this point in the history
  • Loading branch information
JonSaffron committed Jun 21, 2024
1 parent 0202a2e commit 21083b3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
24 changes: 24 additions & 0 deletions FacadeFor3e.Tests/AttributeCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ public void SetAnItem()
ClassicAssert.AreEqual(n, coll[0]);
}

[Test]
public void TestSetOrReplace()
{
var coll = new AttributeCollection
{
new NamedAttributeValue("boolean", new BoolAttribute(true)),
new NamedAttributeValue("integer", new IntAttribute(25)),
new NamedAttributeValue("guid", new GuidAttribute(Guid.NewGuid()))
};

var g = new NamedAttributeValue("guid", new BoolAttribute(true));
var i = new NamedAttributeValue("integer", new IntAttribute(25));
var t = new NamedAttributeValue("date", new DateTimeAttribute(DateTime.Now));

coll.AddOrReplace(g);
coll.AddOrReplace(i);
coll.AddOrReplace(t);

ClassicAssert.AreEqual(4, coll.Count);
ClassicAssert.AreEqual(g, coll["guid"]);
ClassicAssert.AreEqual(i, coll["integer"]);
ClassicAssert.AreEqual(t, coll["date"]);
}

[Test]
public void GetErrors()
{
Expand Down
2 changes: 1 addition & 1 deletion FacadeFor3e.Tests/FacadeFor3e.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NLog" Version="5.3.2" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion FacadeFor3e/FacadeFor3e.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageId>FacadeFor3E</PackageId>
<Authors>J Saffron Consulting</Authors>
<Product>FacadeFor3E</Product>
<Version>4.0.0-rc3</Version>
<Version>4.0.0</Version>
<Description>A library that makes integrating with Elite 3E a little easier</Description>
<Copyright>Copyright © J Saffron Consulting Ltd 2014 - 2024</Copyright>
<RepositoryUrl>https://github.com/JonSaffron/FacadeFor3e</RepositoryUrl>
Expand Down
30 changes: 23 additions & 7 deletions FacadeFor3e/ProcessCommandBuilder/AttributeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public NamedAttributeValue this[string name]
{
if (name == null)
throw new ArgumentNullException(nameof(name));
// ReSharper disable once PossibleNullReferenceException
var result = this.SingleOrDefault(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (result == null)
throw new KeyNotFoundException("An attribute could not be found with the specified name.");
Expand All @@ -42,18 +41,39 @@ public bool TryGetValue(string name, [NotNullWhen(true)] out NamedAttributeValue
{
if (name == null)
throw new ArgumentNullException(nameof(name));
// ReSharper disable once PossibleNullReferenceException
namedAttribute = this.SingleOrDefault(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
return namedAttribute != null;
}

/// <summary>
/// Adds the specified attribute to the collection, or replaces it if an attribute with the same name is already present
/// </summary>
/// <param name="namedAttribute">The attribute to add or to replace</param>
/// <exception cref="ArgumentNullException">If <see cref="namedAttribute"/> is null</exception>
public void AddOrReplace(NamedAttributeValue namedAttribute)
{
if (namedAttribute == null) throw new ArgumentNullException(nameof(namedAttribute));
for (int i = 0; i < this.Count; i++)
{
var a = this[i];

if (a.Name.Equals(namedAttribute.Name, StringComparison.OrdinalIgnoreCase))
{
// ReSharper disable once RedundantBaseQualifier
this.SetItem(i, namedAttribute);
return;
}
}

base.Add(namedAttribute);
}

/// <inheritdoc />
// ReSharper disable once AnnotationConflictInHierarchy
protected override void InsertItem(int index, NamedAttributeValue item)
{
if (item == null)
throw new ArgumentNullException(nameof(item));
// ReSharper disable once PossibleNullReferenceException
if (this.Any(a => a.Name.Equals(item.Name, StringComparison.OrdinalIgnoreCase)))
throw new ArgumentOutOfRangeException($"An attribute with the name {item.Name} has already been added.");
base.InsertItem(index, item);
Expand All @@ -71,8 +91,6 @@ protected override void SetItem(int index, NamedAttributeValue item)
continue;

var a = this[i];
if (a == null)
throw new InvalidOperationException();
if (a.Name.Equals(item.Name, StringComparison.OrdinalIgnoreCase))
throw new ArgumentOutOfRangeException($"An attribute with the name {item.Name} already exists.");
}
Expand All @@ -94,8 +112,6 @@ public bool Remove(string name)
{
var a = this[i];

if (a == null)
throw new InvalidOperationException();
if (a.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
// ReSharper disable once RedundantBaseQualifier
Expand Down
6 changes: 5 additions & 1 deletion FacadeFor3e/ReleaseHistory.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
4.0.0-rc3
4.0.0
-----
Added AddOrReplace to AttributeCollection to serve a particular use case

4.0.0-rc3
---------
Change the ODataServiceResult.ErrorMesssages property to make it less brittle and enable it to cope with errors caused by rate limiting
Updated the json deserialiser to be case-insensitive and to cache the options object
Expand Down

0 comments on commit 21083b3

Please sign in to comment.