From 44029cd6e73fd0cdd418b48c0dcaaf7e1f5eca20 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 24 Aug 2021 16:13:43 -0700 Subject: dry up the updated constraint check --- sqlite.zig | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/sqlite.zig b/sqlite.zig index 67b67f5..abeea55 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -1053,20 +1053,11 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t .Typed => |typ| { const FieldTypeInfo = @typeInfo(struct_field.field_type); switch (FieldTypeInfo) { - .Struct, .Enum, .Union => { - if (@hasDecl(struct_field.field_type, "BaseType")) { - if (struct_field.field_type.BaseType != typ) { - @compileError("value type " ++ @typeName(struct_field.field_type.BaseType) ++ " is not the bind marker type " ++ @typeName(typ)); - } - } else if (struct_field.field_type != typ) { - @compileError("value type " ++ @typeName(struct_field.field_type) ++ " is not the bind marker type " ++ @typeName(typ)); - } - }, - else => { - if (struct_field.field_type != typ) { - @compileError("value type " ++ @typeName(struct_field.field_type) ++ " is not the bind marker type " ++ @typeName(typ)); - } - }, + .Struct, .Enum, .Union => comptime assertMarkerType( + if (@hasDecl(struct_field.field_type, "BaseType")) struct_field.field_type.BaseType else struct_field.field_type, + typ, + ), + else => comptime assertMarkerType(struct_field.field_type, typ), } }, .Untyped => {}, @@ -1078,6 +1069,12 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t } } + fn assertMarkerType(comptime Actual: type, comptime Expected: type) void { + if (Actual != Expected) { + @compileError("value type " ++ @typeName(Actual) ++ " is not the bind marker type " ++ @typeName(Expected)); + } + } + fn bindField(self: *Self, comptime FieldType: type, comptime field_name: []const u8, i: c_int, field: FieldType) void { const field_type_info = @typeInfo(FieldType); const column = i + 1; -- cgit v1.2.3