v2 / vlib / v / tests / comptime / comptime_map_fields_decode_test.v
24 lines · 21 sloc · 450 bytes · b2a0f6814232681059a42ceb616ff9a518382172
Raw
1struct Maps {
2 a map[string]int
3 b map[string]bool
4 c map[string]f64
5}
6
7fn decode_struct[T](t T) []string {
8 mut decoded := []string{}
9 $for f in T.fields {
10 $if f is $map {
11 k, v := map_key_value(t.$(f.name))
12 decoded << '${typeof(k).name} ${typeof(v).name}'
13 }
14 }
15 return decoded
16}
17
18fn map_key_value[K, V](m map[K]V) (K, V) {
19 return K{}, V{}
20}
21
22fn test_main() {
23 assert decode_struct(Maps{}) == ['string int', 'string bool', 'string f64']
24}
25