From ae2ff31f21ba4306bf11bae62b9af5cd124240ea Mon Sep 17 00:00:00 2001 From: Booklordofthedings Date: Sat, 3 Aug 2024 15:31:19 +0200 Subject: [PATCH] started another rewrite --- src/BofaAbleAttribute.bf | 13 +++++++++++ src/BofaAttribute.bf | 29 +++++++++++++++++++++++++ src/BofaReflector.bf | 47 ++++++++++++++++++++++++++++++++++++++++ src/IBofaParseable.bf | 7 ++++++ 4 files changed, 96 insertions(+) create mode 100644 src/BofaAbleAttribute.bf create mode 100644 src/BofaAttribute.bf create mode 100644 src/BofaReflector.bf create mode 100644 src/IBofaParseable.bf diff --git a/src/BofaAbleAttribute.bf b/src/BofaAbleAttribute.bf new file mode 100644 index 0000000..0e7170a --- /dev/null +++ b/src/BofaAbleAttribute.bf @@ -0,0 +1,13 @@ +namespace Bofa; + +using System; + +struct BofaAbleAttribute : Attribute +{ + public StringView Name; + + public this(StringView pName) + { + Name = pName; + } +} \ No newline at end of file diff --git a/src/BofaAttribute.bf b/src/BofaAttribute.bf new file mode 100644 index 0000000..8da58ec --- /dev/null +++ b/src/BofaAttribute.bf @@ -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, "}"); + } +} \ No newline at end of file diff --git a/src/BofaReflector.bf b/src/BofaReflector.bf new file mode 100644 index 0000000..09bb373 --- /dev/null +++ b/src/BofaReflector.bf @@ -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 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() 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(value, fieldContent); + buffer.Value.Object.Add(fieldContent.Name, fieldContent); + } + } + } + + } +} \ No newline at end of file diff --git a/src/IBofaParseable.bf b/src/IBofaParseable.bf new file mode 100644 index 0000000..1285338 --- /dev/null +++ b/src/IBofaParseable.bf @@ -0,0 +1,7 @@ +namespace Bofa; + +interface IBofaParseable +{ + public void ToBofa(Bofa target); + public void FromBofa(Bofa input); +} \ No newline at end of file