1
0
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:
yuyi 2022-07-26 09:37:16 +08:00 committed by GitHub
parent f1ebfb2d42
commit 0b0c496eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View File

@ -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('}')
}
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}},')
for i, field in info.fields {
// Skip `str:skip` fields
if attr := field.attrs.find_first('str') {
if attr.arg == 'skip' {
fn_body.writeln('{_SLIT(""), 0, {.d_c=0}},')
fn_body.writeln('{_SLIT(""), 0, {.d_c=0}},')
continue
}
}

View File

@ -0,0 +1,3 @@
Foo{
name: 'Peter'
}

View File

@ -0,0 +1,9 @@
struct Foo {
name string
age int [str: skip]
}
fn main() {
x := Foo{'Peter', 25}
println(x)
}