Skip to content

Commit

Permalink
clean and organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 21, 2024
1 parent ba9aaf0 commit ae8027b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 137 deletions.
8 changes: 4 additions & 4 deletions src/src/interfance/IPrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public interface IPrimitiveAdd
void Bool(bool value);
void Byte(byte value);
void SByte(sbyte value);

// 2 bytes (3)
void Char(char value);
void Short(short value);
void UShort(ushort value);

// 4 bytes (4)
void Int(int value);
void UInt(uint value);
Expand All @@ -27,10 +27,10 @@ public interface IPrimitiveAdd
void ULong(ulong value);
void Double(double value);
void DateTime(DateTime value);

// 16 bytes (1)
void Decimal(decimal value);

// dynamic (7)
void String(string value);
void Class<T>(T value);
Expand Down
64 changes: 23 additions & 41 deletions src/src/partials/PrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;

namespace Byter
Expand All @@ -12,13 +11,14 @@ public partial class Primitive
private class PrimitiveAdd : IPrimitiveAdd
{
private readonly Primitive _primitive;
private List<byte> Vault => _primitive._bytes;

public PrimitiveAdd(Primitive primitive)
{
_primitive = primitive;
}

private List<byte> Vault => _primitive._bytes;

public void Bool(bool value)
{
Vault.Add(Prefix.Bool);
Expand Down Expand Up @@ -108,34 +108,28 @@ public void Decimal(decimal value)
{
Vault.Add(Prefix.Decimal);

List<int> list = decimal.GetBits(value).ToList();
var list = decimal.GetBits(value).ToList();

foreach (int x in list)
{
Vault.AddRange(BitConverter.GetBytes(x));
}
foreach (var x in list) Vault.AddRange(BitConverter.GetBytes(x));
}

public void String(string value)
{
Vault.Add(Prefix.String);

byte[] bytes = Encoding.UTF8.GetBytes(value ?? string.Empty);
var bytes = Encoding.UTF8.GetBytes(value ?? string.Empty);

Vault.AddRange(BitConverter.GetBytes(bytes.Length));

if (bytes.Length > 0)
{
Vault.AddRange(bytes);
}
if (bytes.Length > 0) Vault.AddRange(bytes);
}

public void Class<T>(T value)
{
const int defaultCount = 0;
const int defaultBuffer = 0;

Type type = typeof(T);
var type = typeof(T);

if (!type.IsClass) throw new InvalidOperationException("Only class is accepted");
// if (!type.IsSerializable) throw new InvalidOperationException("Only serialized class is accepted");
Expand All @@ -150,7 +144,7 @@ public void Class<T>(T value)
return;
}

PropertyInfo[] props = type.GetProperties();
var props = type.GetProperties();

if (props.Length <= 0)
{
Expand All @@ -159,21 +153,19 @@ public void Class<T>(T value)
return;
}

int count = 0;
var count = 0;

foreach (var prop in props)
{
if (prop.CanRead && prop.CanWrite)
{
object propValue = prop.GetValue(value);
var propValue = prop.GetValue(value);
var propBuffer = propValue.ToPrimitive(prop.PropertyType);
if (propBuffer != null && propBuffer.Length > 0)
{
count++;
cache.AddRange(propBuffer);
}
}
}

if (count <= 0 || cache.Count <= 0)
{
Expand All @@ -193,9 +185,10 @@ public void Struct<T>(T value)
const int defaultCount = 0;
const int defaultBuffer = 0;

Type type = typeof(T);
var type = typeof(T);

if (!(type.IsValueType && !type.IsEnum && !type.IsPrimitive)) throw new InvalidOperationException("Only struct is accepted");
if (!(type.IsValueType && !type.IsEnum && !type.IsPrimitive))
throw new InvalidOperationException("Only struct is accepted");
// if (!type.IsSerializable) throw new InvalidOperationException("Only serialized class is accepted");

Vault.Add(Prefix.Struct);
Expand All @@ -208,7 +201,7 @@ public void Struct<T>(T value)
return;
}

PropertyInfo[] props = type.GetProperties();
var props = type.GetProperties();

if (props.Length <= 0)
{
Expand All @@ -217,21 +210,19 @@ public void Struct<T>(T value)
return;
}

int count = 0;
var count = 0;

foreach (var prop in props)
{
if (prop.CanRead && prop.CanWrite)
{
object propValue = prop.GetValue(value);
var propValue = prop.GetValue(value);
var propBuffer = propValue.ToPrimitive(prop.PropertyType);
if (propBuffer != null && propBuffer.Length > 0)
{
count++;
cache.AddRange(propBuffer);
}
}
}

if (count <= 0 || cache.Count <= 0)
{
Expand All @@ -250,16 +241,13 @@ public void Array<T>(T[] value)
{
Vault.Add(Prefix.Array);

int size = value?.Length ?? 0;
var size = value?.Length ?? 0;

if (size > 0 && value != null)
{
var collection = new List<byte>();

foreach (T x in value)
{
collection.AddRange(x.ToPrimitive());
}
foreach (var x in value) collection.AddRange(x.ToPrimitive());

Vault.AddRange(BitConverter.GetBytes(size)); // objects count
Vault.AddRange(BitConverter.GetBytes(collection.Count)); // buffer size
Expand All @@ -276,16 +264,13 @@ public void List<T>(List<T> value)
{
Vault.Add(Prefix.List);

int size = value?.Count ?? 0;
var size = value?.Count ?? 0;

if (size > 0 && value != null)
{
var collection = new List<byte>();

foreach (T x in value)
{
collection.AddRange(x.ToPrimitive());
}
foreach (var x in value) collection.AddRange(x.ToPrimitive());

Vault.AddRange(BitConverter.GetBytes(size)); // objects count
Vault.AddRange(BitConverter.GetBytes(collection.Count)); // buffer size
Expand All @@ -302,7 +287,7 @@ public void BigInteger(BigInteger value)
{
Vault.Add(Prefix.BigInteger);

byte[] bytes = value.ToByteArray();
var bytes = value.ToByteArray();

Vault.AddRange(BitConverter.GetBytes(bytes.Length));

Expand All @@ -311,16 +296,13 @@ public void BigInteger(BigInteger value)

public void Bytes(byte[] value)
{
byte[] bytes = value ?? System.Array.Empty<byte>();
var bytes = value ?? System.Array.Empty<byte>();

Vault.Add(Prefix.Bytes);

Vault.AddRange(BitConverter.GetBytes(bytes.Length));

if (bytes.Length > 0)
{
Vault.AddRange(bytes);
}
if (bytes.Length > 0) Vault.AddRange(bytes);
}
}
}
Expand Down
Loading

0 comments on commit ae8027b

Please sign in to comment.