Skip to content

Commit

Permalink
using vtable as found in some guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Arwalk committed Mar 10, 2024
1 parent 13898aa commit 65de126
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions tests/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const FieldType = protobuf.FieldType;
const tests = @import("./generated/tests.pb.zig");
const DefaultValues = @import("./generated/jspb/test.pb.zig").DefaultValues;
const tests_oneof = @import("./generated/tests/oneof.pb.zig");
const assert = std.debug.assert;

pub fn printAllDecoded(input: []const u8) !void {
var iterator = protobuf.WireDecoderIterator{ .input = input };
Expand Down Expand Up @@ -54,47 +55,71 @@ const Invoice = struct {
// generated struct
};

const InvoiceServicesImplementations = struct {
SubscribeSingleInvoice : *const fn(server: *anyopaque, request: SubscribeSingleInvoiceRequest) Invoice
};

pub fn InvoiceServicesImplementations(comptime ServerContext: type) type {
return struct {
SubscribeSingleInvoiceMethod : *const fn(server: *ServerContext, request: SubscribeSingleInvoiceRequest) Invoice
};
}
const InvoiceService = struct {
_ptr: *anyopaque,
_vtab: *const InvoiceServicesImplementations,

pub fn InvoicesService(comptime ServerContext: type) type {
return struct {
context: *ServerContext,
implementations: InvoiceServicesImplementations(ServerContext),
pub fn SubscribeSingleInvoice(self: InvoiceService, request: SubscribeSingleInvoiceRequest) Invoice {
return self._vtab.SubscribeSingleInvoice(self._ptr, request);
}

pub fn init(obj: anytype) InvoiceService {

const Ptr = @TypeOf(obj);
const PtrInfo = @typeInfo(Ptr);
assert(PtrInfo == .Pointer); // Must be a pointer
assert(PtrInfo.Pointer.size == .One); // Must be a single-item pointer
assert(@typeInfo(PtrInfo.Pointer.child) == .Struct); // Must point to a struct
const impl = struct {
fn SubscribeSingleInvoice(ptr: *anyopaque, request: SubscribeSingleInvoiceRequest) Invoice {
const self : Ptr = @ptrCast(@alignCast(ptr));
return self.SubscribeSingleInvoice(request);
}
};
return .{
._ptr = obj,
._vtab = &.{
.SubscribeSingleInvoice = impl.SubscribeSingleInvoice
}
};
}

const Self = @This();

};

pub fn SubscribeSingleInvoice(self: *Self, request: SubscribeSingleInvoiceRequest) Invoice {
return self.implementations.SubscribeSingleInvoiceMethod(self.context, request);
}
};
}
//pub fn InvoicesService(comptime ServerContext: type) type {
// return struct {
// context: *ServerContext,
// implementations: InvoiceServicesImplementations(ServerContext),
//
// const Self = @This();
//
// pub fn SubscribeSingleInvoice(self: *Self, request: SubscribeSingleInvoiceRequest) Invoice {
// return self.implementations.SubscribeSingleInvoiceMethod(self.context, request);
// }
// };
//}

const Server = struct {
something: u32
};
something: u32,

fn implementation(server: *Server, _: SubscribeSingleInvoiceRequest) Invoice {
server.something = 1;
std.debug.print("doing stuff", .{});
return Invoice{};
}
pub fn SubscribeSingleInvoice(server: *Server, _: SubscribeSingleInvoiceRequest) Invoice {
server.something = 1;
std.debug.print("doing stuff", .{});
return Invoice{};
}
};

test "services" {
var server = Server{
.something = 0,
};

var service = InvoicesService(Server){
.context = &server,
.implementations = .{
.SubscribeSingleInvoiceMethod = implementation
}
};
var service = InvoiceService.init(&server);

_ = service.SubscribeSingleInvoice(SubscribeSingleInvoiceRequest{});
try testing.expectEqual(1, server.something);
Expand Down

0 comments on commit 65de126

Please sign in to comment.