I’m writing a deserialiser. Given a type it can decode bytes into that type.
Some of the serialised bytes have the same representation in zig but are different in byte format, for example a bytearray might be prefixed by 0x01 and array of u8 as 0x02, but they are both just decoded as []const u8.
For now i just use a switch statement, when 0x02 or 0x01 is found then decode into []const u8. Is there a way to add metadata to a type so i could optimize this at comptime?
Here a is of type Field, that has the type to decode into (u8) and the format that it will find it as (.Bin). This way decodeAs has some extra context and might remove some unused branches.
Perhaps you could provide more details about the data that you have. Is the prefix a header byte? Are you trying to work with variable length lists or static arrays? Are you consuming bytes in a stream?
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.