File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change @@ -478,7 +478,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
478
478
c.fn_scope = node.scope
479
479
// Register implicit context var
480
480
typ_veb_result := c.table.get_veb_result_type_idx () // c.table.find_type('veb.Result')
481
- if node.return_type == typ_veb_result {
481
+ if node.is_method && node. return_type == typ_veb_result {
482
482
// Find a custom user Context type first
483
483
mut ctx_idx := c.table.find_type ('main.Context' )
484
484
if ctx_idx < 1 {
Original file line number Diff line number Diff line change
1
+ vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:9: error: undefined ident: `ctx`
2
+ 48 | fn serve_file(name string) veb.Result {
3
+ 49 | content := os.read_file(name) or { panic('Error reading ${name}') }
4
+ 50 | return ctx.html(content)
5
+ | ~~~
6
+ 51 | }
7
+ vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:2: error: `ctx.html(content)` used as value
8
+ 48 | fn serve_file(name string) veb.Result {
9
+ 49 | content := os.read_file(name) or { panic('Error reading ${name}') }
10
+ 50 | return ctx.html(content)
11
+ | ~~~~~~~~~~~~~~~~~~~~~~~~
12
+ 51 | }
Original file line number Diff line number Diff line change
1
+ module main
2
+
3
+ import veb
4
+ import os
5
+
6
+ pub struct User {
7
+ pub mut:
8
+ name string
9
+ id int
10
+ }
11
+
12
+ // Our context struct must embed `veb.Context`!
13
+ pub struct Context {
14
+ veb.Context
15
+ pub mut:
16
+ // In the context struct we store data that could be different
17
+ // for each request. Like a User struct or a session id
18
+ user User
19
+ session_id string
20
+ }
21
+
22
+ pub struct App {
23
+ pub:
24
+ // In the app struct we store data that should be accessible by all endpoints.
25
+ // For example, a database or configuration values.
26
+ secret_key string
27
+ }
28
+
29
+ // This is how endpoints are defined in veb. This is the index route
30
+
31
+ fn main() {
32
+ mut app := &App{
33
+ secret_key: 'secret'
34
+ }
35
+ // Pass the App and context type and start the web server on port 8080
36
+ veb.run[App, Context](mut app, 8080)
37
+ }
38
+
39
+ @['/foo']
40
+ pub fn (app &App) world(mut ctx Context) veb.Result {
41
+ return ctx.text('World')
42
+ }
43
+
44
+ pub fn (app &App) index(mut ctx Context) veb.Result {
45
+ return serve_file('html/index.html')
46
+ }
47
+
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
+ }
You can’t perform that action at this time.
0 commit comments