gen: fix sumtype custom str (#6534)

pull/6535/head
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

@ -383,16 +383,13 @@ fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name, fi
} }
if has_custom_str { if has_custom_str {
return '${fn_name}($obj)' 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 { if has_custom_str {
return '${fn_name}(it->${c_name(field_name)})' return '${fn_name}(it->${c_name(field_name)})'
} }
return 'indent_${fn_name}(it->${c_name(field_name)}, indent_count + 1)' 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 { } else {
mut method_str := 'it->${c_name(field_name)}' mut method_str := 'it->${c_name(field_name)}'
if sym.kind == .bool { if sym.kind == .bool {

View File

@ -57,3 +57,21 @@ fn test_pointer() {
st := ST(0) st := ST(0)
assert '${&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}'
}