1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix vweb using generic method (fix #15888) (#15963)

This commit is contained in:
yuyi 2022-10-05 18:23:47 +08:00 committed by GitHub
parent 46138a2841
commit 6ccdf89546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -467,12 +467,19 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
if methods.len > 0 { if methods.len > 0 {
g.writeln('FunctionData $node.val_var = {0};') g.writeln('FunctionData $node.val_var = {0};')
} }
for method in methods { // sym.methods { typ_vweb_result := g.table.find_type_idx('vweb.Result')
/* for method in methods {
if method.return_type != vweb_result_type { // ast.void_type { // filter vweb route methods (non-generic method)
continue if method.receiver_type != 0 && method.return_type == typ_vweb_result {
rec_sym := g.table.sym(method.receiver_type)
if rec_sym.kind == .struct_ {
if _ := g.table.find_field_with_embeds(rec_sym, 'Context') {
if method.generic_names.len > 0 {
continue
}
}
}
} }
*/
g.comptime_for_method = method.name g.comptime_for_method = method.name
g.writeln('/* method $i */ {') g.writeln('/* method $i */ {')
g.writeln('\t${node.val_var}.name = _SLIT("$method.name");') g.writeln('\t${node.val_var}.name = _SLIT("$method.name");')

View File

@ -64,3 +64,31 @@ fn (mut app App) time_json_pretty() {
'time': time.now().format() 'time': time.now().format()
}) })
} }
struct ApiSuccessResponse<T> {
success bool
result T
}
fn (mut app App) json_success<T>(result T) vweb.Result {
response := ApiSuccessResponse<T>{
success: true
result: result
}
return app.json(response)
}
// should compile, this is a helper method, not exposed as a route
fn (mut app App) some_helper<T>(result T) ApiSuccessResponse<T> {
response := ApiSuccessResponse<T>{
success: true
result: result
}
return response
}
// should compile, the route method itself is not generic
fn (mut app App) ok() vweb.Result {
return app.json(app.some_helper(123))
}