Skip to content

Commit

Permalink
PrimitiveExtension.ToPrimitive impl base types
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 19, 2024
1 parent 0ef0d1f commit aa5582c
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/extension/Primitive.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
using System;
using System.Collections.Generic;
using System.Numerics;

namespace Byter
{
public static class PrimitiveExtension
{
public static byte[] ToPrimitive<T>(this T value)
internal static byte[] ToPrimitive<T>(this T value)
{
return default;
var primitive = new Primitive();
var type = typeof(T);

object blackbox = value;

// 1 byte (3)
if (type == typeof(bool)) primitive.Add.Bool((bool)blackbox);
if (type == typeof(byte)) primitive.Add.Byte((byte)blackbox);
if (type == typeof(sbyte)) primitive.Add.SByte((sbyte)blackbox);

// 2 bytes (3)
if (type == typeof(char)) primitive.Add.Char((char)blackbox);
if (type == typeof(short)) primitive.Add.Short((short)blackbox);
if (type == typeof(ushort)) primitive.Add.UShort((ushort)blackbox);

// 4 bytes (4)
if (type == typeof(int)) primitive.Add.Int((int)blackbox);
if (type == typeof(uint)) primitive.Add.UInt((uint)blackbox);
if (type == typeof(float)) primitive.Add.Float((float)blackbox);
if (type.IsEnum) primitive.Add.Enum(value);

// 8 bytes (3)
else if (type == typeof(long)) primitive.Add.Long((long)blackbox);
else if (type == typeof(ulong)) primitive.Add.ULong((ulong)blackbox);
else if (type == typeof(double)) primitive.Add.Double((double)blackbox);
else if (type == typeof(DateTime)) primitive.Add.DateTime((DateTime)blackbox);

// 16 bytes (1)
else if (type == typeof(decimal)) primitive.Add.Decimal((decimal)blackbox);

// dynamic (7)
else if (type == typeof(string)) primitive.Add.String((string)blackbox);
else if (type == typeof(BigInteger)) primitive.Add.BigInteger((BigInteger)blackbox);
else if (type == typeof(byte[])) primitive.Add.Bytes((byte[])blackbox);
else if (type == typeof(T[])) ; // TODO: array

Check warning on line 45 in src/extension/Primitive.cs

View workflow job for this annotation

GitHub Actions / build

Possible mistaken empty statement

Check warning on line 45 in src/extension/Primitive.cs

View workflow job for this annotation

GitHub Actions / build

Possible mistaken empty statement
else if (type == typeof(List<T>)) ; // TODO: list

Check warning on line 46 in src/extension/Primitive.cs

View workflow job for this annotation

GitHub Actions / build

Possible mistaken empty statement
else if (type == typeof(object)) ; // TODO: class

Check warning on line 47 in src/extension/Primitive.cs

View workflow job for this annotation

GitHub Actions / build

Possible mistaken empty statement
else if (type == typeof(object)) ; // TODO: struct

Check warning on line 48 in src/extension/Primitive.cs

View workflow job for this annotation

GitHub Actions / build

Possible mistaken empty statement

return primitive.Data;
}

public static T ToPrimitive<T>(this T value, Primitive primitive)
{
return value;
}

public static T FromPrimitive<T>(this T value, Primitive primitive)
{
return value;
Expand Down

0 comments on commit aa5582c

Please sign in to comment.