started another rewrite
This commit is contained in:
parent
06f2f8a12c
commit
ae2ff31f21
4 changed files with 96 additions and 0 deletions
13
src/BofaAbleAttribute.bf
Normal file
13
src/BofaAbleAttribute.bf
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
namespace Bofa;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
struct BofaAbleAttribute : Attribute
|
||||||
|
{
|
||||||
|
public StringView Name;
|
||||||
|
|
||||||
|
public this(StringView pName)
|
||||||
|
{
|
||||||
|
Name = pName;
|
||||||
|
}
|
||||||
|
}
|
29
src/BofaAttribute.bf
Normal file
29
src/BofaAttribute.bf
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
namespace Bofa;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
[AttributeUsage(.Field)]
|
||||||
|
struct BofaObjectAttribute : Attribute, IOnTypeInit
|
||||||
|
{
|
||||||
|
public StringView Name;
|
||||||
|
|
||||||
|
public this(StringView pName)
|
||||||
|
{
|
||||||
|
Name = pName;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Comptime]
|
||||||
|
public void OnTypeInit(Type type, Self* prev)
|
||||||
|
{
|
||||||
|
Compiler.EmitTypeBody(type, "public override void ToString(String str)\n{\n");
|
||||||
|
for (var fieldInfo in type.GetFields())
|
||||||
|
{
|
||||||
|
if (!fieldInfo.IsInstanceField)
|
||||||
|
continue;
|
||||||
|
if (@fieldInfo.Index > 0)
|
||||||
|
Compiler.EmitTypeBody(type, "\tstr.Append(\", \");\n");
|
||||||
|
Compiler.EmitTypeBody(type, scope $"\tstr.AppendF($\"{fieldInfo.Name}={{ {fieldInfo.Name} }}\");\n");
|
||||||
|
}
|
||||||
|
Compiler.EmitTypeBody(type, "}");
|
||||||
|
}
|
||||||
|
}
|
47
src/BofaReflector.bf
Normal file
47
src/BofaReflector.bf
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
namespace Bofa;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
class BofaReflector
|
||||||
|
{
|
||||||
|
|
||||||
|
///Parse an object into its bofa representation
|
||||||
|
public static void ToBofa<T>(T pInput, Bofa buffer)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Recursivly loop through all fields of the object and parse all marked fields
|
||||||
|
if a custom parser is supplied via IBofaParseable we use that.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//User supplied their own parser
|
||||||
|
if(let iface = pInput as IBofaParseable)
|
||||||
|
{
|
||||||
|
//Simply just call the user supplied parser
|
||||||
|
iface.ToBofa(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.Type = .Object;
|
||||||
|
buffer.TypeName = "Object";
|
||||||
|
buffer.Value.Object = new .();
|
||||||
|
|
||||||
|
var fields = typeof(T).GetFields();
|
||||||
|
for(var i in fields) //Loop through all fields of the object
|
||||||
|
{
|
||||||
|
//If we should actually parse the field
|
||||||
|
if(i.GetCustomAttribute<BofaAbleAttribute>() case .Ok(let attr))
|
||||||
|
{
|
||||||
|
if(i.GetValue(pInput) case .Ok(let value))
|
||||||
|
{
|
||||||
|
|
||||||
|
Bofa fieldContent = new .();
|
||||||
|
fieldContent.Name = attr.Name;
|
||||||
|
Compiler.Mixin(scope $"ToBofa<{i.GetType()}>(value,fieldContent);");
|
||||||
|
//ToBofa<typeof(value)>(value, fieldContent);
|
||||||
|
buffer.Value.Object.Add(fieldContent.Name, fieldContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
7
src/IBofaParseable.bf
Normal file
7
src/IBofaParseable.bf
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
namespace Bofa;
|
||||||
|
|
||||||
|
interface IBofaParseable
|
||||||
|
{
|
||||||
|
public void ToBofa(Bofa target);
|
||||||
|
public void FromBofa(Bofa input);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue