Skip to content

Commit

Permalink
impl PrimitiveGet->list
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 19, 2024
1 parent ba40eb6 commit c3c6f4e
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/src/partials/PrimitiveGet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Numerics;
Expand Down Expand Up @@ -367,7 +368,56 @@ public T[] Array<T>()

public List<T> List<T>()
{
throw new NotImplementedException();
try
{
if (!IsValidPrefix(Prefix.List)) throw new InvalidDataException();

var list = new List<T>();

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<T>(primitive);

if (result.IsError)
{
throw new InvalidDataException();
}

list.Add(result.Value);
}
}

return list;
}
catch
{
return SetError<List<T>>();
}
}

public BigInteger BigInteger()
Expand Down

0 comments on commit c3c6f4e

Please sign in to comment.