update: context set

This commit is contained in:
Lucas F. 2026-01-04 12:29:36 -03:00
parent 164e68e94c
commit e5305f85c9
2 changed files with 110 additions and 75 deletions

View file

@ -10,7 +10,7 @@ pub const Value = union(enum) {
dict: std.StringHashMapUnmanaged(Value),
pub fn deinit(self: Value) void {
_ = self; // nada a arena libera tudo
_ = self; // arena libera tudo
}
};
@ -34,18 +34,94 @@ pub const Context = struct {
self.arena.deinit();
}
pub fn set(self: *Context, key: []const u8, value: Value) !void {
const gop = try self.map.getOrPut(self.allocator(), key);
if (gop.found_existing) {
// opcional: deinit value antigo se necessário
// mas como arena libera tudo, não precisa
} else {
gop.key_ptr.* = try self.allocator().dupe(u8, key);
}
gop.value_ptr.* = value;
fn toValue(self: *Context, value: anytype) !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 = std.fmt.allocPrint(self.allocator(), "{s}", .{value}) catch "" },
.@"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,
else => @compileError("Unsupported type: " ++ @typeName(T)),
};
}
pub fn get(self: *const Context, key: []const u8) ?Value {
return self.map.get(key);
pub fn set(self: *Context, key: []const u8, value: anytype) !void {
const v = try self.toValue(value);
const gop = try self.map.getOrPut(self.allocator(), key);
if (!gop.found_existing) {
gop.key_ptr.* = try self.allocator().dupe(u8, key);
}
gop.value_ptr.* = v;
}
//get com suporte a ponto ("user.name", "lista.0")
pub fn get(self: *const Context, path: []const u8) ?Value {
if (path.len == 0) return null;
var current = self.map.get(path);
if (current) |v| return v;
var remaining = path;
var start: usize = 0;
while (std.mem.indexOfScalarPos(u8, remaining, start, '.')) |dot_pos| {
const key = remaining[start..dot_pos];
current = self.map.get(key);
if (current == null) return null;
remaining = remaining[dot_pos + 1 ..];
start = 0;
current = switch (current.?) {
.dict => |d| d.get(remaining),
.list => |l| blk: {
const index = std.fmt.parseInt(usize, remaining, 10) catch return null;
break :blk if (index < l.len) l[index] else null;
},
else => return null,
};
if (current == null) return null;
}
return current;
}
// pub fn get(self: *const Context, comptime T: type, key: []const u8) !T {
// // const opt_value = self.map.get(key) orelse return error.KeyNotFound;
// const opt_value = self.getOptional(T, key);
//
// return switch (opt_value) {
// .null => if (T == void or @typeInfo(T) == .optional) null else error.TypeMismatch,
// .bool => |b| if (T == bool) b else error.TypeMismatch,
// .int => |i| if (@typeInfo(T) == .int or @typeInfo(T) == .comptime_int) @intCast(i) else error.TypeMismatch,
// .float => |f| if (@typeInfo(T) == .float or @typeInfo(T) == .comptime_float) @floatCast(f) else error.TypeMismatch,
// .string => |s| if (T == []const u8) s else error.TypeMismatch,
// .list => |l| if (T == []const Value) l else error.TypeMismatch,
// .dict => |d| if (T == std.StringHashMapUnmanaged(Value)) d else error.TypeMismatch,
// };
// }
//
// // Versão opcional que retorna optional
// pub fn getOptional(self: *const Context, comptime T: type, key: []const u8) ?T {
// return self.get(T, key) catch null;
// }
};