mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix generic struct to string (#10191)
This commit is contained in:
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 {
|
||||
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
|
||||
mut quote_str := ''
|
||||
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 {
|
||||
quote_str = "'"
|
||||
} else if field.typ in ast.charptr_types {
|
||||
|
33
vlib/v/tests/generics_struct_to_string_test.v
Normal file
33
vlib/v/tests/generics_struct_to_string_test.v
Normal file
@ -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
Block a user