update: toValue method
This commit is contained in:
parent
bb76e6cd44
commit
ac122a513b
1 changed files with 32 additions and 1 deletions
|
|
@ -318,7 +318,38 @@ pub const Renderer = struct {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn valueToString(self: *const Renderer, alloc: Allocator, buf: *ArrayListUnmanaged(u8), value: Value) !void {
|
fn toValue(self: *Context, value: anytype) RenderError!Value {
|
||||||
|
const T = @TypeOf(value);
|
||||||
|
return switch (@typeInfo(T)) {
|
||||||
|
.bool => Value{ .bool = value },
|
||||||
|
.int, .comptime_int => Value{ .int = @intCast(value) },
|
||||||
|
.float, .comptime_float => Value{ .float = @floatCast(value) },
|
||||||
|
.pointer => Value{ .string = try std.fmt.allocPrint(self.allocator(), "{s}", .{value}) },
|
||||||
|
.@"struct" => blk: {
|
||||||
|
var dict = std.StringHashMapUnmanaged(Value){};
|
||||||
|
inline for (std.meta.fields(T)) |field| {
|
||||||
|
const field_val = @field(value, field.name);
|
||||||
|
const converted = try self.toValue(field_val);
|
||||||
|
try dict.put(self.allocator(), field.name, converted);
|
||||||
|
}
|
||||||
|
break :blk Value{ .dict = dict };
|
||||||
|
},
|
||||||
|
.array => blk: {
|
||||||
|
var list = try self.allocator().alloc(Value, value.len);
|
||||||
|
for (value, 0..) |item, i| {
|
||||||
|
list[i] = try self.toValue(item);
|
||||||
|
}
|
||||||
|
break :blk Value{ .list = list };
|
||||||
|
},
|
||||||
|
.optional => if (value) |v| try self.toValue(v) else .null,
|
||||||
|
.null => .null,
|
||||||
|
// CASO ESPECIAL: o valor já é um Value (ex: lista de Value)
|
||||||
|
.@"union" => if (T == Value) value else @compileError("Unsupported union type: " ++ @typeName(T)),
|
||||||
|
else => @compileError("Unsupported type: " ++ @typeName(T)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn valueToString(self: *const Renderer, alloc: Allocator, buf: *ArrayListUnmanaged(u8), value: Value) RenderError!void {
|
||||||
_ = self;
|
_ = self;
|
||||||
var w = buf.writer(alloc);
|
var w = buf.writer(alloc);
|
||||||
switch (value) {
|
switch (value) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue