From c653977c15b7fce7607e92cfbbd74aea49278ab8 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 6 May 2020 14:08:48 +0800 Subject: [PATCH] cgen: fix struct with map field str() error --- vlib/v/gen/cgen.v | 2 +- .../tests/string_interpolation_struct_test.v | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index f6beb25eec..1af035a062 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -3271,7 +3271,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { } else if sym.kind == .struct_ { g.auto_str_funcs.write('indents, ') g.auto_str_funcs.write('${field_styp_fn_name}( it->${field.name}${second_str_param} ) ') - } else if sym.kind in [.array, .array_fixed] { + } else if sym.kind in [.array, .array_fixed, .map] { g.auto_str_funcs.write('indents, ') g.auto_str_funcs.write('${field_styp_fn_name}( it->${field.name}) ') } else { diff --git a/vlib/v/tests/string_interpolation_struct_test.v b/vlib/v/tests/string_interpolation_struct_test.v index 130d183f29..5ba4a5a29d 100644 --- a/vlib/v/tests/string_interpolation_struct_test.v +++ b/vlib/v/tests/string_interpolation_struct_test.v @@ -10,12 +10,12 @@ struct Man { fn test_default_struct_string_interpolation() { superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']} s := '$superman' - assert s.contains('Man {') + assert s.starts_with('Man {') assert s.contains("name: 'Superman'") assert s.contains('age: 30') assert s.contains('interests: [') assert s.contains("'being nice'") - assert s.contains('}') + assert s.ends_with('}') // println(s) } @@ -29,7 +29,24 @@ fn test_fixed_array_struct_string_interpolation() { x := 2.32 ctx.vb = [1.1, x, 3.3, 4.4, 5.0, 6.0, 7.0, 8.9]!! s := '$ctx' - assert s.contains('Context {') + assert s.starts_with('Context {') assert s.contains('vb: [1.1, 2.32, 3.3, 4.4, 5, 6, 7, 8.9]') - assert s.contains('}') + assert s.ends_with('}') +} + +struct Info { + name string + dict map[string]int +} + +fn test_struct_map_field_string_interpolation() { + info := Info{ + name: 'test' + dict: {'a': 1, 'b': 2} + } + s := '$info' + assert s.starts_with('Info {') + assert s.contains("name: 'test'") + assert s.contains("dict: {'a': 1, 'b': 2}") + assert s.ends_with('}') }