Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for @fieldParentPointer RLS change #89

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/endpoint/userweb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ fn userIdFromPath(self: *Self, path: []const u8) ?usize {
}

fn getUser(e: *zap.Endpoint, r: zap.Request) void {
const self: Self = @fieldParentPtr("ep", e);
const self: *Self = @fieldParentPtr("ep", e);

if (r.path) |path| {
// /users
if (path.len == e.settings.path.len) {
Expand All @@ -81,7 +82,7 @@ fn listUsers(self: *Self, r: zap.Request) void {
}

fn postUser(e: *zap.Endpoint, r: zap.Request) void {
const self: Self = @fieldParentPtr("ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.body) |body| {
const maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null;
if (maybe_user) |u| {
Expand All @@ -100,7 +101,7 @@ fn postUser(e: *zap.Endpoint, r: zap.Request) void {
}

fn putUser(e: *zap.Endpoint, r: zap.Request) void {
const self: Self = @fieldParentPtr("ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.path) |path| {
if (self.userIdFromPath(path)) |id| {
if (self._users.get(id)) |_| {
Expand All @@ -126,7 +127,7 @@ fn putUser(e: *zap.Endpoint, r: zap.Request) void {
}

fn deleteUser(e: *zap.Endpoint, r: zap.Request) void {
const self: Self = @fieldParentPtr("ep", e);
const self: *Self = @fieldParentPtr("ep", e);
if (r.path) |path| {
if (self.userIdFromPath(path)) |id| {
var jsonbuf: [128]u8 = undefined;
Expand Down
6 changes: 3 additions & 3 deletions examples/middleware/middleware.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const UserMiddleWare = struct {
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {

// this is how we would get our self pointer
const self: Self = @fieldParentPtr("handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;

// do our work: fill in the user field of the context
Expand Down Expand Up @@ -115,7 +115,7 @@ const SessionMiddleWare = struct {
// note that the first parameter is of type *Handler, not *Self !!!
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self: Self = @fieldParentPtr("handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;

context.session = Session{
Expand Down Expand Up @@ -151,7 +151,7 @@ const HtmlMiddleWare = struct {
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {

// this is how we would get our self pointer
const self: Self = @fieldParentPtr("handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;

std.debug.print("\n\nHtmlMiddleware: handling request with context: {any}\n\n", .{context});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const UserMiddleWare = struct {
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {

// this is how we would get our self pointer
const self: Self = @fieldParentPtr("handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;

// do our work: fill in the user field of the context
Expand Down Expand Up @@ -105,7 +105,7 @@ const SessionMiddleWare = struct {
// note that the first parameter is of type *Handler, not *Self !!!
pub fn onRequest(handler: *Handler, r: zap.Request, context: *Context) bool {
// this is how we would get our self pointer
const self: Self = @fieldParentPtr("handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
_ = self;

context.session = Session{
Expand Down Expand Up @@ -155,7 +155,7 @@ const HtmlEndpoint = struct {
}

pub fn get(ep: *zap.Endpoint, r: zap.Request) void {
const self: Self = @fieldParentPtr("ep", ep);
const self: *Self = @fieldParentPtr("ep", ep);
_ = self;

var buf: [1024]u8 = undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/middleware.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt
/// If `breakOnFinish` is `true`, the handler will stop handing requests down the chain if
/// the endpoint processed the request.
pub fn onRequest(handler: *HandlerType, r: zap.Request, context: *ContextType) bool {
var self: Self = @fieldParentPtr("handler", handler);
const self: *Self = @fieldParentPtr("handler", handler);
r.setUserContext(context);
self.endpoint.onRequest(r);

Expand Down
Loading