| ... | ... | @@ -0,0 +1,38 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | pub const GET = struct { |
| 4 | key: []const u8, |
| 5 | |
| 6 | pub fn init(key: []const u8) GET { |
| 7 | return .{ .key = key }; |
| 8 | } |
| 9 | |
| 10 | pub const Redis = struct { |
| 11 | pub const Command = struct { |
| 12 | pub fn serialize(self: GET, comptime rootSerializer: type) void { |
| 13 | return rootSerializer.serializeCommand(.{ "GET", self.key }); |
| 14 | } |
| 15 | }; |
| 16 | }; |
| 17 | }; |
| 18 | |
| 19 | pub fn isCommand(comptime T: type) bool { |
| 20 | const tid = @typeId(T); |
| 21 | return (tid == .Struct or tid == .Enum or tid == .Union) and |
| 22 | @hasDecl(T, "Redis") and @hasDecl(T.Redis, "Command"); |
| 23 | } |
| 24 | |
| 25 | pub const ArgSerializer = struct { |
| 26 | pub fn serializeCommand(command: var) void { |
| 27 | const CmdT = @typeOf(command); |
| 28 | |
| 29 | if (comptime isCommand(CmdT)) { |
| 30 | // COMMENTING THE NEXT LINE REMOVES THE ERROR |
| 31 | return CmdT.Redis.Command.serialize(command, ArgSerializer); |
| 32 | } |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | test "fixed" { |
| 37 | ArgSerializer.serializeCommand(GET.init("banana")); |
| 38 | } |