stuff
This commit is contained in:
parent
e87e08e964
commit
1277d22656
@ -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});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user