This commit is contained in:
yuzu 2025-05-30 02:09:23 -05:00
parent e87e08e964
commit 1277d22656

View File

@ -39,6 +39,10 @@ pub fn reflectT(self: *Self, comptime T: type, allocator: mem.Allocator, idx: us
const Schema = @typeInfo(T); const Schema = @typeInfo(T);
const flags = self.language.options.flags; const flags = self.language.options.flags;
if (std.meta.hasFn(T, "toJson")) {
return T.toJson(self, allocator, idx);
}
switch (self.language.index.get(idx)) { switch (self.language.index.get(idx)) {
.null => { .null => {
if (Schema == .null) return 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, else => unreachable,
}, },
.string => |string| switch (Schema) { .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| { .@"union" => |unionInfo| inline for (unionInfo.fields) |field| {
if (field.type == []const u8) { if (field.type == []const u8) {
var r: T = undefined; var r: T = undefined;
@ -193,7 +203,7 @@ test reflectT {
\\ "admin": true, \\ "admin": true,
\\ "flags": 0, \\ "flags": 0,
\\ "union": ":D", \\ "union": ":D",
\\ "enum": 1 \\ "enum": "world"
\\} \\}
; ;
var self = try allocator.create(Self); var self = try allocator.create(Self);
@ -220,5 +230,6 @@ test reflectT {
const root = try self.reflectT(UserSchema, allocator, idx); 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}); std.debug.print("friend? {s}\n", .{root.@"union".n128});
} }