From d806d19c692013f65d77285a075dcd1b717fee73 Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Tue, 21 May 2024 11:20:50 +0200 Subject: [PATCH] impl. PrimitiveGet->struct --- src/src/partials/PrimitiveGet.cs | 66 +++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/src/partials/PrimitiveGet.cs b/src/src/partials/PrimitiveGet.cs index e63bf1b..5bcda0d 100644 --- a/src/src/partials/PrimitiveGet.cs +++ b/src/src/partials/PrimitiveGet.cs @@ -424,7 +424,71 @@ public T Class() public T Struct() { - throw new NotImplementedException(); + Type type = typeof(T); + + try + { + if (!(type.IsValueType && !type.IsEnum && !type.IsPrimitive)) + throw new InvalidOperationException("Only struct is accepted"); + + if (!IsValidPrefix(Prefix.Struct)) throw new InvalidDataException(); + + int objectCount = BitConverter.ToInt32(VaultArray, Position); + Position += sizeof(int); + + int collectionBuffer = BitConverter.ToInt32(VaultArray, Position); + Position += sizeof(int); + + if + ( + // objects count lower than zero + objectCount < 0 || + // buffer size lower than zero + collectionBuffer < 0 || + // if zero objects count is zero, buffer size must be zero too + (objectCount == 0 && collectionBuffer != 0) || + // if have object(s), the buffer size must not be zero + (objectCount != 0 && collectionBuffer == 0) + ) + { + throw new InvalidConstraintException(); + } + + T instance = (T)Activator.CreateInstance(typeof(T)); + + if (objectCount > 0 && collectionBuffer > 0) + { + var primitive = new Primitive(Vault.GetRange(Position, collectionBuffer).ToArray()); + + PropertyInfo[] props = typeof(T).GetProperties(); + + if (props.Length <= 0) return default; + + foreach (var prop in props) + { + if (prop.CanRead && prop.CanWrite) + { + var result = PrimitiveExtension.FromPrimitive(prop.PropertyType, primitive); + + if (result.IsError) + { + throw new InvalidDataException(); + } + + // TODO: fix this bug (is set value) + prop.SetValue(instance, result.Value); + } + } + + return instance; + } + + return instance; + } + catch + { + return SetError(); + } } public T[] Array()