1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gen: fix sumtype custom str (#6534)

This commit is contained in:
Daniel Däschle 2020-10-02 09:32:07 +02:00 committed by GitHub
parent 1ddf46f3c6
commit 8e8e808fc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -297,7 +297,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) {
for field in info.fields {
sym := g.table.get_type_symbol(field.typ)
if !sym.has_method('str') {
mut typ := field.typ
mut typ := field.typ
if typ.is_ptr() {
typ = typ.deref()
}
@ -383,16 +383,13 @@ fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name, fi
}
if has_custom_str {
return '${fn_name}($obj)'
} else {
return 'indent_${fn_name}($obj, indent_count + 1)'
}
} else if sym.kind in [.array, .array_fixed, .map] {
return 'indent_${fn_name}($obj, indent_count + 1)'
} else if sym.kind in [.array, .array_fixed, .map, .sum_type] {
if has_custom_str {
return '${fn_name}(it->${c_name(field_name)})'
}
return 'indent_${fn_name}(it->${c_name(field_name)}, indent_count + 1)'
} else if sym.kind == .sum_type {
return 'indent_${fn_name}(it->${c_name(field_name)}, indent_count + 1)'
} else {
mut method_str := 'it->${c_name(field_name)}'
if sym.kind == .bool {

View File

@ -57,3 +57,21 @@ fn test_pointer() {
st := ST(0)
assert '${&st}' == '&ST(0)'
}
struct Xyz {}
type Hola = Abc | Xyz
fn (h Hola) str() string {
return 'Hola'
}
struct HolaContainer {
h Hola
}
fn test_custom_str_method() {
h := HolaContainer{}
assert h.str() == 'HolaContainer {\n h: Hola\n}'
assert '$h' == 'HolaContainer {\n h: Hola\n}'
}