mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
json: allow passing an anon struct as a decode type (#22228)
This commit is contained in:
parent
0c8ce3bcb9
commit
97c9f5f9e4
9
vlib/json/json_decode_anon_struct_test.v
Normal file
9
vlib/json/json_decode_anon_struct_test.v
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
json_text := '{ "a": "b" }'
|
||||||
|
b := json.decode(struct {
|
||||||
|
a string
|
||||||
|
}, json_text)!.a
|
||||||
|
assert dump(b) == 'b'
|
||||||
|
}
|
@ -130,6 +130,7 @@ pub:
|
|||||||
pos token.Pos
|
pos token.Pos
|
||||||
pub mut:
|
pub mut:
|
||||||
typ Type
|
typ Type
|
||||||
|
stmt Stmt = empty_stmt // for anon struct
|
||||||
end_comments []Comment // comments that after current type node
|
end_comments []Comment // comments that after current type node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3088,6 +3088,9 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
|
|||||||
&& c.table.cur_fn.generic_names.len == 0 {
|
&& c.table.cur_fn.generic_names.len == 0 {
|
||||||
c.error('unexpected generic variable in non-generic function `${c.table.cur_fn.name}`',
|
c.error('unexpected generic variable in non-generic function `${c.table.cur_fn.name}`',
|
||||||
node.pos)
|
node.pos)
|
||||||
|
} else if node.stmt != ast.empty_stmt && node.typ == ast.void_type {
|
||||||
|
c.stmt(mut node.stmt)
|
||||||
|
node.typ = c.table.find_type_idx((node.stmt as ast.StructDecl).name)
|
||||||
}
|
}
|
||||||
return node.typ
|
return node.typ
|
||||||
}
|
}
|
||||||
|
@ -837,8 +837,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
|
|||||||
node.pos)
|
node.pos)
|
||||||
return ast.void_type
|
return ast.void_type
|
||||||
}
|
}
|
||||||
expr := node.args[0].expr
|
mut expr := node.args[0].expr
|
||||||
if expr is ast.TypeNode {
|
if mut expr is ast.TypeNode {
|
||||||
|
expr.typ = c.expr(mut expr)
|
||||||
mut unwrapped_typ := c.unwrap_generic(expr.typ)
|
mut unwrapped_typ := c.unwrap_generic(expr.typ)
|
||||||
if c.needs_unwrap_generic_type(expr.typ) {
|
if c.needs_unwrap_generic_type(expr.typ) {
|
||||||
unwrapped_typ = c.table.unwrap_generic_type(expr.typ, c.table.cur_fn.generic_names,
|
unwrapped_typ = c.table.unwrap_generic_type(expr.typ, c.table.cur_fn.generic_names,
|
||||||
|
@ -3216,8 +3216,13 @@ pub fn (mut f Fmt) string_inter_literal(node ast.StringInterLiteral) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) type_expr(node ast.TypeNode) {
|
pub fn (mut f Fmt) type_expr(node ast.TypeNode) {
|
||||||
f.mark_types_import_as_used(node.typ)
|
if node.stmt == ast.empty_stmt {
|
||||||
f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias))
|
f.mark_types_import_as_used(node.typ)
|
||||||
|
f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias))
|
||||||
|
} else {
|
||||||
|
f.struct_decl(ast.StructDecl{ fields: (node.stmt as ast.StructDecl).fields },
|
||||||
|
true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) type_of(node ast.TypeOf) {
|
pub fn (mut f Fmt) type_of(node ast.TypeOf) {
|
||||||
|
@ -478,9 +478,19 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if p.tok.kind == .key_struct && p.peek_tok.kind == .lcbr {
|
if p.tok.kind == .key_struct && p.peek_tok.kind == .lcbr {
|
||||||
// Anonymous struct
|
if p.expecting_type && p.inside_call_args {
|
||||||
p.next()
|
// Anonymous struct for json.decode
|
||||||
return p.struct_init('', .anon, false)
|
tok_pos := p.tok.pos()
|
||||||
|
return ast.TypeNode{
|
||||||
|
stmt: p.struct_decl(true)
|
||||||
|
pos: tok_pos
|
||||||
|
typ: ast.void_type
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Anonymous struct
|
||||||
|
p.next()
|
||||||
|
return p.struct_init('', .anon, false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) {
|
if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) {
|
||||||
// eof should be handled where it happens
|
// eof should be handled where it happens
|
||||||
|
Loading…
x
Reference in New Issue
Block a user