cgen: fix generic struct to string (#10191)
parent
306c16f0fa
commit
f3274700cd
|
@ -94,12 +94,12 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
|
||||||
|
|
||||||
for i, field in info.fields {
|
for i, field in info.fields {
|
||||||
mut ptr_amp := if field.typ.is_ptr() { '&' } else { '' }
|
mut ptr_amp := if field.typ.is_ptr() { '&' } else { '' }
|
||||||
base_fmt := g.type_to_fmt1(field.typ)
|
base_fmt := g.type_to_fmt1(g.unwrap_generic(field.typ))
|
||||||
|
|
||||||
// manage prefix and quote symbol for the filed
|
// manage prefix and quote symbol for the filed
|
||||||
mut quote_str := ''
|
mut quote_str := ''
|
||||||
mut prefix := ''
|
mut prefix := ''
|
||||||
sym := g.table.get_type_symbol(field.typ)
|
sym := g.table.get_type_symbol(g.unwrap_generic(field.typ))
|
||||||
if sym.kind == .string {
|
if sym.kind == .string {
|
||||||
quote_str = "'"
|
quote_str = "'"
|
||||||
} else if field.typ in ast.charptr_types {
|
} else if field.typ in ast.charptr_types {
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
struct Info<T> {
|
||||||
|
data T
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_info<T>(res Info<T>) string {
|
||||||
|
return '$res'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generic_struct_to_string() {
|
||||||
|
mut ret := get_info(Info<bool>{true})
|
||||||
|
println(ret)
|
||||||
|
assert ret.contains('data: true')
|
||||||
|
|
||||||
|
ret = get_info(Info<int>{123})
|
||||||
|
println(ret)
|
||||||
|
assert ret.contains('data: 123')
|
||||||
|
|
||||||
|
ret = get_info(Info<f32>{f32(2.2)})
|
||||||
|
println(ret)
|
||||||
|
assert ret.contains('data: 2.2')
|
||||||
|
|
||||||
|
ret = get_info(Info<f64>{2.2})
|
||||||
|
println(ret)
|
||||||
|
assert ret.contains('data: 2.2')
|
||||||
|
|
||||||
|
ret = get_info(Info<string>{'aaa'})
|
||||||
|
println(ret)
|
||||||
|
assert ret.contains("data: 'aaa'")
|
||||||
|
|
||||||
|
ret = get_info(Info<u64>{u64(234)})
|
||||||
|
println(ret)
|
||||||
|
assert ret.contains('data: 234')
|
||||||
|
}
|
Loading…
Reference in New Issue