v2 / vlib / v / checker / tests / comptime_selector_assign.vv
31 lines · 26 sloc · 454 bytes · 2d33a7f2819dd5fc1f4aa3b3ca0bcc660810d7af
Raw
1import x.json2 { Any, decode }
2
3struct Income {
4mut:
5 email string
6 code int
7}
8
9pub fn structuring[T](res Any) T {
10 mut typ := T{}
11 res_map := res.as_map()
12
13 $for field in T.fields {
14 if field.name in res_map {
15 $if field.typ is int {
16 typ.$(field.name) = 2
17 }
18 typ.$(field.name) = 3
19 }
20 }
21 return typ
22}
23
24fn main() {
25 res := decode[Any]('{
26 "email": ["sdvsdv", "sds"],
27 "code": 12
28 }')!.as_map()
29
30 structuring[Income](res)
31}
32