diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v b/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v index fa96c19afe..00b379abbe 100644 --- a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v +++ b/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v @@ -7,13 +7,13 @@ import json struct App { vweb.Context -mut: +pub mut: db sqlite.DB [server_var] user_id string } fn main() { - vweb.run(8081) + vweb.run(&App{}, 8081) } /* diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5ee6b954af..a75e2ce461 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5749,11 +5749,13 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { comptime_field_name = left.expr.str() c.comptime_fields_type[comptime_field_name] = got_type is_comptime_type_is_expr = true - } else if branch.cond.right is ast.TypeNode && left is ast.TypeNode { + } else if branch.cond.right is ast.TypeNode && left is ast.TypeNode + && sym.kind == .interface_ { // is interface checked_type := c.unwrap_generic((left as ast.TypeNode).typ) should_skip = !c.table.type_implements_interface(checked_type, got_type) + eprintln('> checked_type: $checked_type | got_type: $got_type | should_skip: $should_skip') } else if left is ast.TypeNode { is_comptime_type_is_expr = true left_type := c.unwrap_generic(left.typ) diff --git a/vlib/vweb/vweb_app_test.v b/vlib/vweb/vweb_app_test.v new file mode 100644 index 0000000000..0ebfa6ed80 --- /dev/null +++ b/vlib/vweb/vweb_app_test.v @@ -0,0 +1,63 @@ +module main + +import vweb +import time +import sqlite + +struct App { + vweb.Context +pub mut: + db sqlite.DB [server_var] + user_id string +} + +struct Article { + id int + title string + text string +} + +fn test_a_vweb_application_compiles() { + go fn() { + time.sleep(2 * time.second) + exit(0) + }() + vweb.run(&App{}, 18081) +} + +pub fn (mut app App) init_server() { + app.db = sqlite.connect('blog.db') or { panic(err) } + app.db.create_table('article', [ + 'id integer primary key', + "title text default ''", + "text text default ''", + ]) +} + +pub fn (mut app App) before_request() { + app.user_id = app.get_cookie('id') or { '0' } +} + +['/new_article'; post] +pub fn (mut app App) new_article() vweb.Result { + title := app.form['title'] + text := app.form['text'] + if title == '' || text == '' { + return app.text('Empty text/title') + } + article := Article{ + title: title + text: text + } + println('posting article') + println(article) + sql app.db { + insert article into Article + } + + return app.redirect('/') +} + +fn (mut app App) time() { + app.text(time.now().format()) +}