Skip to content

Commit

Permalink
implement FinalElementAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
akopetsch committed Jul 12, 2024
1 parent 709a177 commit 6a1d5c5
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.IO;
using ByteSerialization.Tests.Integration.Attributes.FinalElement.TestObjects;
using Xunit;
using Xunit.Abstractions;

namespace ByteSerialization.Tests.Integration.Attributes.FinalElement
{
public class FinalElementAttributeTest(ITestOutputHelper testOutputHelper) :
IntegrationTestBase(testOutputHelper)
{
#region Fields

private static readonly byte[] TestBytes =
HexStringConverter.ToByteArray("01 02 03 FF 05");

private static readonly ListContainer TestObject = new()
{
Bytes = [1, 2, 3, 0xFF],
EndByte = 5,
};

#endregion

#region Methods

[Fact]
public void Test_Deserialization() =>
AssertDeserializedObject(
TestObject, TestBytes, Endianness.BigEndian);

[Fact]
public void Test_Serialization() =>
AssertSerializedObject(
TestBytes, TestObject, Endianness.BigEndian);

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.Attributes;
using ByteSerialization.IO;

namespace ByteSerialization.Tests.Integration.Attributes.FinalElement.TestObjects
{
internal class ListContainer
{
#region Properties

[Order(0), FinalElement(byte.MaxValue)]
public List<byte> Bytes { get; set; }
[Order(1)]
public byte EndByte { get; set; }

#endregion

#region Methods (: object)

public override bool Equals(object obj)
{
if (obj is ListContainer other)
{
if (!Enumerable.SequenceEqual(Bytes, other.Bytes))
return false;
if (!Equals(EndByte, other.EndByte))
return false;
return true;
}
else
return false;
}

public override int GetHashCode() =>
HashCode.Combine(
HashCodeHelper.CombineHashCodes(Bytes?.ToArray()),
EndByte);

#endregion
}
}

This file was deleted.

29 changes: 29 additions & 0 deletions ByteSerialization/Attributes/FinalElementAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.Components.Attributes.Limiting;
using System;

namespace ByteSerialization.Attributes
{
[AttributeComponent(typeof(FinalElementComponent))]
[AttributeUsage(AttributeTargets.Property)]
public class FinalElementAttribute : ByteSerializationAttribute
{
#region Properties

public object Value { get; }
public Type ValueType { get; }

#endregion

#region Constructor

public FinalElementAttribute(object value) =>
Value = value;

public FinalElementAttribute(Type valueType) =>
ValueType = valueType;

#endregion
}
}
2 changes: 1 addition & 1 deletion ByteSerialization/ByteSerialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>disable</Nullable>
<Authors>Alexander Kopetsch</Authors>
<Version>0.0.1.0</Version>
<PackageVersion>0.0.1-alpha.9</PackageVersion>
<PackageVersion>0.0.1-alpha.10</PackageVersion>
<PackageProjectUrl>https://github.com/akopetsch/ByteSerialization</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT

using ByteSerialization.Attributes;
using ByteSerialization.Attributes.Limiting;
using ByteSerialization.Components.Values.Composites.Collections;
using System;
using System.Linq;

namespace ByteSerialization.Components.Attributes.Limiting
{
public class FinalElementComponent :
AttributeComponent<FinalElementAttribute>, ILimitingComponent
{
#region Properties

private object _expectedFinalElementValue;

#endregion

#region Methods

protected override void OnInitialized()
{
base.OnInitialized();
_expectedFinalElementValue = Attribute.Value ?? Activator.CreateInstance(Attribute.ValueType);
}

public bool IsDeserializedUntilEnd(CollectionComponent collection) =>
collection.Elements.LastOrDefault()?.Value.Equals(_expectedFinalElementValue) ?? false;

#endregion
}
}

0 comments on commit 6a1d5c5

Please sign in to comment.