From 8201b8a45f86edc6165ba2813bc3120c3ee4ee14 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 2 Mar 2025 05:16:02 -0300 Subject: [PATCH] vfmt: fix formatting for an option array of anon structs (fix #23841) (#23844) --- vlib/v/fmt/struct.v | 3 +++ .../tests/array_option_anon_struct_keep.vv | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 vlib/v/fmt/tests/array_option_anon_struct_keep.vv diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 030f10b52..c09c45939 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -169,6 +169,9 @@ fn (mut f Fmt) write_anon_struct_field_decl(field_typ ast.Type, field_anon_decl elem_sym := f.table.sym(sym.info.elem_type) if elem_sym.info is ast.Struct { if elem_sym.info.is_anon { + if field_typ.has_flag(.option) { + f.write('?') + } f.write('[]'.repeat(sym.info.nr_dims)) f.write_anon_struct_field_decl(sym.info.elem_type, field_anon_decl) return true diff --git a/vlib/v/fmt/tests/array_option_anon_struct_keep.vv b/vlib/v/fmt/tests/array_option_anon_struct_keep.vv new file mode 100644 index 000000000..439ba204a --- /dev/null +++ b/vlib/v/fmt/tests/array_option_anon_struct_keep.vv @@ -0,0 +1,22 @@ +module main + +import json + +struct Data { +mut: + a_token ?[]string + dfv ?[]struct { + key string + } +} + +fn main() { + j := '{ + "a_token2": ["one", "two"], + "a_email2": { + "email": "email@email.ru" + } + }' + d := json.decode(Data, j)! + println(d) +} -- 2.39.5