mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
endpoint type check with meaningful error messages
This commit is contained in:
parent
8e80eecc3e
commit
0123e60059
1 changed files with 30 additions and 0 deletions
|
@ -9,6 +9,34 @@ const Request = zap.Request;
|
||||||
const ListenerSettings = zap.HttpListenerSettings;
|
const ListenerSettings = zap.HttpListenerSettings;
|
||||||
const HttpListener = zap.HttpListener;
|
const HttpListener = zap.HttpListener;
|
||||||
|
|
||||||
|
pub fn checkEndpointType(T: type) void {
|
||||||
|
if (@hasField(T, "path")) {
|
||||||
|
if (@FieldType(T, "path") != []const u8) {
|
||||||
|
@compileError(@typeName(@FieldType(T, "path")) ++ " has wrong type, expected: []const u8");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
@compileError(@typeName(T) ++ " has no path field");
|
||||||
|
}
|
||||||
|
|
||||||
|
const methods_to_check = [_][]const u8{
|
||||||
|
"get",
|
||||||
|
"post",
|
||||||
|
"put",
|
||||||
|
"delete",
|
||||||
|
"patch",
|
||||||
|
"options",
|
||||||
|
};
|
||||||
|
inline for (methods_to_check) |method| {
|
||||||
|
if (@hasDecl(T, method)) {
|
||||||
|
if (@TypeOf(@field(T, method)) != fn (_: *T, _: Request) void) {
|
||||||
|
@compileError(method ++ " method of " ++ @typeName(T) ++ " has wrong type:\n" ++ @typeName(@TypeOf(T.get)) ++ "\nexpected:\n" ++ @typeName(fn (_: *T, _: Request) void));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
@compileError(@typeName(T) ++ " has no method named `" ++ method ++ "`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const EndpointWrapper = struct {
|
const EndpointWrapper = struct {
|
||||||
pub const Wrapper = struct {
|
pub const Wrapper = struct {
|
||||||
call: *const fn (*Wrapper, zap.Request) void = undefined,
|
call: *const fn (*Wrapper, zap.Request) void = undefined,
|
||||||
|
@ -53,6 +81,7 @@ const EndpointWrapper = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(T: type, value: *T) EndpointWrapper.Wrap(T) {
|
pub fn init(T: type, value: *T) EndpointWrapper.Wrap(T) {
|
||||||
|
checkEndpointType(T);
|
||||||
var ret: EndpointWrapper.Wrap(T) = .{
|
var ret: EndpointWrapper.Wrap(T) = .{
|
||||||
.wrapped = value,
|
.wrapped = value,
|
||||||
.wrapper = .{ .path = value.path },
|
.wrapper = .{ .path = value.path },
|
||||||
|
@ -218,6 +247,7 @@ pub const Listener = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const EndpointType = @typeInfo(@TypeOf(e)).pointer.child;
|
const EndpointType = @typeInfo(@TypeOf(e)).pointer.child;
|
||||||
|
checkEndpointType(EndpointType);
|
||||||
const wrapper = try self.allocator.create(EndpointWrapper.Wrap(EndpointType));
|
const wrapper = try self.allocator.create(EndpointWrapper.Wrap(EndpointType));
|
||||||
wrapper.* = EndpointWrapper.init(EndpointType, e);
|
wrapper.* = EndpointWrapper.init(EndpointType, e);
|
||||||
try endpoints.append(self.allocator, &wrapper.wrapper);
|
try endpoints.append(self.allocator, &wrapper.wrapper);
|
||||||
|
|
Loading…
Add table
Reference in a new issue