@@ -260,12 +260,16 @@ pub fn map_from[T](t T) map[string]Any {
260
260
value := t.$(field.name)
261
261
262
262
$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
+ }
266
270
}
267
- m[field.name] = arr
268
- arr .clear ()
271
+ m[field.name] = top_arr
272
+ top_arr .clear ()
269
273
} $else $if field.is_struct {
270
274
m[field.name] = map_from (value)
271
275
} $else $if field.is_map {
@@ -275,33 +279,33 @@ pub fn map_from[T](t T) map[string]Any {
275
279
} $else $if field.is_option {
276
280
// TODO
277
281
} $else {
278
- // TODO: improve memory usage when convert
282
+ // TODO: simplify check of type & improve memory usage when convert
279
283
$if field.typ is string {
280
- m[field.name] = value. str ()
284
+ m[field.name] = value
281
285
} $else $if field.typ is bool {
282
- m[field.name] = t.$(field.name). str (). bool ()
286
+ m[field.name] = t.$(field.name)
283
287
} $else $if field.typ is i8 {
284
- m[field.name] = t.$(field.name). str (). i8 ()
288
+ m[field.name] = t.$(field.name)
285
289
} $else $if field.typ is i16 {
286
- m[field.name] = t.$(field.name). str (). i16 ()
290
+ m[field.name] = t.$(field.name)
287
291
} $else $if field.typ is i32 {
288
- m[field.name] = t.$(field.name). str (). i32 ()
292
+ m[field.name] = t.$(field.name)
289
293
} $else $if field.typ is int {
290
- m[field.name] = t.$(field.name). str (). int ()
294
+ m[field.name] = t.$(field.name)
291
295
} $else $if field.typ is i64 {
292
- m[field.name] = t.$(field.name). str (). i64 ()
296
+ m[field.name] = t.$(field.name)
293
297
} $else $if field.typ is f32 {
294
- m[field.name] = t.$(field.name). str (). f32 ()
298
+ m[field.name] = t.$(field.name)
295
299
} $else $if field.typ is f64 {
296
- m[field.name] = t.$(field.name). str (). f64 ()
300
+ m[field.name] = t.$(field.name)
297
301
} $else $if field.typ is u8 {
298
- m[field.name] = t.$(field.name). str (). u8 ()
302
+ m[field.name] = t.$(field.name)
299
303
} $else $if field.typ is u16 {
300
- m[field.name] = t.$(field.name). str (). u16 ()
304
+ m[field.name] = t.$(field.name)
301
305
} $else $if field.typ is u32 {
302
- m[field.name] = t.$(field.name). str (). u32 ()
306
+ m[field.name] = t.$(field.name)
303
307
} $else $if field.typ is u64 {
304
- m[field.name] = t.$(field.name). str (). u64 ()
308
+ m[field.name] = t.$(field.name)
305
309
} $else {
306
310
// return error("The type of `${field.name}` can't be decoded. Please open an issue at https://github.com/vlang/v/issues/new/choose")
307
311
}
@@ -310,3 +314,21 @@ pub fn map_from[T](t T) map[string]Any {
310
314
}
311
315
return m
312
316
}
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