From 97c9f5f9e46d74dfcf1d9031bb81e65cd95498f9 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 17 Sep 2024 09:33:17 -0300 Subject: [PATCH] json: allow passing an anon struct as a decode type (#22228) --- vlib/json/json_decode_anon_struct_test.v | 9 +++++++++ vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 3 +++ vlib/v/checker/fn.v | 5 +++-- vlib/v/fmt/fmt.v | 9 +++++++-- vlib/v/parser/expr.v | 16 +++++++++++++--- 6 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 vlib/json/json_decode_anon_struct_test.v diff --git a/vlib/json/json_decode_anon_struct_test.v b/vlib/json/json_decode_anon_struct_test.v new file mode 100644 index 0000000000..fc20b70a3f --- /dev/null +++ b/vlib/json/json_decode_anon_struct_test.v @@ -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' +} diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index d43e80dad0..b4cd5ce81c 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -130,6 +130,7 @@ pub: pos token.Pos pub mut: typ Type + stmt Stmt = empty_stmt // for anon struct end_comments []Comment // comments that after current type node } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 815c91c7d0..db841d63b3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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.error('unexpected generic variable in non-generic function `${c.table.cur_fn.name}`', 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 } diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index ccc26c0feb..52e356f44b 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -837,8 +837,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. node.pos) return ast.void_type } - expr := node.args[0].expr - if expr is ast.TypeNode { + mut expr := node.args[0].expr + if mut expr is ast.TypeNode { + expr.typ = c.expr(mut expr) mut unwrapped_typ := c.unwrap_generic(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, diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e0a5d123e6..ca78f16ae2 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) { - f.mark_types_import_as_used(node.typ) - f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) + if node.stmt == ast.empty_stmt { + 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) { diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index d59c966034..ae86e5e3a0 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -478,9 +478,19 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr { } else { if p.tok.kind == .key_struct && p.peek_tok.kind == .lcbr { - // Anonymous struct - p.next() - return p.struct_init('', .anon, false) + if p.expecting_type && p.inside_call_args { + // Anonymous struct for json.decode + 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) { // eof should be handled where it happens