gen: struct str fixes (#6483)

pull/6486/head
Daniel Däschle 2020-09-25 23:12:33 +02:00 committed by GitHub
parent b999d01de7
commit d782de5b00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 36 deletions

View File

@ -320,7 +320,10 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) {
g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; ++i) {')
g.auto_str_funcs.writeln('\t\tindents = string_add(indents, tos_lit(" "));')
g.auto_str_funcs.writeln('\t}')
g.auto_str_funcs.writeln('\treturn _STR("$clean_struct_v_type_name {\\n"')
if info.fields.len == 0 {
g.auto_str_funcs.write('\treturn tos_lit("$clean_struct_v_type_name { }");')
} else {
g.auto_str_funcs.write('\treturn _STR("$clean_struct_v_type_name {\\n"')
for field in info.fields {
mut fmt := g.type_to_fmt(field.typ)
if field.typ.is_ptr() {
@ -359,6 +362,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) {
}
g.auto_str_funcs.writeln(',')
g.auto_str_funcs.writeln('\t\tindents);')
}
g.auto_str_funcs.writeln('}')
}

View File

@ -177,8 +177,8 @@ struct Wrapper4 {
fn test_struct_with_struct_pointer() {
b := Foo{}
w := Wrapper4{&b}
assert '$w' == 'Wrapper4 {\n foo: &Foo {\n }\n}'
assert w.str() == 'Wrapper4 {\n foo: &Foo {\n }\n}'
assert '$w' == 'Wrapper4 {\n foo: &Foo { }\n}'
assert w.str() == 'Wrapper4 {\n foo: &Foo { }\n}'
}
fn test_struct_with_nil() {