v2 / vlib / v / tests / comptime / comptime_var_assignment_test.v
31 lines · 29 sloc · 501 bytes · 66207866bb98f11345ce49c67014dde5b4a65cdb
Raw
1struct FixedStruct1 {
2 a int
3 b string
4 c ?int
5 d ?string
6}
7
8fn encode_struct[T](val T) []string {
9 mut out := []string{}
10 $for field in T.fields {
11 value := val.$(field.name)
12 $if field.is_option {
13 gg := value ?
14 println(gg)
15 out << '${gg}'
16 } $else {
17 gg := value
18 println(gg)
19 out << gg.str()
20 }
21 }
22 return out
23}
24
25fn test_main() {
26 out := encode_struct(FixedStruct1{1, 'foo', 4, 'bar'})
27 assert out[0] == '1'
28 assert out[1] == 'foo'
29 assert out[2] == '4'
30 assert out[3] == 'bar'
31}
32