Update to 1.1.0 #5
3 changed files with 57 additions and 4 deletions
7
src/Serialization/BofaRequiredAttribute.bf
Normal file
7
src/Serialization/BofaRequiredAttribute.bf
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Bofa.Serialization;
|
||||
|
||||
using System;
|
||||
|
||||
struct BofaRequiredAttribute : Attribute
|
||||
{
|
||||
}
|
|
@ -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}"]);
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue