Исправления чтения списков

This commit is contained in:
2025-02-25 17:09:04 +03:00
parent ac5eded147
commit d1a4d6f97e
5 changed files with 51 additions and 19 deletions
@@ -11,7 +11,7 @@ using mROA.Implementation.Attributes;
namespace mROA.Cbor namespace mROA.Cbor
{ {
public class CborSerializaitonToolkit : IContextualSerializationToolKit public class CborSerializationToolkit : IContextualSerializationToolKit
{ {
public byte[] Serialize(object objectToSerialize, IEndPointContext context) public byte[] Serialize(object objectToSerialize, IEndPointContext context)
{ {
@@ -122,9 +122,6 @@ namespace mROA.Cbor
private void WriteList(IList list, CborWriter writer, IEndPointContext context) private void WriteList(IList list, CborWriter writer, IEndPointContext context)
{ {
writer.WriteStartArray(list.Count); writer.WriteStartArray(list.Count);
foreach (var element in list) foreach (var element in list)
@@ -193,10 +190,10 @@ namespace mROA.Cbor
if (type.IsSubclassOf(typeof(ISharedObject))) if (type.IsSubclassOf(typeof(ISharedObject)))
return ReadSharedObject(reader, type, context); return ReadSharedObject(reader, type, context);
if (type.IsSubclassOf(typeof(IList)) || type.IsArray) if (typeof(IList).IsAssignableFrom(type) || type.IsArray)
return ReadList(reader, type, context); return ReadList(reader, type, context);
return ReadObject(reader, type, context); return ReadObject(reader, type, context);
case CborReaderState.StartMap: case CborReaderState.StartMap:
@@ -216,21 +213,27 @@ namespace mROA.Cbor
var length = reader.ReadStartArray(); var length = reader.ReadStartArray();
if (length != null) if (length != null)
{ {
var values = new object[length.Value]; var elementType = typeof(object);
Type elementType = typeof(object);
if (type is { IsArray: true }) if (type is { IsArray: true })
elementType = type.GetElementType(); elementType = type.GetElementType();
else if (typeof(IList).IsAssignableFrom(type))
elementType = type.GetGenericArguments()[0];
Array values = Array.CreateInstance(elementType, length.Value);
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
values[i] = ReadData(reader, elementType, context); values.SetValue(ReadData(reader, elementType, context), i);
} }
reader.ReadEndArray();
return values; return values;
} }
reader.ReadEndArray();
return Array.Empty<object>(); return Array.Empty<object>();
} }
+5 -4
View File
@@ -14,7 +14,9 @@ public class CborTest
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
_complexTestObject = new () _basicCollectionElement = new() { A = 567565, B = "test text", C = 2.781f };
_complexTestObject = new ComplexTestObject
{ {
IntValue = 123, IntValue = 123,
DoubleValue = 3.14159, DoubleValue = 3.14159,
@@ -26,8 +28,7 @@ public class CborTest
], ],
IntArray = [1, 4, 8, 16, 87] IntArray = [1, 4, 8, 16, 87]
}; };
_basicCollectionElement = new() { A = 567565, B = "test text", C = 2.781f }; _serializationToolKit = new CborSerializationToolkit();
_serializationToolKit = new CborSerializaitonToolkit();
} }
[Test] [Test]
@@ -69,7 +70,7 @@ public class CborTest
public double DoubleValue { get; set; } public double DoubleValue { get; set; }
public string StringValue { get; set; } public string StringValue { get; set; }
public int[] IntArray { get; set; } public int[] IntArray { get; set; }
public BasicCollectionElement[] CollectionElements { get; set; } public List<BasicCollectionElement> CollectionElements { get; set; }
protected bool Equals(ComplexTestObject other) protected bool Equals(ComplexTestObject other)
{ {
+1
View File
@@ -5,5 +5,6 @@
IContextRepository RealRepository { get; } IContextRepository RealRepository { get; }
IContextRepository RemoteRepository { get; } IContextRepository RemoteRepository { get; }
int HostId { get; } int HostId { get; }
int OwnerId { get; }
} }
} }
+20
View File
@@ -0,0 +1,20 @@
using System;
using mROA.Abstract;
namespace mROA.Implementation
{
public class EndPointContext : IEndPointContext
{
public Func<int> OwnerFunc;
public IContextRepository RealRepository { get; set; }
public IContextRepository RemoteRepository { get; set; }
public int HostId { get; set; }
public int OwnerId
{
get => OwnerFunc();
set
{
OwnerFunc = () => value;
} }
}
}
+11 -4
View File
@@ -1,6 +1,7 @@
using System; using System;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using mROA.Abstract; using mROA.Abstract;
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
#pragma warning disable CS8618, CS9264 #pragma warning disable CS8618, CS9264
@@ -20,7 +21,8 @@ namespace mROA.Implementation
public static IContextRepository RemoteEndpointContextRepository public static IContextRepository RemoteEndpointContextRepository
{ {
get => _remoteEndpointContextRepository ?? throw new NullReferenceException("RemoteEndpointContextRepository is null"); get => _remoteEndpointContextRepository ??
throw new NullReferenceException("RemoteEndpointContextRepository is null");
set => _remoteEndpointContextRepository = value; set => _remoteEndpointContextRepository = value;
} }
@@ -29,14 +31,13 @@ namespace mROA.Implementation
get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null"); get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null");
set => _ownershipRepository = value; set => _ownershipRepository = value;
} }
} }
public interface ISharedObject public interface ISharedObject
{ {
IEndPointContext EndPointContext { get; set; } IEndPointContext EndPointContext { get; set; }
} }
public class SharedObject<T> : ISharedObject where T : notnull public class SharedObject<T> : ISharedObject where T : notnull
{ {
private IContextRepository GetDefaultContextRepository() => private IContextRepository GetDefaultContextRepository() =>
@@ -105,6 +106,12 @@ namespace mROA.Implementation
public static implicit operator SharedObject<T>(T value) => public static implicit operator SharedObject<T>(T value) =>
new(value); new(value);
public IEndPointContext EndPointContext { get; set; } public IEndPointContext EndPointContext { get; set; } = new EndPointContext
{
RealRepository = TransmissionConfig.RealContextRepository,
RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository,
HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(),
OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId
};
} }
} }