mirror of
https://github.com/vlang/v.git
synced 2025-09-16 16:02:29 +03:00
encoding.csv: generic serialization (#15097)
This commit is contained in:
parent
8d24522d02
commit
b4dedcae43
2 changed files with 94 additions and 0 deletions
67
vlib/encoding/csv/to_struct_arr.v
Normal file
67
vlib/encoding/csv/to_struct_arr.v
Normal file
|
@ -0,0 +1,67 @@
|
|||
module csv
|
||||
|
||||
import strconv
|
||||
|
||||
// decode csv to struct
|
||||
pub fn decode<T>(data string) []T {
|
||||
mut result := []T{}
|
||||
if data == '' {
|
||||
return result
|
||||
}
|
||||
|
||||
mut parser := new_reader(data)
|
||||
mut columns_names := []string{}
|
||||
mut i := 0
|
||||
for {
|
||||
items := parser.read() or { break }
|
||||
if i == 0 {
|
||||
for val in items {
|
||||
columns_names << val
|
||||
}
|
||||
} else {
|
||||
mut t_val := T{}
|
||||
$for field in T.fields {
|
||||
col := get_column(field.name, columns_names)
|
||||
if col > -1 && col < items.len {
|
||||
$if field.typ is string {
|
||||
t_val.$(field.name) = items[col]
|
||||
} $else $if field.typ is int {
|
||||
t_val.$(field.name) = items[col].int()
|
||||
} $else $if field.typ is f32 {
|
||||
t_val.$(field.name) = f32(strconv.atof64(items[col]) or { f32(0.0) })
|
||||
} $else $if field.typ is f64 {
|
||||
t_val.$(field.name) = strconv.atof64(items[col]) or { f64(0.0) }
|
||||
} $else $if field.typ is bool {
|
||||
t_val.$(field.name) = string_to_bool(items[col])
|
||||
}
|
||||
}
|
||||
}
|
||||
result << t_val
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fn string_to_bool(val string) bool {
|
||||
l_val := val.to_lower().trim_space()
|
||||
i_val := val.int()
|
||||
if l_val == 'true' {
|
||||
return true
|
||||
}
|
||||
if i_val == 1 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
fn get_column(name string, columns []string) int {
|
||||
for i, val in columns {
|
||||
if val == name {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue