diff options
| author | 2021-01-01 18:46:57 +0100 | |
|---|---|---|
| committer | 2021-01-06 01:04:18 +0100 | |
| commit | f74d40048465965fe044b4e2e3c26b939256fadf (patch) | |
| tree | 391d9801187f507e08dae69d6a4fedaacffde40f /sqlite.zig | |
| parent | reorder fields to workaround a compiler bug (diff) | |
| download | zig-sqlite-f74d40048465965fe044b4e2e3c26b939256fadf.tar.gz zig-sqlite-f74d40048465965fe044b4e2e3c26b939256fadf.tar.xz zig-sqlite-f74d40048465965fe044b4e2e3c26b939256fadf.zip | |
add readField
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 35 |
1 files changed, 20 insertions, 15 deletions
| @@ -569,27 +569,32 @@ pub fn Iterator(comptime Type: type) type { | |||
| 569 | 569 | ||
| 570 | inline for (@typeInfo(Type).Struct.fields) |field, _i| { | 570 | inline for (@typeInfo(Type).Struct.fields) |field, _i| { |
| 571 | const i = @as(usize, _i); | 571 | const i = @as(usize, _i); |
| 572 | const field_type_info = @typeInfo(field.field_type); | 572 | |
| 573 | 573 | const ret = try self.readField(field.field_type, i, options); | |
| 574 | const ret = switch (field.field_type) { | ||
| 575 | Blob => try self.readBytes(Blob, options.allocator, i, .Blob), | ||
| 576 | Text => try self.readBytes(Text, options.allocator, i, .Text), | ||
| 577 | else => switch (field_type_info) { | ||
| 578 | .Int => try self.readInt(field.field_type, i), | ||
| 579 | .Float => try self.readFloat(field.field_type, i), | ||
| 580 | .Bool => try self.readBool(i), | ||
| 581 | .Void => {}, | ||
| 582 | .Array => try self.readArray(field.field_type, i), | ||
| 583 | .Pointer => try self.readPointer(field.field_type, options.allocator, i), | ||
| 584 | else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(field.field_type)), | ||
| 585 | }, | ||
| 586 | }; | ||
| 587 | 574 | ||
| 588 | @field(value, field.name) = ret; | 575 | @field(value, field.name) = ret; |
| 589 | } | 576 | } |
| 590 | 577 | ||
| 591 | return value; | 578 | return value; |
| 592 | } | 579 | } |
| 580 | |||
| 581 | fn readField(self: *Self, comptime FieldType: type, i: usize, options: anytype) !FieldType { | ||
| 582 | const field_type_info = @typeInfo(FieldType); | ||
| 583 | |||
| 584 | return switch (FieldType) { | ||
| 585 | Blob => try self.readBytes(Blob, options.allocator, i, .Blob), | ||
| 586 | Text => try self.readBytes(Text, options.allocator, i, .Text), | ||
| 587 | else => switch (field_type_info) { | ||
| 588 | .Int => try self.readInt(FieldType, i), | ||
| 589 | .Float => try self.readFloat(FieldType, i), | ||
| 590 | .Bool => try self.readBool(i), | ||
| 591 | .Void => {}, | ||
| 592 | .Array => try self.readArray(FieldType, i), | ||
| 593 | .Pointer => try self.readPointer(FieldType, options.allocator, i), | ||
| 594 | else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(FieldType)), | ||
| 595 | }, | ||
| 596 | }; | ||
| 597 | } | ||
| 593 | }; | 598 | }; |
| 594 | } | 599 | } |
| 595 | 600 | ||