Skip to content

Commit

Permalink
zig- replace Remote.byId with generic byKey
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Sep 13, 2021
1 parent d80220a commit 8e89add
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/db/Remote.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ pub const Remote = struct {
pub const BaseType = string;
};

pub fn byID(alloc: *std.mem.Allocator, id: u64) !?Remote {
return try db.first(alloc, Remote, "select * from remotes where id = ?", .{
.id = id,
});
}
pub const byKey = _internal.ByKeyGen(Remote, "remotes").byKey;

pub fn findUserByName(self: Remote, alloc: *std.mem.Allocator, name: string) !?User {
return try db.first(alloc, User, "select * from users where provider = ? and name = ?", .{
Expand Down
38 changes: 38 additions & 0 deletions src/db/_internal.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
const std = @import("std");
const string = []const u8;
const zorm = @import("zorm");

pub const Engine = zorm.engine(.sqlite3);
pub var db: Engine = undefined;

pub fn ByKeyGen(comptime T: type, comptime table_name: string) type {
return struct {
pub fn byKey(alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: FieldType(T, @tagName(key))) !?T {
return try db.first(
alloc,
T,
"select * from " ++ table_name ++ " where " ++ @tagName(key) ++ " = ?",
foo(@tagName(key), value),
);
}
};
}

fn FieldType(comptime T: type, comptime name: string) type {
inline for (std.meta.fields(T)) |item| {
if (std.mem.eql(u8, item.name, name)) {
return item.field_type;
}
}
@compileError(@typeName(T) ++ " does not have a field named " ++ name);
}

pub fn foo(comptime name: string, value: anytype) Struct(name, @TypeOf(value)) {
const T = @TypeOf(value);
var x: Struct(name, T) = undefined;
@field(x, name) = value;
return x;
}

pub fn Struct(comptime name: string, comptime T: type) type {
return @Type(.{ .Struct = .{ .layout = .Auto, .fields = &.{structField(name, T)}, .decls = &.{}, .is_tuple = false } });
}

pub fn structField(comptime name: string, comptime T: type) std.builtin.TypeInfo.StructField {
return .{ .name = name, .field_type = T, .default_value = null, .is_comptime = false, .alignment = @alignOf(T) };
}
2 changes: 1 addition & 1 deletion src/handler/package.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const _internal = @import("./_internal.zig");

pub fn get(_: void, response: *http.Response, request: http.Request, args: struct { remote: u64, user: string, package: string }) !void {
const alloc = request.arena;
const r = try db.Remote.byID(alloc, args.remote);
const r = try db.Remote.byKey(alloc, .id, args.remote);
const o = try r.?.findUserByName(alloc, args.user);
const p = try o.?.findPackageByName(alloc, args.package);
const v = try p.?.versions(alloc);
Expand Down
2 changes: 1 addition & 1 deletion src/handler/user.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const _internal = @import("./_internal.zig");

pub fn get(_: void, response: *http.Response, request: http.Request, args: struct { remote: u64, user: string }) !void {
const alloc = request.arena;
const r = try db.Remote.byID(alloc, args.remote);
const r = try db.Remote.byKey(alloc, .id, args.remote);
const o = try r.?.findUserByName(alloc, args.user);
const p = try o.?.packages(alloc);

Expand Down

0 comments on commit 8e89add

Please sign in to comment.