Add metadata to types

TAGS adds metadata to struct fields.


Lets say that we want to generate SQL statements that create the database tables from our structs.

pub const Client = struct {
    id: u64,
    name: []const u8,
    phone: ?[]const u8,

    pub const TAGS = .{
        .id = .{ .db_type = "bigint"},
        .name = .{ .db_type = "varchar(80)"},
        .phone = .{ .db_type = "varchar(20)"},
    };
};

What we need is the following statement:

CREATE TABLE Client(
    id bigint not null, 
    name varchar(80) not null,
    phone varchar(20) null
);

From zig struct definition we have the name of the struct, the name of the attributes and the nullability of the attributes (if it is optional or not) but we are missing the database types.
TAGS holds the fields additional metadata that we require. .db_type is the type of the field in the database.

4 Likes