Skip to content

Commit

Permalink
impl. IPrimitiveGet->Struct(Type)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 25, 2024
1 parent 578a758 commit 1302a12
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/src/extension/PrimitiveExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ internal static (object Value, bool IsError) FromPrimitive(Type type, Primitive
}
else if (type.IsValueType && !type.IsEnum && !type.IsPrimitive)
{
// TODO: impl this...
//value = primitive.Get.Struct(value);
value = primitive.Get.Struct(type);
}

if (primitive.IsValid)
Expand Down
1 change: 1 addition & 0 deletions src/src/primitive/interfances/IPrimitiveGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public interface IPrimitiveGet
object List(Type type);
object Class(Type type);
object Array(Type type);
object Struct(Type type);
}
}
16 changes: 10 additions & 6 deletions src/src/primitive/partials/PrimitiveGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ public T Class<T>()

public T Struct<T>()
{
var type = typeof(T);
return (T)Struct(typeof(T));
}

public object Struct(Type type)
{
if (type == null) return null;

try
{
Expand Down Expand Up @@ -495,15 +500,15 @@ public T Struct<T>()
)
throw new InvalidConstraintException();

var instance = (T)Activator.CreateInstance(typeof(T));
var instance = Activator.CreateInstance(type);

if (objectCount > 0 && collectionBuffer > 0)
{
var primitive = new Primitive(Vault.GetRange(Position, collectionBuffer).ToArray());

Position += collectionBuffer;

var props = typeof(T).GetProperties();
var props = type.GetProperties();

foreach (var prop in props)
if (prop.CanRead && prop.CanWrite)
Expand Down Expand Up @@ -559,7 +564,7 @@ public T Struct<T>()

prop.SetValue(dto, result.Value);

instance = (T)dto;
instance = dto;
}

return instance;
Expand All @@ -569,7 +574,7 @@ public T Struct<T>()
}
catch
{
return SetError<T>();
return SetError<object>();
}
}

Expand Down Expand Up @@ -628,7 +633,6 @@ public object Array(Type type)
}
}


public T[] Array<T>()
{
try
Expand Down
5 changes: 5 additions & 0 deletions test/primitive/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ public object Array(Type type)
throw new NotImplementedException();
}

public object Struct(Type type)
{
throw new NotImplementedException();
}

[Fact] // DONE
public string? String()
{
Expand Down

0 comments on commit 1302a12

Please sign in to comment.