1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/v/tests/comptime_for_in_field_typeof_test.v
2022-12-31 15:57:55 +02:00

42 lines
792 B
V

struct Ab {
a []int
}
struct ComplexStruct {
a int
b []Ab
c string
d map[string]int
}
fn test_typeof_in_comptime_for_in_fields() {
out := encode_struct(ComplexStruct{})
dump(out)
assert out[0] == 'a'
assert out[1] == typeof[int]().idx.str()
assert out[2] == 'int'
assert out[3] == 'b'
assert out[4] == typeof[[]Ab]().idx.str()
assert out[5] == '[]Ab'
assert out[6] == 'c'
assert out[7] == typeof[string]().idx.str()
assert out[8] == 'string'
assert out[9] == 'd'
assert out[10] == typeof[map[string]int]().idx.str()
assert out[11] == 'map[string]int'
}
fn encode_struct[T](val T) []string {
mut out := []string{}
$for field in T.fields {
value := val.$(field.name)
out << field.name
out << typeof(value).idx.str()
out << typeof(value).name
}
return out
}