From 2c19a41988c66a2b7ac1ce7479176cd5437f7ab9 Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Sun, 19 May 2024 18:30:38 +0200 Subject: [PATCH] impl PrimitiveGet->array --- src/src/partials/PrimitiveGet.cs | 53 ++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/src/partials/PrimitiveGet.cs b/src/src/partials/PrimitiveGet.cs index 29b7264..a4bedf4 100644 --- a/src/src/partials/PrimitiveGet.cs +++ b/src/src/partials/PrimitiveGet.cs @@ -363,9 +363,58 @@ public T Struct() public T[] Array() { - throw new NotImplementedException(); - } + try + { + if (!IsValidPrefix(Prefix.Array)) throw new InvalidDataException(); + var list = new List(); + + 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(); + } + + if (objectCount > 0 && collectionBuffer > 0) + { + var primitive = new Primitive(Vault.GetRange(Position, collectionBuffer).ToArray()); + + for (int i = 0; i < objectCount; i++) + { + var result = PrimitiveExtension.FromPrimitive(primitive); + + if (result.IsError) + { + throw new InvalidDataException(); + } + + list.Add(result.Value); + } + } + + return list.ToArray(); + } + catch + { + return SetError(); + } + } + public List List() { try