From 1277d22656322f330b78f6fd4aa0f973e80a042f Mon Sep 17 00:00:00 2001 From: yuzu Date: Fri, 30 May 2025 02:09:23 -0500 Subject: [PATCH] stuff --- reflection.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/reflection.zig b/reflection.zig index 1677ffe..e46ee37 100644 --- a/reflection.zig +++ b/reflection.zig @@ -39,6 +39,10 @@ pub fn reflectT(self: *Self, comptime T: type, allocator: mem.Allocator, idx: us const Schema = @typeInfo(T); const flags = self.language.options.flags; + if (std.meta.hasFn(T, "toJson")) { + return T.toJson(self, allocator, idx); + } + switch (self.language.index.get(idx)) { .null => { if (Schema == .null) return null; @@ -78,6 +82,12 @@ pub fn reflectT(self: *Self, comptime T: type, allocator: mem.Allocator, idx: us else => unreachable, }, .string => |string| switch (Schema) { + .@"enum" => |enumInfo| { + const strslice = string.slice(&self.language.strings); + inline for (enumInfo.fields) |field| if (mem.eql(u8, field.name, strslice)) { + return std.meta.stringToEnum(T, strslice) orelse error.TypeError; + }; + }, .@"union" => |unionInfo| inline for (unionInfo.fields) |field| { if (field.type == []const u8) { var r: T = undefined; @@ -193,7 +203,7 @@ test reflectT { \\ "admin": true, \\ "flags": 0, \\ "union": ":D", - \\ "enum": 1 + \\ "enum": "world" \\} ; var self = try allocator.create(Self); @@ -220,5 +230,6 @@ test reflectT { const root = try self.reflectT(UserSchema, allocator, idx); + std.debug.print("hello? {s}\n", .{@tagName(root.@"enum")}); std.debug.print("friend? {s}\n", .{root.@"union".n128}); }