Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Feb 19, 2025
1 parent a694918 commit 01d0e76
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.fn_scope = node.scope
// Register implicit context var
typ_veb_result := c.table.get_veb_result_type_idx() // c.table.find_type('veb.Result')
if node.return_type == typ_veb_result {
if node.is_method && node.return_type == typ_veb_result {
// Find a custom user Context type first
mut ctx_idx := c.table.find_type('main.Context')
if ctx_idx < 1 {
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/veb_ctx_on_fn_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:9: error: undefined ident: `ctx`
48 | fn serve_file(name string) veb.Result {
49 | content := os.read_file(name) or { panic('Error reading ${name}') }
50 | return ctx.html(content)
| ~~~
51 | }
vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:2: error: `ctx.html(content)` used as value
48 | fn serve_file(name string) veb.Result {
49 | content := os.read_file(name) or { panic('Error reading ${name}') }
50 | return ctx.html(content)
| ~~~~~~~~~~~~~~~~~~~~~~~~
51 | }
51 changes: 51 additions & 0 deletions vlib/v/checker/tests/veb_ctx_on_fn_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module main

import veb
import os

pub struct User {
pub mut:
name string
id int
}

// Our context struct must embed `veb.Context`!
pub struct Context {
veb.Context
pub mut:
// In the context struct we store data that could be different
// for each request. Like a User struct or a session id
user User
session_id string
}

pub struct App {
pub:
// In the app struct we store data that should be accessible by all endpoints.
// For example, a database or configuration values.
secret_key string
}

// This is how endpoints are defined in veb. This is the index route

fn main() {
mut app := &App{
secret_key: 'secret'
}
// Pass the App and context type and start the web server on port 8080
veb.run[App, Context](mut app, 8080)
}

@['/foo']
pub fn (app &App) world(mut ctx Context) veb.Result {
return ctx.text('World')
}

pub fn (app &App) index(mut ctx Context) veb.Result {
return serve_file('html/index.html')
}

fn serve_file(name string) veb.Result {
content := os.read_file(name) or { panic('Error reading ${name}') }
return ctx.html(content)
}

0 comments on commit 01d0e76

Please sign in to comment.