@@ -260,12 +260,16 @@ pub fn map_from[T](t T) map[string]Any {
260260 value := t.$(field.name)
261261
262262 $if field.is_array {
263- mut arr := []Any{}
264- for variable in value {
265- arr << Any (variable)
263+ mut top_arr := unsafe { []Any{len: t.$(field.name).len} }
264+ for idx, variable in value {
265+ $if variable is $array {
266+ top_arr[idx] = flatten_array (variable)
267+ } $else {
268+ top_arr[idx] = variable
269+ }
266270 }
267- m[field.name] = arr
268- arr .clear ()
271+ m[field.name] = top_arr
272+ top_arr .clear ()
269273 } $else $if field.is_struct {
270274 m[field.name] = map_from (value)
271275 } $else $if field.is_map {
@@ -275,33 +279,33 @@ pub fn map_from[T](t T) map[string]Any {
275279 } $else $if field.is_option {
276280 // TODO
277281 } $else {
278- // TODO: improve memory usage when convert
282+ // TODO: simplify check of type & improve memory usage when convert
279283 $if field.typ is string {
280- m[field.name] = value. str ()
284+ m[field.name] = value
281285 } $else $if field.typ is bool {
282- m[field.name] = t.$(field.name). str (). bool ()
286+ m[field.name] = t.$(field.name)
283287 } $else $if field.typ is i8 {
284- m[field.name] = t.$(field.name). str (). i8 ()
288+ m[field.name] = t.$(field.name)
285289 } $else $if field.typ is i16 {
286- m[field.name] = t.$(field.name). str (). i16 ()
290+ m[field.name] = t.$(field.name)
287291 } $else $if field.typ is i32 {
288- m[field.name] = t.$(field.name). str (). i32 ()
292+ m[field.name] = t.$(field.name)
289293 } $else $if field.typ is int {
290- m[field.name] = t.$(field.name). str (). int ()
294+ m[field.name] = t.$(field.name)
291295 } $else $if field.typ is i64 {
292- m[field.name] = t.$(field.name). str (). i64 ()
296+ m[field.name] = t.$(field.name)
293297 } $else $if field.typ is f32 {
294- m[field.name] = t.$(field.name). str (). f32 ()
298+ m[field.name] = t.$(field.name)
295299 } $else $if field.typ is f64 {
296- m[field.name] = t.$(field.name). str (). f64 ()
300+ m[field.name] = t.$(field.name)
297301 } $else $if field.typ is u8 {
298- m[field.name] = t.$(field.name). str (). u8 ()
302+ m[field.name] = t.$(field.name)
299303 } $else $if field.typ is u16 {
300- m[field.name] = t.$(field.name). str (). u16 ()
304+ m[field.name] = t.$(field.name)
301305 } $else $if field.typ is u32 {
302- m[field.name] = t.$(field.name). str (). u32 ()
306+ m[field.name] = t.$(field.name)
303307 } $else $if field.typ is u64 {
304- m[field.name] = t.$(field.name). str (). u64 ()
308+ m[field.name] = t.$(field.name)
305309 } $else {
306310 // return error("The type of `${field.name}` can't be decoded. Please open an issue at https://github.com/vlang/v/issues/new/choose")
307311 }
@@ -310,3 +314,21 @@ pub fn map_from[T](t T) map[string]Any {
310314 }
311315 return m
312316}
317+
318+ // flatten_array convert a array of values to array of Any
319+ @[inline]
320+ fn flatten_array [T](t []T) []Any {
321+ $if t ! is $array {
322+ return Any (t)
323+ } $else {
324+ mut arr := []Any{}
325+ for variable in t {
326+ $if variable is $array {
327+ arr << flatten_array (variable)
328+ } $else {
329+ arr << Any (variable)
330+ }
331+ }
332+ return arr
333+ }
334+ }
0 commit comments