Skip to content

Commit

Permalink
primitive write and read null and empty struct #29 #30
Browse files Browse the repository at this point in the history
primitive write and read null and empty struct #29 #30
  • Loading branch information
alec1o committed Jul 8, 2024
1 parent 44e187e commit 7df9f8e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/src/primitive/partials/PrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,26 @@ public void Class<T>(T value)

public void Struct<T>(T value)
{
var type = value.GetType();
var type = value == null ? typeof(T) : value.GetType();

if (!(type.IsValueType && !type.IsEnum && !type.IsPrimitive))
throw new InvalidOperationException($"Only struct is accepted. {type} isn't allowed");
// if (!type.IsSerializable) throw new InvalidOperationException("Only serialized class is accepted");

Vault.Add(Prefix.Struct);
var cache = new List<byte>();


var props = type.GetProperties();

if (props.Length <= 0 || value == null)
{
Vault.Add(1); // error
return;
}

Vault.Add(0); // success

var cache = new List<byte>();

if (props.Length <= 0)
{
return;
Expand Down
3 changes: 2 additions & 1 deletion src/src/primitive/partials/PrimitiveGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ public object Struct(Type type)
throw new InvalidOperationException("Only struct is accepted");

if (!IsValidPrefix(Prefix.Struct)) throw new InvalidDataException();

if (!IsValidObject()) return default; // empty or null data

var instance = Activator.CreateInstance(type);

var props = type.GetProperties();
Expand Down
5 changes: 1 addition & 4 deletions test/primitive/Primitive.Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ public void Null()
primitive.Add.Class(NullConfig.TheEmptyClass);
primitive.Add.Class(NullConfig.TheNullClass);

/*
primitive.Add.Struct(NullConfig.TheEmptyStruct);
primitive.Add.Struct(NullConfig.TheNullStruct);

primitive.Add.Enum(NullConfig.TheEmptyEnum);
primitive.Add.Enum(NullConfig.TheNullEnum);
*/


// read
_ = primitive.Get.Bool();
Expand Down Expand Up @@ -117,13 +116,11 @@ public void Null()
_ = primitive.Get.Class<NullConfig.EmptyClass>(); // empty
_ = primitive.Get.Class<NullConfig.NonEmptyClass>(); // null

/*
_ = primitive.Get.Struct<NullConfig.EmptyStruct>(); // empty
_ = primitive.Get.Struct<NullConfig.NonEmptyStruct>(); // null

_ = primitive.Get.Enum<NullConfig.EmptyEnum>(); // empty
_ = primitive.Get.Enum<NullConfig.NonEmptyEnum>(); // null
*/
}

public static class NullConfig
Expand Down

0 comments on commit 7df9f8e

Please sign in to comment.