Skip to content

Commit

Permalink
impl PrimitiveGet->array
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 19, 2024
1 parent c3c6f4e commit 2c19a41
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/src/partials/PrimitiveGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,58 @@ public T Struct<T>()

public T[] Array<T>()
{
throw new NotImplementedException();
}
try
{
if (!IsValidPrefix(Prefix.Array)) 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.ToArray();
}
catch
{
return SetError<T[]>();
}
}

public List<T> List<T>()
{
try
Expand Down

0 comments on commit 2c19a41

Please sign in to comment.