mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix printing struct with skip fields (#15224)
This commit is contained in:
parent
f1ebfb2d42
commit
0b0c496eff
@ -843,14 +843,21 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
|||||||
fn_builder.writeln('\treturn res;')
|
fn_builder.writeln('\treturn res;')
|
||||||
fn_builder.writeln('}')
|
fn_builder.writeln('}')
|
||||||
}
|
}
|
||||||
fn_body.writeln('\tstring res = str_intp( ${info.fields.len * 4 + 3}, _MOV((StrIntpData[]){')
|
// find skip fields
|
||||||
|
mut skip_field_count := 0
|
||||||
|
for field in info.fields {
|
||||||
|
if attr := field.attrs.find_first('str') {
|
||||||
|
if attr.arg == 'skip' {
|
||||||
|
skip_field_count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn_body.writeln('\tstring res = str_intp( ${(info.fields.len - skip_field_count) * 4 + 3}, _MOV((StrIntpData[]){')
|
||||||
fn_body.writeln('\t\t{_SLIT("$clean_struct_v_type_name{\\n"), 0, {.d_c=0}},')
|
fn_body.writeln('\t\t{_SLIT("$clean_struct_v_type_name{\\n"), 0, {.d_c=0}},')
|
||||||
for i, field in info.fields {
|
for i, field in info.fields {
|
||||||
// Skip `str:skip` fields
|
// Skip `str:skip` fields
|
||||||
if attr := field.attrs.find_first('str') {
|
if attr := field.attrs.find_first('str') {
|
||||||
if attr.arg == 'skip' {
|
if attr.arg == 'skip' {
|
||||||
fn_body.writeln('{_SLIT(""), 0, {.d_c=0}},')
|
|
||||||
fn_body.writeln('{_SLIT(""), 0, {.d_c=0}},')
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
3
vlib/v/tests/inout/printing_struct_with_skip_fields.out
Normal file
3
vlib/v/tests/inout/printing_struct_with_skip_fields.out
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Foo{
|
||||||
|
name: 'Peter'
|
||||||
|
}
|
9
vlib/v/tests/inout/printing_struct_with_skip_fields.vv
Normal file
9
vlib/v/tests/inout/printing_struct_with_skip_fields.vv
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
struct Foo {
|
||||||
|
name string
|
||||||
|
age int [str: skip]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
x := Foo{'Peter', 25}
|
||||||
|
println(x)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user