Update to 1.1.0 #5

Merged
Booklordofthedings merged 16 commits from dev into main 2024-09-26 17:32:20 +02:00
3 changed files with 57 additions and 4 deletions
Showing only changes of commit c8629face9 - Show all commits

View file

@ -0,0 +1,7 @@
namespace Bofa.Serialization;
using System;
struct BofaRequiredAttribute : Attribute
{
}

View file

@ -8,6 +8,12 @@ using Bofa;
[AttributeUsage(.Struct | .Class)]
struct BofaSerializeAttribute : Attribute, IOnTypeInit
{
private bool Strict;
public this(bool pStrict = false)
{
Strict = pStrict;
}
[Comptime]
public void OnTypeInit(Type type, Self* prev)
{
@ -71,6 +77,8 @@ struct BofaSerializeAttribute : Attribute, IOnTypeInit
hasIFace = true;
else if(f.FieldType == typeof(double))
hasIFace = true;
else if(f.FieldType == typeof(System.Security.Cryptography.SHA256Hash))
hasIFace = true;
if(!hasIFace)
continue;
@ -81,24 +89,39 @@ struct BofaSerializeAttribute : Attribute, IOnTypeInit
if(f.HasCustomAttribute<BofaIncludeAttribute>() && f.GetCustomAttribute<BofaIncludeAttribute>() case .Ok(let attr))
name = attr.Name;
var required = f.HasCustomAttribute<BofaRequiredAttribute>() || Strict;
serializeString.Append(scope $"""
toAdd = new .();
toAdd.Name = "{name}";
if(!{f.Name}.Serialize(toAdd))
\{
delete toAdd;
return false;
{required ? "return false;" : ""}
\}
else
b.Value.Object.Add("{name}", toAdd);
""");
deserializeString.Append(scope $"""
if(required)
{
deserializeString.Append((scope $"""
if(!b.Value.Object.ContainsKey("{name}"))
return false;
if(!{f.Name}.Deserialize(b.Value.Object["{name}"]))
return false;
"""));
}
else
{
deserializeString.Append(scope $"""
if(b.Value.Object.ContainsKey("{name}"))
{f.Name}.Deserialize(b.Value.Object["{name}"]);
""");
}
}

View file

@ -10,10 +10,11 @@ using Bofa.Serialization;
class Serialization
{
public int anInteger = 1;
public String aString = "Value";
public String aString = new .("Value") ~ delete _;
public List<String> listOfStrings = new .() ~ DeleteContainerAndItems!(_);
public int anotherOne = 0;
[Test]
[Test(Name="Serialize")]
public static void Test()
{
Serialization s = scope .();
@ -24,4 +25,26 @@ class Serialization
}
[Test(Name="Deserialize")]
public static void Test_Deserialize()
{
Serialization s = scope .();
String content = """
o name
i anInteger 35
l aString this is a string
a listOfStrings
""";
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
Test.Assert(s.Deserialize(output["name"]));
DeleteDictionaryAndValues!(output);
delete errors;
}
}