Skip to content

Element

Neon32eeee edited this page Feb 20, 2026 · 5 revisions

The Element type is provided as a struct that represents a single row/record in the table, with the following fields:

  • tname — name of the table this element belongs to (used for type checking)
  • field — hash map that contains all fields of this row (std.StringHashMap(FieldType))
  • scheme — Array used to index fields

Function breakdown (Element)

  • get
    Purpose: returns the raw value of a field
    Signature:

    get(self: @This(), key: []const u8) ?FieldType
  • getAs
    Purpose: returns field value cast to desired type
    Signature:

    getAs(self: @This(), comptime T: type, key: []const u8) ?T
  • getIndex/getIndexAs Same as get and getAs but using an index instead of a key

  • set
    Purpose: set the raw value of a field
    Signature:

    set(self: *@This(), key: []const u8, value: FieldType) !void
  • setAs
    Purpose: sets field value with type conversion
    Signature:

    setAs(self: *@This(), comptime T: type, key: []const u8, value: T) !void
  • setIndex/setIndexAs Same as set and setAs but using an index instead of a key

  • save / load Save all fields to binary writer Load fields from binary reader

  • deinit Purpose: frees hash map memory Signature:

    deinit(self: *@This(), allocator: std.mem.Allocator) void

Examples

1. Creating and filling Element manually

var elem = Types.Element{
    .tname = "users",
    .field = std.StringHashMap(Types.FiledType).init(allocator),
    .scheme = std.ArrayList([]const u8){},
};
defer elem.deinit(allocator);

try elem.setAs(i32, "id", 1);
try elem.setAs([]const u8, "name", "Alice");
try elem.setAs(bool, "active", true);

try users_table.append(elem);

2. Reading values from Element

// continuing from previous code...

if (users_table.get(0)) |row| {
    const id     = row.getAs(i32, "id")     orelse 0;
    const name   = row.getAs([]const u8, "name")   orelse "unknown";
    const active = row.getAs(bool, "active") orelse false;

    std.debug.print("{s} (id {d}, active: {})\n", .{name, id, active});
}

Clone this wiki locally