mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
json: fix default struct field initialization with long array (#23355)
This commit is contained in:
parent
f821c657a7
commit
5eecd04eee
2 changed files with 21 additions and 2 deletions
20
vlib/json/tests/json_decode_struct_default_test.v
Normal file
20
vlib/json/tests/json_decode_struct_default_test.v
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
struct Bar {
|
||||||
|
b []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo {
|
||||||
|
Bar
|
||||||
|
a []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
str := json.encode(Foo{})
|
||||||
|
assert json.decode(Foo, str)!.str() == 'Foo{
|
||||||
|
Bar: Bar{
|
||||||
|
b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
}
|
||||||
|
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
}'
|
||||||
|
}
|
|
@ -848,8 +848,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
dec.writeln('\t} else {')
|
dec.writeln('\t} else {')
|
||||||
default_str := g.expr_string_opt(field.typ, field.default_expr)
|
default_str := g.expr_string_opt(field.typ, field.default_expr)
|
||||||
lines := default_str.count('\n')
|
if default_str.count(';\n') > 1 {
|
||||||
if lines > 1 {
|
|
||||||
dec.writeln(default_str.all_before_last('\n'))
|
dec.writeln(default_str.all_before_last('\n'))
|
||||||
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${default_str.all_after_last('\n')};')
|
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${default_str.all_after_last('\n')};')
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue